I need to check if the CTRL button was pressed while I am clicking on a control on my html page using JavaScript.
How can I do this?
To detect the combination of keys with “Ctrl”, we use the ctrl property of the keydown event. It returns a “boolean” value to tell if “ctrl” is pressed or not when the key event got triggered. Return Value: true: When “ctrl” was pressed.
Definition and Usage The ctrlKey property returns a Boolean value that indicates whether or not the "CTRL" key was pressed when a mouse event was triggered. Note: This property is read-only.
Try looking in the event object.
e.g.
document.body.onclick = function (e) { if (e.ctrlKey) { alert("ctr key was pressed during the click"); } }
<p>Click me, and sometimes hold CTRL down!</p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With