问题
I am having trouble figuring out how to check for when a user presses the backspace/delete key when taking raw input. Here is the code example
process.stdin.setRawMode( true );
process.stdin.setEncoding( 'utf8' );
process.stdin.resume();
process.stdin.on( 'data', function( key ){
/**
* Trying to check if the delete key is entered
*/
if( key === '\u0008' ){
/**
* code
*/
}
process.stdout.write( key );
});
The reason I'm am trying to do this is because when i set stdin to rawMode then the hitting the backspace key just moves the cursor (as if the space bar is pressed.). I found how to emulate deleting by using stdout.moveCursor, and stdout.clearLine, but can't figure out how to check that the delete key was pressed.
thanks
回答1:
You can do this :
if( key.charCodeAt(0) === 127 ){
来源:https://stackoverflow.com/questions/24237468/trouble-with-checking-for-backspace-character-with-nodejs-raw-stdin