Defold provides a lightweight yet powerful API for handling input, making a button click straightforward for both new and experienced developers. This guide walks through the essential steps to detect presses and trigger reliable actions using the engine’s built-in patterns.
Whether you are prototyping a mobile tap or wiring a complex UI, understanding messages, input bindings, and action mapping will keep your game responsive and maintainable.
| Action | Input Source | Message | Trigger Context |
|---|---|---|---|
| Button press | Physical key | on_press | Game or UI scene |
| Tap | Touch screen | on_input | UI element hit area |
| Shortcut | Gamepad button | gamepad_pressed | Menu navigation |
| Repeat | Keyboard key hold | on_hold | Continuous movement |
Setting Up Input Bindings
Input bindings map physical controls to logical actions, keeping your code platform independent. By defining bindings in the project settings, you can later refer to them by name instead of hardcoding keys or touch IDs.
Each binding can handle multiple devices, so a single action like accept can represent both the Return key and a controller A button.
Listening for On Input Messages
Using on_input in Script Components
Inside a script attached to a game object, you implement the on_input function to react when a bound action occurs. The function receives an action_id and a table containing pressed states, allowing you to branch logic cleanly.
Here you can call helper functions, post custom messages, or directly modify components while preserving a clear separation between input detection and gameplay behavior.
Creating a Simple UI Button
Button Component and Hit Areas
For touch devices, a UI button relies on a component that defines a hit area and reports pointer events. The engine dispatches on_click messages that you can connect to either Lua handlers or custom script logic.
By nesting your button inside a container and anchoring it properly, you ensure consistent placement across different resolutions and aspect ratios.
Scripting the Click Behavior
When a binding or UI event fires, you typically send a message to the same game object or to a dedicated controller. Lightweight actions may run inline, while heavier sequences can be queued or handled by a separate manager object.
Using go.post_message with a unique message id keeps traffic predictable, and pairing it with guards such as enabled flags prevents stray triggers during cutscenes or loading screens.
Final Recommendations
- Define clear input bindings for all actions in Project Settings.
- Keep input handling decoupled from gameplay logic using messages.
- Use UI button components for touch and mouse interactions.
- Guard click handlers with state flags to avoid accidental triggers.
- Test across target devices to ensure consistent response times.
FAQ
Reader questions
How do I make a script respond when a keyboard button is pressed in Defold?
Bind the desired key in Project Settings, then implement on_input in your script and check for the matching action_id with msg.action_id to trigger your logic.
Can I simulate a button click from code without physical input?
Yes, you can post a custom message such as msg.post("#button", "click") or directly call the associated handler function to imitate a click event.
How do I handle button clicks for touch-based UI buttons?
Attach a script to the UI button and connect to the on_click callback, or listen for on_input with a specific action bound to touch or mouse interactions.
What is the best practice for handling repeated presses of the same button?
Use on_hold or implement a cooldown timer inside your handler to rate-limit repeated execution while keeping input responsive.