Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture the arrow keys in node.js

Tags:

What is the utf8 code for all four arrow keys (up down left right)?

I am learning node.js and I am trying to detect whenever these keys are being pressed.

Here is what I did, but none of it capturing the arrow keys... I am a complete newbie to node.js so I might be doing something hilariously stupid here.

var stdin = process.stdin; stdin.setRawMode(true); stdin.resume(); stdin.setEncoding('utf8');  stdin.on('data', function(key){     if (key === '39') {         process.stdout.write('right');      }     if (key === 39) {         process.stdout.write('right');      }     if (key == '39') {             process.stdout.write('right');      }     if (key == 39) {         process.stdout.write('right');      }      if (key == '\u0003') { process.exit(); }    // ctrl-c }); 

Thanks.

like image 973
Rosdi Kasim Avatar asked Jul 04 '13 12:07

Rosdi Kasim


People also ask

How do you code arrow keys in JavaScript?

To detect the arrow key when it is pressed, use onkeydown in JavaScript. The button has key code. As you know the left arrow key has the code 37. The up arrow key has the code 38 and right has the 39 and down has 40.

How do you tell which arrow key is which?

Alternatively referred to as cursor keys, direction keys, and navigation keys, the arrow keys are usually located between the standard section and the numeric pad on computer keyboards. It is made up of four keys: the left arrow (back arrow), up arrow, down arrow, and the right arrow (forward arrow).

Where are the arrow keys located?

The numeric keypad (1 2 3 etc) on the right of the keyboard. Cursor control keys (the arrows) on the right of the keyboard. A number of specially defined keys (Enter, Shift, Windows key etc)


2 Answers

You can use keypress package. Trying the example given on the page.

var keypress = require('keypress');  // make `process.stdin` begin emitting "keypress" events keypress(process.stdin);  // listen for the "keypress" event process.stdin.on('keypress', function (ch, key) {   console.log('got "keypress"', key);   if (key && key.ctrl && key.name == 'c') {     process.stdin.pause();   } });  process.stdin.setRawMode(true); process.stdin.resume(); 

You get the UTF-8 values of arrow keys at sequence.

> process.stdin.resume();got "keypress" { name: 'up',   ctrl: false,   meta: false,   shift: false,   sequence: '\u001b[A',   code: '[A' } > got "keypress" { name: 'down',   ctrl: false,   meta: false,   shift: false,   sequence: '\u001b[B',   code: '[B' } got "keypress" { name: 'right',   ctrl: false,   meta: false,   shift: false,   sequence: '\u001b[C',   code: '[C' } got "keypress" { name: 'left',   ctrl: false,   meta: false,   shift: false,   sequence: '\u001b[D',   code: '[D' } 
like image 157
user568109 Avatar answered Oct 26 '22 20:10

user568109


This should solve your problem:

var stdin = process.stdin; stdin.setRawMode(true); stdin.resume(); stdin.setEncoding('utf8');  stdin.on('data', function(key){     if (key == '\u001B\u005B\u0041') {         process.stdout.write('up');      }     if (key == '\u001B\u005B\u0043') {         process.stdout.write('right');      }     if (key == '\u001B\u005B\u0042') {         process.stdout.write('down');      }     if (key == '\u001B\u005B\u0044') {         process.stdout.write('left');      }      if (key == '\u0003') { process.exit(); }    // ctrl-c }); 

This could also be of interest for you:

stdin.on('data', function(key){     console.log(toUnicode(key)); //Gives you the unicode of the pressed key     if (key == '\u0003') { process.exit(); }    // ctrl-c });  function toUnicode(theString) {   var unicodeString = '';   for (var i=0; i < theString.length; i++) {     var theUnicode = theString.charCodeAt(i).toString(16).toUpperCase();     while (theUnicode.length < 4) {       theUnicode = '0' + theUnicode;     }     theUnicode = '\\u' + theUnicode;     unicodeString += theUnicode;   }   return unicodeString; } 

I found the function here: http://buildingonmud.blogspot.de/2009/06/convert-string-to-unicode-in-javascript.html

like image 35
MoeSattler Avatar answered Oct 26 '22 20:10

MoeSattler