Trouble with checking for backspace character with nodejs raw stdin

徘徊边缘 提交于 2020-08-09 03:56:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!