Search code examples
javascriptdom-events

Javascript event e.which?


What is the functionality of Javascript event e.which? Please brief with example.


Solution

  • Deprecation notice (as of September 2020)

    KeyboardEvent.which has been deprecated. Please look for alternatives, such as KeyboardEvent.key. Read the full API here.

    e.which is not an event, which is a property of the event object, which most people label as e in their event handlers. It contains the key code of the key which was pressed to trigger the event (eg: keydown, keyup).

    document.onkeypress = function(myEvent) { // doesn't have to be "e"
        console.log(myEvent.which);
    };
    

    With that code, the console will print out the code of any key you press on the keyboard.