EN
JavaScript - detect Ctrl key pressed
10
points
In this short article, we would like to show how to detect if Ctrl
key was pressed using JavaScript.
Quick solution:
// ONLINE-RUNNER:browser;
<script>
var handleMouseClick = function(e) {
if (e.ctrlKey) {
console.log('Ctrl key was pressed with mouse click.');
}
};
</script>
<input placeholder="Press Ctrl key and click with mouse." onclick="handleMouseClick(event)" />
Alternative solutions
Only Ctrl
key pressed detection:
// ONLINE-RUNNER:browser;
<script>
var handleKeyUp = function(e) {
if (e.key === 'Control') {
console.log('Ctrl key was pressed.');
}
};
</script>
<input placeholder="Click me and press Ctrl key ..." onkeyup="handleKeyUp(event)" />