ActionRemapButton.gd 971 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. # We first change the event in this game instance.
  21. InputMap.action_erase_events(action)
  22. InputMap.action_add_event(action, event)
  23. # And then save it to the keymaps file
  24. KeyPersistence.keymaps[action] = event
  25. KeyPersistence.save_keymap()
  26. text = "%s Key" % event.as_text()
  27. func display_current_key():
  28. var current_key = InputMap.action_get_events(action)[0].as_text()
  29. text = "%s Key" % current_key