'The function "isControlDown()" for an event is CTRL+ALT instead of CTRL in Javafx

So i would like to use CTRL+Z to undo something in my project, So as I have seen online I have done it like this:

gridgame.setOnKeyPressed(event -> {
        if (event.getCode() == KeyCode.Z && event.isControlDown()){
               do stuff...
        } 
});

But the problem is when i do this i have to press CTRL+ALT+Z and not only CTRL+Z for the undo.



Solution 1:[1]

It sounds like there is some strange keyboard mapping on the machine you are using. Here are some other options to try:

It's usually preferable to use isShortcutDown() instead of isControlDown(), as this will give the expected behavior across multiple platforms (specifically, on a Mac, this will map to Command-Z instead of Control-Z). This may circumvent your keyboard issues too.

gridgame.setOnKeyPressed(event -> {
    if (event.getCode() == KeyCode.Z && event.isShortcutDown()){
           do stuff...
    } 
});

If that doesn't work, try creating a KeyCombination and testing via its match(...) method. Here you can explicitly instruct it to ignore the state of the Alt key:

KeyCombination ctrlZ = new KeyCodeCombinbation(
    KeyCode.Z, 
    KeyCombination.ALT_ANY, 
    KeyCombination.SHORTCUT_DOWN
);
// or KeyCombination ctrlZ = KeyCombination.keyCombination("Ignore Alt+Shortcut+Z");
gridgame.setOnKeyPressed(event -> {
    if (ctrlZ.match(event)){
           do stuff...
    } 
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James_D