How to use Shortcut?

Godot Version

4.2

Question

I can’t understand ShortCut with this documentation. I don’t know what to do or how to even set it up. This is what I have so far.

func _ready():
	DisplayServer.window_set_title(app_name + ' - ' + current_file)
	
	for i in ['New File', 'Open File', 'Save File', 'Save as File', 'Quit']:
		$FileMenu.get_popup().add_item(i)
	
	$FileMenu.get_popup().set_item_shortcut(4, set_shortcut(KEY_Q), true)
	
	for i in ['About', "How to Use"]:
		$HelpMenu.get_popup().add_item(i)
	
	$FileMenu.get_popup().id_pressed.connect(_on_item_pressed)
	$HelpMenu.get_popup().id_pressed.connect(_on_item_help_pressed)

func set_shortcut(key):
	var shortcut = Shortcut.new()
	var inputevent = InputEventKey.new()
	inputevent.keycode = key
	inputevent.ctrl_pressed = true
	shortcut.events # what
	return shortcut

I have never used that before.

However, when I find the documentation lacking, I go to github and search for the term and filter down to gdscript and see how other projecta use it.

https://github.com/search?q=Shortcut.new()+language%3AGDScript+&type=code

Hopefully someone will chime in with better info, but this strategy has worked a lot for me.

1 Like