-
Hi Everyone, I want to select a checkbox while pressing the command/ctrl key. In my onChange event of the checkbox, I should know if the command/ctrl key was pressed or not. I tried reading the "window.event" variable but couldn't find any information related to the pressed command/ctrl key. Any Suggestions? Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @shivamdz the So, to check whether CTRL or COMMAND is pressed (probably depending on OS), you need to register a global listener for it, then you can just update a React state depending on if one of the keys is pressed or not and check for that in your https://codesandbox.io/s/command-ctrl-um916y?file=/src/App.js |
Beta Was this translation helpful? Give feedback.
Hi @shivamdz
the
change
event is aCustomEvent
that doesn't hold thectrlKey
ormetaKey
information as it only inherits the properties of theEvent
type. This is similar to the nativechange
event as it is as well only anEvent
.So, to check whether CTRL or COMMAND is pressed (probably depending on OS), you need to register a global listener for it, then you can just update a React state depending on if one of the keys is pressed or not and check for that in your
onChange
callback.https://codesandbox.io/s/command-ctrl-um916y?file=/src/App.js