在 node.js 中使用按键启动操作
Use keypress to initiate action in node.js
我正在使用 node.js v4.5
我写了下面的函数来延迟发送重复消息。
function send_messages() {
Promise.resolve()
.then(() => send_msg() )
.then(() => Delay(1000) )
.then(() => send_msg() )
.then(() => Delay(1000))
.then(() => send_msg() )
;
}
function Delay(duration) {
return new Promise((resolve) => {
setTimeout(() => resolve(), duration);
});
}
我不想延迟,而是想使用按键来激活消息发送。类似于下面的函数。
function send_messages_keystroke() {
Promise.resolve()
.then(() => send_msg() )
.then(() => keyPress('ctrl-b') ) //Run subsequent line of code send_msg() if keystroke ctrl-b is pressed
.then(() => send_msg() )
.then(() => keyPress('ctrl-b') )
.then(() => send_msg() )
;
}
您可以将 process.stdin
置于原始模式以访问单个击键。
这是一个独立的例子:
function send_msg(msg) {
console.log('Message:', msg);
}
// To map the `value` parameter to the required keystroke, see:
// http://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ctrl.htm
function keyPress(value) {
return new Promise((resolve, reject) => {
process.stdin.setRawMode(true);
process.stdin.once('data', keystroke => {
process.stdin.setRawMode(false);
if (keystroke[0] === value) return resolve();
return reject(Error('invalid keystroke'));
});
})
}
Promise.resolve()
.then(() => send_msg('1'))
.then(() => keyPress(2))
.then(() => send_msg('2'))
.then(() => keyPress(2))
.then(() => send_msg('done'))
.catch(e => console.error('Error', e))
它会拒绝任何不是 Ctrl-B 的击键,但如果您不想要这种行为(并且只想等待首先 Ctrl-B,例如)。
传递给keyPress
的值是key的十进制ASCII值:Ctrl-A为1,Ctrl-B为2,a为97等
编辑:正如@mh-cbon 在评论中所建议的,更好的解决方案可能是使用 keypress
模块。
试试这个。如上所述,使用按键使它变得非常简单。此处代码中的 key
对象告诉您 ctrl
或 shift
是否被按下,以及按下的字符。不幸的是,keypress
似乎无法处理数字或特殊字符。
var keypress = require('keypress');
keypress(process.stdin);
process.stdin.on('keypress', function (ch, key) {
console.log("here's the key object", key);
shouldExit(key);
if (key) {
sendMessage(key);
}
});
function shouldExit(key) {
if (key && key.ctrl && key.name == 'c') {
process.stdin.pause();
}
}
function sendMessage(key) {
switch(key.name) {
case 'a';
console.log('you typed a'); break;
case 'b';
console.log('you typed b'); break;
default:
console.log('bob is cool');
}
}
当然,在此处的 sendMessage()
函数中,您可以轻松地将日志语句替换为其他更复杂的内容、进行一些异步调用、调用其他函数等。这里的 process.stdin.pause()
导致程序在 ctrl-c
退出,否则程序只会保持 运行,阻塞你的中断信号,你必须通过命令行手动终止进程.
我正在使用 node.js v4.5
我写了下面的函数来延迟发送重复消息。
function send_messages() {
Promise.resolve()
.then(() => send_msg() )
.then(() => Delay(1000) )
.then(() => send_msg() )
.then(() => Delay(1000))
.then(() => send_msg() )
;
}
function Delay(duration) {
return new Promise((resolve) => {
setTimeout(() => resolve(), duration);
});
}
我不想延迟,而是想使用按键来激活消息发送。类似于下面的函数。
function send_messages_keystroke() {
Promise.resolve()
.then(() => send_msg() )
.then(() => keyPress('ctrl-b') ) //Run subsequent line of code send_msg() if keystroke ctrl-b is pressed
.then(() => send_msg() )
.then(() => keyPress('ctrl-b') )
.then(() => send_msg() )
;
}
您可以将 process.stdin
置于原始模式以访问单个击键。
这是一个独立的例子:
function send_msg(msg) {
console.log('Message:', msg);
}
// To map the `value` parameter to the required keystroke, see:
// http://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ctrl.htm
function keyPress(value) {
return new Promise((resolve, reject) => {
process.stdin.setRawMode(true);
process.stdin.once('data', keystroke => {
process.stdin.setRawMode(false);
if (keystroke[0] === value) return resolve();
return reject(Error('invalid keystroke'));
});
})
}
Promise.resolve()
.then(() => send_msg('1'))
.then(() => keyPress(2))
.then(() => send_msg('2'))
.then(() => keyPress(2))
.then(() => send_msg('done'))
.catch(e => console.error('Error', e))
它会拒绝任何不是 Ctrl-B 的击键,但如果您不想要这种行为(并且只想等待首先 Ctrl-B,例如)。
传递给keyPress
的值是key的十进制ASCII值:Ctrl-A为1,Ctrl-B为2,a为97等
编辑:正如@mh-cbon 在评论中所建议的,更好的解决方案可能是使用 keypress
模块。
试试这个。如上所述,使用按键使它变得非常简单。此处代码中的 key
对象告诉您 ctrl
或 shift
是否被按下,以及按下的字符。不幸的是,keypress
似乎无法处理数字或特殊字符。
var keypress = require('keypress');
keypress(process.stdin);
process.stdin.on('keypress', function (ch, key) {
console.log("here's the key object", key);
shouldExit(key);
if (key) {
sendMessage(key);
}
});
function shouldExit(key) {
if (key && key.ctrl && key.name == 'c') {
process.stdin.pause();
}
}
function sendMessage(key) {
switch(key.name) {
case 'a';
console.log('you typed a'); break;
case 'b';
console.log('you typed b'); break;
default:
console.log('bob is cool');
}
}
当然,在此处的 sendMessage()
函数中,您可以轻松地将日志语句替换为其他更复杂的内容、进行一些异步调用、调用其他函数等。这里的 process.stdin.pause()
导致程序在 ctrl-c
退出,否则程序只会保持 运行,阻塞你的中断信号,你必须通过命令行手动终止进程.