How to capture the arrow keys in node.js

2020-02-23 08:43发布

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.

标签: node.js utf-8
5条回答
太酷不给撩
2楼-- · 2020-02-23 08:44

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

查看更多
Root(大扎)
3楼-- · 2020-02-23 08:51

I'm a node newbie myself, but AFAIK this can't work like this. Node alone has no "front end" or user interface API which would allow you to capture user input. And functional keys like arrow keys are not sent to "stdin", just keys that produce characters.

You could read command line arguments or send data to "stdin" via the command line, e.g.:

 echo "example" | node yourscript.js

or

 node yourscript.js < test.txt

If yourscript.js is

process.stdin.resume();

process.stdin.on('data', function(chunk) {
  console.log('chunk: ' + chunk);
});

then example (or the content of test.txt) will be echo'd to node's console, but neither will allow you any direct user interaction.

For a more complex interaction using "stdin" see http://docs.nodejitsu.com/articles/command-line/how-to-prompt-for-command-line-input , however as I said arrow keys aren't sent to "stdin", so you can't capture them.

Normally you use node for web applications, so you could write your node script as a web server and use a browser as the front end.

Or you look for libraries/modules that provide a front end. The ones I know of are (I haven't used either):

  • node-gui a GTK+ binding
  • node-webkit an alternative runtime based on Chromium which allows you to write front ends in HTML/CSS/JavaScript.

I don't know if there is a library that allows user interaction via the console.

查看更多
啃猪蹄的小仙女
4楼-- · 2020-02-23 08:57

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' }
查看更多
乱世女痞
5楼-- · 2020-02-23 09:04

Assuming you mean the key codes:

Up: 38
Down: 40
Left: 37
Right: 39

http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

To capture them in JavaScript:

if (key.which == 39) {
    // go right!
}
查看更多
Anthone
6楼-- · 2020-02-23 09:05

A mix of Moezalez and user568109 answers worked for me:

var stdin = process.stdin;
// Don't set the rawMode to continue listening for other full length options.
stdin.resume();
stdin.setEncoding('utf8');

stdin.on('data', function(input) {
    // Take out the new line character at the end
    var option = input.substr(0, input.length - 1);
    switch (option) {
        case '\u001b[A':
            // Up
            break;
        case '\u001b[B':
            // Down
            break;
        case '\u001b[C':
            // Right
            break;
        case '\u001b[D':
            // Left
            break;
        case 'something_else':
            // Perform what something_else does 
            break;
    }
});
查看更多
登录 后发表回答