代码之家  ›  专栏  ›  技术社区  ›  user193464

Godot键盘事件

  •  9
  • user193464  · 技术社区  · 7 年前

    我正在学习Godot引擎和GDScript,我在互联网上搜索了键盘事件,但我不懂。戈多有没有这样的东西: on_key_down("keycode")

    6 回复  |  直到 7 年前
        1
  •  12
  •   Kai Hartmann    5 年前

    Godot 3.0及以上版本具有新的输入轮询功能,可在脚本中的任何位置使用:

    • Input.is_action_pressed(action) -检查是否正在按下操作
    • Input.is_action_just_pressed(action)
    • Input.is_action_just_released(action) -检查操作是否刚刚发布
        3
  •  5
  •   skrx    7 年前

    没有官方的OnKeyUp选项,但您可以使用 _input(event)

    func _input(event):
    
        if event.is_action_pressed("my_action"):
            # Your code here
        elif event.is_action_released("my_action):
            # Your code here
    

    操作在项目设置中设置>输入地图。

    _input Input.is_key_pressed() is_key_released()

    var was_pressed = 0
    
    func _fixed_process(delta):
        if !Input.is_key_pressed() && was_pressed = 1:
            # Your key is NOT pressed but WAS pressed 1 frame before
            # Code to be executed
    
        # The rest is just checking whether your key is just pressed
        if Input.is_key_pressed():
            was_pressed = 1
        elif !Input.is_key_pressed():
            was_pressed = 0
    

    这就是我一直在使用的。如果有更好的方法,请随时通知我 OnKeyUp 在戈多。

        4
  •  0
  •   Rashad Dent    3 年前

    如果您正在考虑使用输入或_输入(事件),那么进入项目设置并绑定键非常重要。

        5
  •  0
  •   Superpig 2000    3 年前

    if Input.is_action_just_pressed('Your action name'):
       print('Pressed!')
    

    where is project settings button

        6
  •  0
  •   Abel Valdez    3 年前

    注册密钥有多种方法:

    -is_key_pressed("Enter")
    -Input.is_action_pressed(*action*)
    

    推荐文章
    user193464  ·  Godot键盘事件
    7 年前