question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to catch the keypress

See original GitHub issue

Hi, first of all, compliments for the module.

I’m using term.on( ‘key’) to get the user keyboard input, but when I press something like ARROW_UP it produces on screen this: ^[[A and until I press enter the on(‘key’) callback is not fired… so, since I need to simulate the cursor moving when the user press arrow keys how can I do?

term.grabInput( );
term.on( 'key' , function( key , matches , data ) { 
switch ( key )
    {
        case 'UP' : term.up( 1 ) ; break ;
        case 'DOWN' : term.down( 1 ) ; break ;
        case 'LEFT' : term.left( 1 ) ; break ;
        case 'RIGHT' : term.right( 1 ) ; break ;
        case 'CTRL_C' : process.exit() ; break ;
        default:   
            // Echo anything else
            term.noFormat(
                Buffer.isBuffer( data.code ) ?
                    data.code :
                    String.fromCharCode( data.code )
            ) ;
            break ;
    }
});

The example code is from here: http://blog.soulserv.net and (for me) is not working as described… the arrow keys produces (on screen): ^[[A, … and until I press enter the key event is not fired

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cronvelcommented, Apr 13, 2017

Can you tell me which OS and terminal you are using? It looks like it does not support the raw mode. Windows?

0reactions
codekraft-studiocommented, Apr 14, 2017

@cronvel

If you can, then your windows terminal simply has bad default modes, but vanilla Node.js cannot set any TTY mode (beside raw mode), you need a native module for that: the solution can be to use the excellent termios module by @Gottox:

with the terminal provided by Bitvise I can use editors like nano and vim via ssh, eventually I will try to use termios if I really need, but for now i’m just “playing” with the code so I don’t want to complicate my life too much.

Have a look at this constructor, it allows you to configure your stdio.

I didn’t see the documentation in deep, sorry I should have checked before! Later today when I’ve time I’ll try to see if it works like you suggested many thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Capture key press without placing an input element on the ...
How to capture key press, e.g., Ctrl+Z, without placing an input element on the page in JavaScript? Seems that in IE, keypress and...
Read more >
Catching and Responding to Keyboard Events in JavaScript
The keypress event is mostly triggered when you press down any printable character, and it's fired after the key down event. In fact,...
Read more >
Element: keypress event - Web APIs | MDN
The keypress event is fired when a key that produces a character value is pressed down.
Read more >
onkeypress Event - W3Schools
Note: The onkeypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC) in all browsers. To detect only whether the...
Read more >
How to catch .keypress() on body - jQuery Forum
Hello, How can you catch the keycode on the body tag? ... $(document).keypress(function(event) {; alert('Handler for .keypress() called.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found