Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take keyboard input in JavaScript?

Tags:

javascript

I want to take keyboard input in JavaScript, where arrow keys, when pressed, will result in the change in shape of a particular shape. How do I take the input of any of the keys in JavaScript?

like image 487
Hick Avatar asked Dec 11 '10 11:12

Hick


People also ask

What is keyboard input in JavaScript?

There are three different keyboard events in JavaScript: keydown : Keydown happens when the key is pressed down, and auto repeats if the key is pressed down for long. keypress : This event is fired when an alphabetic, numeric, or punctuation key is pressed down. keyup : Keyup happens when the key is released.

How do you input in JavaScript?

In JavaScript, we use the prompt() function to ask the user for input. As a parameter, we input the text we want to display to the user. Once the user presses “ok,” the input value is returned. We typically store user input in a variable so that we can use the information in our program.

What is keyboard event in JavaScript?

KeyboardEvent objects describe a user interaction with the keyboard; each event describes a single interaction between the user and a key (or combination of a key with modifier keys) on the keyboard. The event type ( keydown , keypress , or keyup ) identifies what kind of keyboard activity occurred.

What is e KeyCode === 13?

key 13 keycode is for ENTER key.


3 Answers

You can do this by registering an event handler on the document or any element you want to observe keystrokes on and examine the key related properties of the event object.

Example that works in FF and Webkit-based browsers:

document.addEventListener('keydown', function(event) {     if(event.keyCode == 37) {         alert('Left was pressed');     }     else if(event.keyCode == 39) {         alert('Right was pressed');     } }); 

DEMO

like image 183
Felix Kling Avatar answered Sep 27 '22 00:09

Felix Kling


You should register an event handler on the window or any element that you want to observe keystrokes on, and use the standard key values instead of keyCode. This modified code from MDN will respond to keydown when the left, right, up, or down arrow keys are pressed:

window.addEventListener("keydown", function (event) {   if (event.defaultPrevented) {     return; // Do nothing if the event was already processed   }    switch (event.key) {     case "ArrowDown":       // code for "down arrow" key press.       break;     case "ArrowUp":       // code for "up arrow" key press.       break;     case "ArrowLeft":       // code for "left arrow" key press.       break;     case "ArrowRight":       // code for "right arrow" key press.       break;     default:       return; // Quit when this doesn't handle the key event.   }    // Cancel the default action to avoid it being handled twice   event.preventDefault(); }, true); // the last option dispatches the event to the listener first, // then dispatches event to window 
like image 25
exd5cxd4 Avatar answered Sep 25 '22 00:09

exd5cxd4


If you are doing this in a browser, you can capture keyboard events.

  • keydown
  • keypress
  • keyup

Can all be listened to on HTML nodes in most browsers.

Webkit also supports...

  • textInput

See for more details .. http://unixpapa.com/js/key.html

like image 20
ocodo Avatar answered Sep 26 '22 00:09

ocodo