FlattedFifth
FlattedFifth

Reputation: 15

How to get keyboard shortcut showing in menu without causing double pasting?

When I assign the standard keyboard shortcut for paste in my menu, using the keyboard shortcut actually pastes twice because the system automatically performs a paste when that shortcut is used and then it activates the menu item, thereby pasting again. If I do not specify the keyboard shortcut, then the keyboard shortcut still works because it's a system default shortcut but the menu item no longer shows the shortcut in the menu.

For example, if I write this:

            menu("Edit"){
                item("Copy") {
                    action {
                        when (scene.focusOwner){
                            myTextField -> myTextField.copy()
                            myTextArea -> myTextArea.copy()
                        }
                    }
                }
                item("Paste") {
                    action{
                        when (scene.focusOwner){
                            myTextField -> myTextField.paste()
                            myTextArea -> myTextArea.paste()
                        }
                    }
                }
            }

Then both the menu and the Ctrl/Command-V shortcut work fine because the shortcut is predefined in the system but it doesn't show in the menu, as you see below:

screenshot of edit menu, keyboard shortcut notation not visible in menu

If, however, I change the code for the Paste menu item to this:

item("Paste", "Shortcut+V"){.....

then the menu looks like it should:

screenshot of edit menu with shortcut notation displayed

BUT, using the Ctrl/Command-V key combo causes a DOUBLE paste, because first the built-in system shortcut happens, causing a paste to occur, and then the key combo activates the action{} code block in the menu code causing another paste to occur.

How do I either get the action{} code block to only happen when the menu item is clicked by a mouse (while still keeping the shortcut notation visible in the menu item), or disable the system shortcut (preferably the former)?

Any help is appreciated.

(IntelliJ Idea, MacOS X Ventura, JDK-FX 11)

Upvotes: 0

Views: 105

Answers (1)

FlattedFifth
FlattedFifth

Reputation: 15

Turns out that the reason for the double pasting is that TextInputControl, the superclass of TextArea() and TextField(), was listening for the standard shortcuts. So to prevent those shortcuts from firing twice if assigned to menu items I added the following to the parent view of all my nodes that are subclasses of TextInputControl:

addEventFilter(KeyEvent.KEY_PRESSED) {
            if (it.isShortcutDown){
                it.consume()
            }
        }

Then, in the view that contains my menu bar, my menu items pass the node that has focus and the shortcut used to a method that then performs the task. For example, my paste menu item is now:

item("Paste", "Shortcut+V") {
action{
    menuBarAction(scene.focusOwner, this.accelerator)
}}

Which passes to

private fun menuBarAction(node: Node, keyCombination: KeyCombination){
if (node is TextInputControl) {
        when (keyCombination.name) {
            "Shortcut+Z" -> node.undo()
            "Shortcut+A" -> node.selectAll()
            "Shortcut+X" -> node.cut()
            "Shortcut+C" -> node.copy()
            "Shortcut+V" -> node.paste()
        }
}

}

Upvotes: 0

Related Questions