Godot Engine 3.2Alpha2
获取键盘按下事件直接使用scancode
extends Node
func _ready()
pass
func _input(e):
if e is InputEventKey and e.pressed and not e.echo:
#筛选键盘输入事件 and 键盘按下事件 and 非一直按下状态
match e.scancode:
KEY_1:
print("1")
KEY_2:
print("2")
KEY_3:
print("3")
_:
print("_")
使用预定义的InputMap
extends Node
func _ready()
pass
func _input(e):
if e is InputEventKey and e.pressed and not e.echo:
#下面使用e.is_action_pressed也是可以的,由于上面已经筛选了按下事件,所以可以直接用e.is_action
if e.is_action("ui_up"):
print("1")
elif e.is_action("ui_down"):
print("2")
elif e.is_action("ui_left"):
print("3")
elif e.is_action("ui_right"):
print("4")
else:
print("5")