ActionRemapButton.gd 790 B

123456789101112131415161718192021222324252627282930313233343536
  1. extends Button
  2. export(String) var action = "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(button_pressed):
  8. set_process_unhandled_key_input(button_pressed)
  9. if 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. 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.get_action_list(action)[0].as_text()
  25. text = "%s Key" % current_key