所有四个箭头键(右上角)的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
});
感谢。
答案 0 :(得分:19)
您可以使用keypress包。试试页面上给出的例子。
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' }
答案 1 :(得分:12)
这可以解决您的问题:
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;
}
我在这里找到了这个功能: http://buildingonmud.blogspot.de/2009/06/convert-string-to-unicode-in-javascript.html
答案 2 :(得分:3)
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;
}
});
答案 3 :(得分:0)
假设您的意思是密码:
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!
}
答案 4 :(得分:0)
我自己是一个节点新手,但AFAIK这个不能像这样工作。单独的节点没有“前端”或用户界面API,可以让您捕获用户输入。并且箭头键之类的功能键不会发送到“stdin”,只会生成产生字符的键。
您可以通过命令行读取command line arguments或将数据发送到“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
的内容)将回显到节点的控制台,但两者都不允许您进行任何直接的用户交互。
对于使用“stdin”进行更复杂的交互,请参阅http://docs.nodejitsu.com/articles/command-line/how-to-prompt-for-command-line-input,但正如我所说箭头键未发送到“stdin”,因此您无法捕获它们。
通常,您将节点用于Web应用程序,因此您可以将节点脚本编写为Web服务器并使用浏览器作为前端。
或者您寻找提供前端的库/模块。我所知道的是(我也没用过):
我不知道是否有一个允许用户通过控制台进行交互的库。