ActionRemapButton.gd 954 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. # 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.get_action_list(action)[0].as_text()
  29. text = "%s Key" % current_key