Godot Version
4.5.1.stable
Question
Im trying to attach custom items and shortcuts to those items to the popup menu of a CodeEdit node. Im not really sure what Im doing wrong because it seems like everything is hooked up correctly. I first tried using accelerators but turns out thats not even supported on another other platform except MacOS (check this). So I tried with Shortcut but that doesnt seem to work either. What am I doing wrong?
extends CodeEdit
## Extra popup menu item ids.
enum CustomMenuItems {
MENU_MOVE_LINE_UP = MENU_MAX + 1,
MENU_MOVE_LINE_DOWN = MENU_MOVE_LINE_UP + 1,
}
func _ready() -> void:
_set_up_popup_menu_items()
grab_focus.call_deferred()
func _set_up_popup_menu_items() -> void:
var menu: PopupMenu = get_menu()
menu.remove_item(menu.get_item_index(MENU_EMOJI_AND_SYMBOL))
menu.remove_item(menu.get_item_index(MENU_EMOJI_AND_SYMBOL) + 1) # Remove separator.
menu.remove_item(menu.get_item_index(MENU_CLEAR))
menu.item_count = menu.get_item_index(MENU_REDO) + 1 # Remove all items after "Redo".
menu.add_separator("", menu.get_item_index(MENU_MAX) + 1)
_add_popup_menu_item(
menu,
KEY_UP,
"Move Line Up",
CustomMenuItems.MENU_MOVE_LINE_UP,
true
)
menu.id_pressed.connect(_on_popup_menu_item_pressed)
func _add_popup_menu_item(
menu: PopupMenu,
keycode: Key,
label: String,
id: int = -1,
alt: bool = false,
ctrl: bool = false,
meta: bool = false,
shift: bool = false,
command_or_control_autoremap: bool = false
) -> void:
var shortcut := Shortcut.new()
var input_key := InputEventKey.new()
input_key.keycode = keycode
input_key.alt_pressed = alt
input_key.ctrl_pressed = ctrl
input_key.meta_pressed = meta
input_key.shift_pressed = shift
input_key.command_or_control_autoremap = command_or_control_autoremap
shortcut.events = [input_key]
menu.add_item(label, id)
menu.set_item_shortcut(menu.get_item_index(id), shortcut, true)
func _on_popup_menu_item_pressed(id: int) -> void:
print(id)
match id:
MENU_COPY, MENU_PASTE:
print("TEST")
CustomMenuItems.MENU_MOVE_LINE_UP:
print("Moving up!")