Question:

How can I make my Javascript listen for specific key presses from a text field in my form?

by  |  earlier

0 LIKES UnLike

'onkeypress', 'onkeydown', and 'onkeyup' only detect when a key is pressed, right? I'm trying to remember the way to find the specific key (including Ctrl, Shift, etc.).

How do I do this again?

 Tags:

   Report

1 ANSWERS


  1. event.keyCode

    function showKeyCode(e)

    {

    alert("keyCode for the key pressed: " + e.keyCode + "\n");

    }

    In a keypress event, the Unicode value of the key pressed is stored in either the keyCode or charCode property, never both. If the key pressed generates a character (e.g. 'a'), charCode is set to the code of that character, respecting the letter case. (i.e. charCode takes into account whether the shift key is held down). Otherwise, the code of the pressed key is stored in keyCode.

    keyCode is always set in the keydown and keyup events. In these cases, charCode is never set.

    To get the code of the key regardless of whether it was stored in keyCode or charCode, query the which property.

    Try this link to determine the code for the exact key your looking for...

    http://www.javascriptkit.com/javatutors/...

    For a list of all codes that are possible to be returned:

    http://www.expandinghead.net/keycode.htm...

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.