Files
godot-demo-projects/gui/input_mapping/ActionRemapButton.gd
Hugo Locurcio 31d1c0c112 Remove old and unused project settings, update various demos for 4.2 (#1024)
- Move all demo projects that don't require Forward+/Mobile-only features
  to the Compatibility rendering method. This improves performance significantly
  on low-end devices and ensures visuals are identical to a web export
  of the demo.
- Set deadzone on all inputs to 0.2 for better gamepad usability.
- Remove reliance on `default_env.tres` to use built-in Environment
  resources in the main scene instead (which follows the preview environment
  workflow).
- Remove notices pointing to GDNative or VisualScript, since both were
  removed in 4.0.
- Various bug fixes and usability tweaks to 10+ demos.
2024-03-26 18:01:58 +01:00

40 lines
955 B
GDScript

extends Button
@export var action: String = "ui_up"
func _ready():
assert(InputMap.has_action(action))
set_process_unhandled_key_input(false)
display_current_key()
func _toggled(is_button_pressed):
set_process_unhandled_key_input(is_button_pressed)
if is_button_pressed:
text = "<press a key>"
release_focus()
else:
display_current_key()
func _unhandled_key_input(event):
# Note that you can use the _input callback instead, especially if
# you want to work with gamepads.
remap_action_to(event)
button_pressed = false
func remap_action_to(event):
# We first change the event in this game instance.
InputMap.action_erase_events(action)
InputMap.action_add_event(action, event)
# And then save it to the keymaps file
KeyPersistence.keymaps[action] = event
KeyPersistence.save_keymap()
text = event.as_text()
func display_current_key():
var current_key = InputMap.action_get_events(action)[0].as_text()
text = current_key