如何捕获node.js中的箭头键

什么是所有四个箭头键的utf8代码(上下左右)?

我正在学习node.js,我试图检测这些键是否被按下。

这是我所做的,但没有一个捕获箭头键…我是一个完整的node.js新手,所以我可能在这里做一些愚蠢的事情。

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 }); 

谢谢。

你可以使用按键包。 尝试在页面上给出的例子。

 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(); 

您可以按顺序获取方向键的UTF-8值。

 > 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' } 

这应该可以解决你的问题:

 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 }); 

这也可能是你感兴趣的:

 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; } 

我在这里find了这个函数: http : //buildingonmud.blogspot.de/2009/06/convert-string-to-unicode-in-javascript.html

假设你的意思是关键代码:

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

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

要在JavaScript中捕获它们:

 if (key.which == 39) { // go right! } 

Moezalez和user568109的答案为我工作:

 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; } }); 

我自己是一个节点新手,但AFAIK这不能这样工作。 节点本身没有“前端”或用户界面API,这将允许您捕获用户input。 而箭头键等function键不会发送到“标准input”,只是产生字符的键。

您可以通过命令行读取命令行参数或将数据发送到“stdin”,例如:

  echo "example" | node yourscript.js 

要么

  node yourscript.js < test.txt 

如果yourscript.js

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

那么example (或者test.txt的内容)将被回显到节点的控制台,但是两者都不允许你直接的用户交互。

对于使用“标准input”的更复杂的交互,请参阅http://docs.nodejitsu.com/articles/command-line/how-to-prompt-for-command-line-input ,但正如我所说的方向键不发送到“标准input”,所以你不能捕获它们。

通常情况下,您将节点用于Web应用程序,因此您可以将节点脚本编写为Web服务器,并使用浏览器作为前端。

或者你寻找提供前端的库/模块。 我所知道的是(我也没用过):

  • node-gui一个GTK +绑定
  • node-webkit是一个基于Chromium的替代运行时,它允许你在HTML / CSS / JavaScript中编写前端。

我不知道是否有一个允许用户通过控制台交互的库。