Skill v1.0.0
LLM-judged scan90/100version: "1.0.0" name: godot-input description: Godot 4 输入处理,InputMap 动作代替硬编码按键,Input.get_axis get_vector 死区,键盘手柄双绑定,_input _unhandled_input _process 轮询的选择,is_action_just_pressed 与 _physics_process 帧对齐,运行时改键 action_erase_events action_add_event 存档,鼠标捕获 MOUSE_MODE_CAPTURED,输入缓冲,触屏 VirtualJoystick,手柄震动 start_joy_vibration 热插拔 joy_connection_changed。在写移动 跳跃 攻击 视角控制 改键菜单 手柄支持 时用。input map keybinding gamepad remap deadzone.
Godot 输入
玩法输入一律走 InputMap 动作,同一个动作绑键盘与手柄,用 Input 的轴和向量接口读值。不管 UI 控件的焦点与点击,也不管按键提示文本,见 godot-ui。
先查内置
| 需求 | 用这个 | 别自己写 | |
|---|---|---|---|
| 判断按键 | InputMap 动作加 Input.is_action_pressed | event.keycode == KEY_W | |
| 一维轴 二维移动 | Input.get_axis Input.get_vector | 分别读两个方向再相减拼向量 | |
| 摇杆死区 | 动作的 deadzone,get_vector 取各动作死区平均值做圆形死区,第五个参数 deadzone 可覆盖 | if abs(x) < 0.2 | |
| 手柄支持 | 同一动作加 InputEventJoypadButton InputEventJoypadMotion | 另写一套手柄分支 | |
| 改键 | InputMap.action_erase_event 加 action_add_event | 自己维护按键表 | |
| 触屏摇杆 | VirtualJoystick 4.7+,TouchScreenButton | 自己解析 InputEventScreenTouch | |
| 模拟输入 | Input.action_press,Input.parse_input_event | 直接改自己的状态 |
定义动作
在项目设置 Input Map 页添加,键盘与手柄放进同一个动作:
[input]jump={"deadzone": 0.2,"events": Array[InputEvent]([Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null), Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":0,"pressure":0.0,"pressed":false,"script":null)])}
- 移动键绑
physical_keycode,编辑器里选 Physical Keycode,AZERTY 布局上 WASD 位置不变。要字符含义的快捷键才用keycode - 摇杆每个方向一个
InputEventJoypadMotion:axis_value-1.0 绑move_left,+1.0 绑move_right。扳机轴只有 +1.0 device为 -1 表示所有设备。本地多人给每位玩家单独的动作如p1_jump,指定各自设备编号- 动作名用玩法语义,不复用
ui_acceptui_up这类内置动作,它们驱动 UI 焦点
extends CharacterBody2Dconst SPEED := 220.0func _physics_process(_delta: float) -> void:velocity = Input.get_vector(&"move_left", &"move_right", &"move_up", &"move_down") * SPEEDmove_and_slide()
get_vector 的长度已限制为 1,不用再 normalized()。只有横向的平台游戏用 Input.get_axis(&"move_left", &"move_right")。模拟量用 Input.get_action_strength。
选回调
| 方式 | 适用 | |
|---|---|---|
_physics_process 里轮询 Input.* | 持续状态:移动,蓄力,按住 | |
_unhandled_input(event) | 一次性动作:跳,攻击,交互。UI 已处理的事件到不了这里 | |
_input(event) | 必须先于 UI 看到的:全局热键,改键捕获 |
事件顺序:_input,GUI,_shortcut_input,_unhandled_key_input,_unhandled_input。处理完调 get_viewport().set_input_as_handled()。
- 事件回调里用
event.is_action_pressed(&"jump"),文档要求不在输入处理中用Input.is_action_just_pressed。默认不含按住重复,要重复传allow_echo = true Input.is_action_just_pressed在_process里按帧、在_physics_process里按物理 tick 判断。一帧跑多个 tick 时只有一个 tick 为 true,按下后迅速松开也返回 true- 在哪个回调里消费就在哪个回调里读。别在
_process里置标志让物理消费,一帧里物理可能跑 0 次或多次,标志会晚到或被重复消费
输入缓冲
_unhandled_input 记录,物理里消费,时间用 Engine.get_physics_frames(),不用毫秒,回放时结果一致:
extends CharacterBody2Dconst BUFFER_TICKS := 6const JUMP_SPEED := -420.0var _jump_pressed_tick: int = -1000func _unhandled_input(event: InputEvent) -> void:if event.is_action_pressed(&"jump"):_jump_pressed_tick = Engine.get_physics_frames()func _physics_process(delta: float) -> void:if not is_on_floor():velocity += get_gravity() * deltaelif Engine.get_physics_frames() - _jump_pressed_tick <= BUFFER_TICKS:velocity.y = JUMP_SPEED_jump_pressed_tick = -1000move_and_slide()
改键
extends Nodeconst SAVE_PATH := "user://keybinds.cfg"func rebind(action: StringName, event: InputEventKey) -> void:_apply(action, event)var cfg := ConfigFile.new()cfg.load(SAVE_PATH)cfg.set_value("keys", action, event.physical_keycode)cfg.save(SAVE_PATH)func load_saved(actions: Array[StringName]) -> void:var cfg := ConfigFile.new()if cfg.load(SAVE_PATH) != OK:returnfor action in actions:if cfg.has_section_key("keys", action):var event := InputEventKey.new()event.physical_keycode = cfg.get_value("keys", action) as Key_apply(action, event)func _apply(action: StringName, event: InputEventKey) -> void:for old: InputEvent in InputMap.action_get_events(action):if old is InputEventKey:InputMap.action_erase_event(action, old)InputMap.action_add_event(action, event)
- 只清同一类设备的事件,改键盘键不要清掉手柄绑定
- 存档存键码整数,启动时调一次
load_saved - 恢复默认:
InputMap.load_from_project_settings()再删存档文件。冲突检测遍历InputMap.get_actions()逐个调InputMap.action_has_event() - 捕获新按键:进入监听状态后,在
_input里接第一个pressed的InputEventKey,调set_input_as_handled(),Esc 取消。手柄接InputEventJoypadButton
鼠标与触屏
- 视角控制:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED,在_unhandled_input里读InputEventMouseMotion.screen_relative,relative会按拉伸缩放,灵敏度随分辨率变。打开菜单时切回Input.MOUSE_MODE_VISIBLE - 2D 世界坐标用
get_global_mouse_position(),get_viewport().get_mouse_position()是视口坐标 Input.use_accumulated_input默认 true,同一帧鼠标移动合并成一个事件。自由画线需要逐点轨迹时关掉- VirtualJoystick 4.7+ 是 Control,
action_leftaction_rightaction_upaction_down指到 InputMap 动作后,Input.get_vector照常读,玩法代码不用改。action_*默认是ui_*动作,必须改成玩法动作。它的deadzone_ratio默认 0.0,设了以后先于动作死区生效 - 按钮用 TouchScreenButton 的
action属性。input_devices/pointing/emulate_mouse_from_touch默认开,触摸合成鼠标事件
手柄
- 震动:
Input.start_joy_vibration(device, weak, strong, duration),两个马达强度各 0 到 1,duration为 0 时尽量久,约 65 秒,Input.stop_joy_vibration提前停。给玩家留关闭选项 - 热插拔:连
Input.joy_connection_changed(device, connected)信号,已连接的设备编号用Input.get_connected_joypads()
陷阱
- 两次
Input.get_axis拼向量没有圆形死区,斜向长度到 1.414 - InputMap 只有键盘事件,插手柄毫无反应。每个动作都检查手柄绑定
- 用
Input.is_key_pressed(KEY_W)或event.keycode判断玩法键,改键和键盘布局都失效 _input里处理游戏内点击会穿透按钮,改_unhandled_inputInput.is_action_pressed等轮询不看 GUI 焦点。LineEdit 聚焦打字时,W 键照样让角色移动,聊天框或改名框打开时要自己暂停轮询- 把
ui_acceptui_cancel当玩法键,菜单打开时同时触发 - 键盘幽灵键让某些组合键的
is_action_pressedis_action_just_pressed为 false,组合键别设计得太多