Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Check if CTRL button was pressed

Tags:

javascript

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?

like image 588
user324589457 Avatar asked Jul 24 '11 10:07

user324589457


People also ask

How do I find Ctrl?

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.

What is event ctrlKey?

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.


1 Answers

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>
like image 70
Charles Ma Avatar answered Sep 23 '22 10:09

Charles Ma