Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

hande text input from mobile keyboard via gui.show_keyboard() (Defold engine)

  1. User

    hande text input from mobile keyboard via gui.show_keyboard() (Defold engine)

  2. Assistant

    In Defold, the gui.show_keyboard() function is used to show the on-screen keyboard which is especially useful for mobile games where text input from the user is required. Handling text input from a mobile keyboard via gui.show_keyboard() in Defold involves the following steps:

    1. Ensure that you have added an input field in your GUI scene by creating a node and attaching a text or input node type to it.

    2. In your GUI script, you need to add functionality to handle the keyboard input. This includes detecting when the text field is selected and invoking gui.show_keyboard() function.

    Here's an example of how you might do this:

    In your GUI script (your_gui_script.gui_script):

    function init(self)
        -- Assuming you have a text node called "text_field" in your GUI.
        self.text_field = gui.get_node("text_field")
    
        -- Variable to keep track of the input focus
        self.input_focused = false
    end
    
    function on_input(self, action_id, action)
        if action_id == hash("touch") and action.pressed then
            -- Check if the text field was touched
            if gui.pick_node(self.text_field, action.x, action.y) then
                -- Input field was selected, show the keyboard
                gui.show_keyboard(gui.KEYBOARD_TYPE_DEFAULT, false)
                self.input_focused = true
            else
                -- Deselect the input field if another area is touched
                self.input_focused = false
                gui.hide_keyboard()
            end
        end
    end
    
    function on_text(self, text)
        -- Only update the text if the input field is focused
        if self.input_focused then
            local current_text = gui.get_text(self.text_field)
            current_text = current_text .. text
            gui.set_text(self.text_field, current_text)
        end
    end
    
    function on_text_input(self, char)
        -- Only update the text if the input field is focused
        if self.input_focused then
            -- You can perform additional checks to filter the input here
            -- Append the character to the current text
            local current_text = gui.get_text(self.text_field)
            current_text = current_text .. char
            gui.set_text(self.text_field, current_text)
        end
    end
    
    function on_key_down(self, key_id, key)
        -- Only handle the key input if the input field is focused
        if self.input_focused then
            if key == gui.KEY_BACKSPACE then
                -- Handle backspace
                local current_text = gui.get_text(self.text_field)
                current_text = string.sub(current_text, 1, -2) -- removes the last character
                gui.set_text(self.text_field, current_text)
            end
        end
    end
    

    Make sure that you've enabled the keyboard related triggers in your input bindings in Defold.

    • "touch" should be set to trigger when a touch occurs.
    • "text" should be set to trigger when text is inputted. This might not be directly available in the input bindings, as handling raw text input isn't done through action mappings but through the on_text() function, which is triggered from the engine.
    • "key_down" should be set to trigger when a key is pressed on the keyboard.

    Remember that the mobile keyboard will not automatically dispatch action ids that relate to keys (like "enter", "backspace", etc.) as you would get on a physical keyboard. You will have to handle what the keyboard sends as text in on_text and on_input, and manage the text accordingly.

    Lastly, it's also important to properly manage the text entry and display updated text since the GUI doesn't provide a complete text input field with its behaviors. You will have to manage cursor position, selection, clipboard actions, and more if required for your application.

  3. User

    why don't you call functions on_text, on_text_input, on_key_down ?

  4. Assistant

    In Defold, the functions on_input, on_text, on_text_input, and on_key_down are all callback functions that the Defold engine calls automatically in response to specific events. You do not call these functions manually in your code; instead, you provide their implementations (or override them) in your script files so the engine knows how to handle corresponding events.

    Here's a brief overview of each:

    • on_input is a callback function that is called by the Defold engine when there is any input action (e.g., touch or mouse events), as specified by your input bindings.

    • on_text (if provided by Defold) would be a callback function that receives text input events, such as those coming from an IME (Input Method Editor). In practice, Defold executes the on_input callback with action.text when it comes to text, so typically you would check action.text inside on_input to handle text input.

    • on_text_input is usually used in environments (like LÖVE 2D) to catch text input, but in Defold, text input is generally handled in the on_input function by checking for action.text.

    • on_key_down is not a Defold engine callback. Key input events are also received through the on_input callback. You would normally check action_id and action.pressed to determine which key was pressed and whether it was a press event.

    When implementing these callbacks, the Defold engine takes care of calling them when appropriate:

    -- This callback is triggered by the engine when there are any input events
    function on_input(self, action_id, action)
        if action_id and action then
            -- your event handling code here...
        end
    end
    
    -- This callback is not a standard Defold callback; 
    -- text input should be handled within the on_input function.
    --[[
    function on_text(self, text)
        -- your text input handling code here...
    end
    ]]
    
    -- There is no on_text_input callback in Defold - use on_input instead.
    -- There is no on_key_down callback in Defold - use on_input instead.
    

    If you were to see functions like on_text_input or on_key_down mentioned in the context of Defold, they would likely be examples of code that's intended to be placed within the on_input callback to handle specific types of input, but they aren't independent callbacks.

    Here's an example demonstrating how text input might be correctly handled in the on_input callback within a Defold GUI script:

    function on_input(self, action_id, action)
        if self.input_focused then
            if action_id then
                -- Handle key press here if action_id corresponds to keyboard keys
            end
            if action.text then
                -- Text input has occurred, append action.text to your GUI's text node
                local current_text = gui.get_text(self.text_field)
                current_text = current_text .. action.text
                gui.set_text(self.text_field, current_text)
            end
            if action_id == hash("backspace") and action.pressed then
                -- Handle backspace (if backspace was mapped to an action_id in your input bindings)
                local current_text = gui.get_text(self.text_field)
                current_text = string.sub(current_text, 1, -2) -- Remove the last character
                gui.set_text(self.text_field, current_text)
            end
        end
    end
    

    It is important to note that the names of the callbacks (such as on_input, on_message, init, etc.) are predefined by the Defold engine. They are invoked automatically and should not be renamed or invoked by the user in the code. Instead, you are supposed to provide the implementation for these callbacks to define how your game should handle such events.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.