0

I am new to using the blessed library and so far, I could not get key events to work.

I would expect the following piece of code to print q whenever the Q key is pressed and Enter whenever the Enter key is pressed. It should also print keypress whenever any key is pressed.

const blessed = require('blessed');

const screen = blessed.screen({
    smartCSR: true,
    title: 'Title',
});

screen.key('q', () => console.log('q'));
screen.key('Enter', () => console.log('Enter'));

screen.on('keypress', () => console.log('keypress'));

screen.render();

The actual behavior is that if any "printable" key (such as alphanumeric characters) is pressed then that key is just written to the console (just as if it was typed regularly). keypress is not printed either.

The behavior is slightly different for the Enter key. Enter is still not being printed, but keypress is.

Is there anything wrong with my setup? I am using the default Gnome terminal under ArchLinux. However, that should not cause an issue because the program json-log-viewer, which is also blessed-based, works just fine.

Lehks
  • 2,582
  • 4
  • 19
  • 50
  • Blessed is a good library but unfortunately it's not maintained. The repo should be archived. `terminal-kit` is maintained and has many APIs but it has terrible documentations. I was trying to write TUI in JS and I gave up eventually. – Ram May 11 '22 at 19:21
  • Yeah that's probably true. Even after I got the key events to work they are still very slow (holding down a key feels incredibly laggy). I'll also give terminal-kit a try and see how that works. Another interesting library should be [ink](https://github.com/vadimdemedes/ink) which promises to bring React to the terminal. – Lehks May 12 '22 at 05:01
  • I personally didn't have any problems with key bindings (I used node readline for key event handling). I was trying to make a ncurses-like interface and the app needed modals. I encountered various issues. I wrote the app by using electron eventually. TUI wasn't a good option for my case. Blessed is unmaintained, but it's still a good library and terminal-kit has many useful APIs. Good luck! – Ram May 12 '22 at 08:13

1 Answers1

1

The problem was with nodemon, fixed by passing the flag -I. See here.

Lehks
  • 2,582
  • 4
  • 19
  • 50
  • Holy crap. I know there's not a need for gratitude comments, but you saved me, and taught me something new. Adding a bit of knowledge I came across to reload your app state on restart: https://github.com/remy/nodemon#gracefully-reloading-down-your-script – Michael Campbell Oct 26 '22 at 05:31