godot-input-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGodot Input Patterns
Godot 输入模式
Purpose
用途
Implement Godot input through InputMap actions with correct event propagation, device support, and rebinding.
通过InputMap动作实现Godot输入,确保正确的事件传播、设备支持与按键重绑定。
Use When
适用场景
- adding or unifying input handling for a Godot game
- gameplay scripts read raw keycodes, scancodes, or joypad button indices directly
- input behaves differently between gameplay, UI, and pause states and the propagation order is unclear
- 为Godot游戏添加或统一输入处理逻辑
- 游戏玩法脚本直接读取原始键码、扫描码或手柄按钮索引
- 输入在游戏玩法、UI与暂停状态下表现不一致,且事件传播顺序不明确
Inputs
输入项
- list of gameplay actions (move, jump, fire, pause, confirm)
- supported devices (keyboard/mouse, joypad, touch) per platform target
- UI flow and pause/overlay states that consume input
- rebinding and accessibility requirements
- 游戏玩法动作列表(移动、跳跃、射击、暂停、确认)
- 各目标平台支持的设备(键盘/鼠标、手柄、触控)
- 会消耗输入的UI流程与暂停/覆盖状态
- 按键重绑定与无障碍需求
Process
流程
- define every action in the InputMap (project settings or at startup) and bind devices to actions; gameplay code calls or handles
Input.is_action_*actions, never raw keycodesInputEvent - choose polling versus events deliberately: poll continuous state (movement axes via ) in
Input.get_vector, and handle discrete edges (just pressed/released) through events or_physics_processis_action_just_pressed - respect the propagation order — , then Control
_input, then_gui_input— and put gameplay input in_unhandled_inputso UI consumes events first; call_unhandled_inputexplicitly when a layer swallows an eventset_input_as_handled() - handle joypads through actions with deadzone configured per action, listen to for hot-plug, and verify mappings on each exported platform
joy_connection_changed - for touch targets, decide between handling and
InputEventScreenTouch, and keep on-screen controls as a separate Control layer bound to the same actionsemulate_mouse_from_touch - implement rebinding by editing InputMap at runtime (/
action_erase_events), persist bindings to user settings, and expose them through the accessibility optionsaction_add_event - define how paused state affects input: which nodes keep processing via , and which actions (pause/menu) must work while the tree is paused
process_mode
- 在InputMap中定义所有动作(通过项目设置或启动时配置),并为动作绑定设备;游戏玩法代码调用或处理
Input.is_action_*动作,绝不直接使用原始键码InputEvent - 谨慎选择轮询与事件两种方式:在中轮询持续状态(通过
_physics_process获取移动轴),通过事件或Input.get_vector处理离散触发(刚按下/释放)is_action_just_pressed - 遵循事件传播顺序——→ Control的
_input→_gui_input——将游戏玩法输入放在_unhandled_input中,确保UI优先消耗事件;当某个层级需要拦截事件时,显式调用_unhandled_inputset_input_as_handled() - 通过配置了死区的动作处理手柄输入,监听事件处理热插拔,并在每个导出平台上验证映射
joy_connection_changed - 对于触控目标,在处理与
InputEventScreenTouch之间做出选择,并将屏幕控件作为独立的Control层绑定到相同动作emulate_mouse_from_touch - 通过运行时编辑InputMap实现按键重绑定(使用/
action_erase_events),将绑定设置持久化到用户配置中,并通过无障碍选项暴露给玩家action_add_event - 定义暂停状态对输入的影响:哪些节点通过保持处理,哪些动作(暂停/菜单)必须在树体暂停时仍能生效
process_mode
Outputs
输出项
- InputMap action inventory with device bindings and deadzones
- propagation rules: which layer handles what, in which callback
- rebinding and persistence design
- device and platform test scenarios (hot-plug, focus loss, touch emulation)
- 包含设备绑定与死区设置的InputMap动作清单
- 传播规则:哪些层级处理哪些输入,使用哪个回调函数
- 按键重绑定与持久化设计方案
- 设备与平台测试场景(热插拔、焦点丢失、触控模拟)
Quality Bar
质量标准
- no gameplay script references keycodes, scancodes, or joypad indices outside the InputMap setup
- UI reliably consumes events before gameplay, and nothing double-handles an event after
set_input_as_handled() - movement uses (or equivalent) with per-action deadzones, not per-frame keycode checks
Input.get_vector - pause/menu actions keep working while the tree is paused, and nothing else does
- rebinds survive a restart and are reflected in any input prompts shown to the player
- 游戏玩法脚本除InputMap配置外,不得引用键码、扫描码或手柄索引
- UI能可靠地优先于游戏玩法消耗事件,调用后不会有重复处理事件的情况
set_input_as_handled() - 移动操作使用带动作级死区的(或等效方法),而非逐帧检查键码
Input.get_vector - 暂停/菜单动作在树体暂停时仍能生效,其他动作则不能
- 按键重绑定设置在重启后仍保留,并能反映在向玩家展示的所有输入提示中
Common Failure Modes
常见失败模式
- mixing and
_inputpolling for the same action, double-counting presses across frames_process - gameplay input in so UI clicks also fire gameplay actions underneath
_input - hardcoded joypad button indices that differ between controller types and platforms
- pause menus that cannot be closed because their own input stopped processing with the tree
- touch handled through mouse emulation only, breaking multi-touch (move plus fire) on mobile
- 对同一动作同时使用与
_input轮询,导致跨帧重复计数按键按下事件_process - 将游戏玩法输入放在中,导致UI点击同时触发下层的游戏玩法动作
_input - 硬编码手柄按钮索引,导致在不同控制器类型与平台上表现不一致
- 暂停菜单因树体暂停而停止处理输入,无法关闭
- 仅通过鼠标模拟处理触控,破坏移动端的多点触控(如移动+射击)功能
Related Agents
关联Agent
- godot-reviewer
- gameplay-programmer
- ui-programmer
- godot-reviewer
- gameplay-programmer
- ui-programmer
Related Commands
关联命令
- input-review
- godot-review
- verify
- input-review
- godot-review
- verify
Related Skills
关联技能
- godot-signals-patterns
- godot-scene-architecture
- godot-signals-patterns
- godot-scene-architecture
Notes
注意事项
- Keep this skill aligned with the relevant rules layer and current project documentation.
- The engine-neutral action-abstraction policy lives in ; this skill is the Godot-specific implementation of it.
engineering-common/input-abstraction
- 保持本技能与相关规则层及当前项目文档一致
- 引擎无关的动作抽象策略位于;本技能是该策略在Godot中的具体实现
engineering-common/input-abstraction