Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of all currently pressed keys in Javascript [duplicate]

Tags:

javascript

In Javascript, I want to write a function that returns a list of all keys that are currently pressed (so that I can allow the user to create custom keyboard shortcuts.) Is there any way to obtain a list of all currently pressed keys in Javascript?

like image 962
Anderson Green Avatar asked Nov 30 '12 06:11

Anderson Green


People also ask

How do you see which key is being pressed JavaScript?

Attach an event to the input box. like onkeypress event. Call a function when that event happens and pass the event parameter in it. In the called function, identify the key pressed.

How do I know which key is pressed on my keyboard?

The Windows on-screen keyboard is a program included in Windows that shows an on-screen keyboard to test modifier keys and other special keys. For example, when pressing the Alt , Ctrl , or Shift key, the On-Screen Keyboard highlights the keys as pressed.

What is the difference between keypress and Keydown?

The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup , but as 97 by keypress .

What is keypress event in JavaScript?

The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .


2 Answers

  • whenever a key is pressed a keydown event will be sent
  • whenever a key is released a keyup event will be triggered

So you just need to save the keys in an array and check whether your combination is true.

Example

var keys = [];
window.addEventListener("keydown",
    function(e){
        keys[e.keyCode] = true;
        checkCombinations(e);
    },
false);

window.addEventListener('keyup',
    function(e){
        keys[e.keyCode] = false;
    },
false);

function checkCombinations(e){
    if(keys["a".charCodeAt(0)] && e.ctrlKey){
        alert("You're not allowed to mark all content!");
        e.preventDefault();
    }
}

Note that you should use e.key instead of e.keyCode whenever possible (in this case var key = {}, since e.key is a string).

like image 125
Zeta Avatar answered Oct 15 '22 02:10

Zeta


Improving on the previous answer, I have written a demo that prints the list of pressed keys on keydown and keyup.

Here it is on jsfiddle.

var keys = [];
document.body.innerHTML = "Keys currently pressed: "
window.addEventListener("keydown",
    function(e){
        keys[e.keyCode] = e.keyCode;
        var keysArray = getNumberArray(keys);
        document.body.innerHTML = "Keys currently pressed:" + keysArray;
        if(keysArray.toString() == "17,65"){
            document.body.innerHTML += " Select all!"
        }
    },
false);

window.addEventListener('keyup',
    function(e){
        keys[e.keyCode] = false;
        document.body.innerHTML = "Keys currently pressed: " + getNumberArray(keys);
    },
false);

function getNumberArray(arr){
    var newArr = new Array();
    for(var i = 0; i < arr.length; i++){
        if(typeof arr[i] == "number"){
            newArr[newArr.length] = arr[i];
        }
    }
    return newArr;
}
​
like image 9
Anderson Green Avatar answered Oct 15 '22 01:10

Anderson Green