用途:为 Godot 4.3+ 项目实现输入处理,涵盖 InputEvent 体系、Input Map 动作、手柄/游戏手柄、鼠标与触摸、动作重绑定及输入架构。示例同时给出 GDScript 与 C#,不含废弃 API。
主要能力:说明输入事件流与各回调(_input、_shortcut_input、_unhandled_key_input、_unhandled_input)的适用场景;推荐用 Project Settings > Input Map 定义动作而非检查原始键码,动作命名如 move_left、attack、interact,避免 press_a 一类键名;区分事件驱动(离散动作如跳跃、攻击用 _unhandled_input)与轮询(移动、冲刺在 _physics_process 用 Input.get_vector、Input.is_action_pressed);介绍 Input 方法(is_action_pressed、just_pressed、get_action_strength、get_axis、get_vector)与输入缓冲;鼠标部分涉及 relative 与 MOUSE_MODE_CAPTURED、自定义光标;手柄部分涉及 get_connected_joypads、joy_connection_changed、死区、震动与运动传感器(部分为 4.7+);触摸部分涉及 InputEventScreenTouch/Drag、event.index 多指与 VirtualJoystick(4.7+);重绑定三步:捕获按键、action_erase_events + action_add_event、用 ConfigFile 持久化到 user://。
使用方式:按文档在编辑器中配置动作,代码示例见各引用文件(如 references/action-rebinding.md、mouse.md、gamepad.md、touch.md、input-buffering.md、event-propagation.md)。
适用场景与限制:适用桌面、主机风格手柄、移动触摸及 XR 输入基础。注意 _physics_process 中 is_action_just_pressed 可能漏输入,应改在 _unhandled_input 捕获;_input 会先于 UI 消费,游戏动作宜用 _unhandled_input;暂停节点需 PROCESS_MODE_ALWAYS;Godot 4.7 起键鼠设备 ID 改为常量,event.device == 0 判断会失效。其余具体版本差异与实现细节以官方资料为准,暂未提供更多内容。