ActionRemapButton.gd 809 B

1234567891011121314151617181920212223242526272829303132333435
  1. extends Button
  2. @export var action: String = "ui_up"
  3. func _ready():
  4. assert(InputMap.has_action(action))
  5. set_process_unhandled_key_input(false)
  6. display_current_key()
  7. func _toggled(is_button_pressed):
  8. set_process_unhandled_key_input(is_button_pressed)
  9. if is_button_pressed:
  10. text = "... Key"
  11. release_focus()
  12. else:
  13. display_current_key()
  14. func _unhandled_key_input(event):
  15. # Note that you can use the _input callback instead, especially if
  16. # you want to work with gamepads.
  17. remap_action_to(event)
  18. button_pressed = false
  19. func remap_action_to(event):
  20. InputMap.action_erase_events(action)
  21. InputMap.action_add_event(action, event)
  22. text = "%s Key" % event.as_text()
  23. func display_current_key():
  24. var current_key = InputMap.action_get_events(action)[0].as_text()
  25. text = "%s Key" % current_key