Buttonclick event - ctrlKey always false

I would like to check wether the control key on the keyboard was pressed while a button was clicked.

async function main(eventDetails){

document.getElementById("XYZ").addEventListener('click', (event) => { main(event); });

This works fine (also with “eventDetails.altKey”). If i exchange it to “ctrlKey”, however, I always get false. I mean, as it generally works, is it a bug? Or am I (again) missing something? If i just console log the event itself, it says the object has a ctrlKey attribute…

I found this, which says its just not working, but nobody else explains why in the thread:

In the end, the solution just uses “event.ctrlKey”…

Sometimes it is different for MacOS. They have their own key.

Try this

    console.log(eventDetails.metaKey || eventDetails.ctrlKey);

That did the trick, thanks, Wanokuni!

So basically on Windows the control key isnt “ctrlKey”, but the “metaKey”, which I think is very wierd. Shouldnt the “metaKey” rather be the Windows key, which opens the start menu, and “ctrlKey” be the control key on both platforms? (Just a rhetoric question, no need for an answer…)

1 Like

No, on Windows, the Ctrl key is referred to as the CtrlKey, while on macOS, it is called the Command key (metaKey), which corresponds to the Ctrl key on Windows.

the ctrlKey part of ctrlKey || metaKey is superfluous. on macOS, this property will be true when the ctrl (^) key is pressed, which is undesirable. on Windows, this property will always be false; indeed confusingly metaKey corresponds to Ctrl instead. so if you only want to check for on macOS and Ctrl on Windows, you should only check for metaKey.

1 Like