Ruby curses controlling the enter key

I am learning the Curses class and am having trouble controlling the ENTER key. This is my code:

require 'curses' win = Curses::Window.new(0, 0, 0, 0) Curses.init_screen Curses.cbreak Curses.nonl Curses.stdscr.keypad(true) loop do case Curses.getch when 13 # Enter Curses.addstr "abc" when 8 # Backspace Curses.delch end end win.close 

The problem is that when I press the ENTER key, "abc" is written on the screen (as expected); however, when I press ENTER again, "abc" just overwrites the same position on the screen. It seems that pressing the ENTER key first sets the cursor position to (0,0) and then adds the line. How can I stop setting the cursor position to (0,0)?

In addition, for some reason, the constants do not match my keys, so the ENTER key is 13, but the Ruby constant for the ENTER key is 400ish. I do not use a fancy keyboard or anything else; standard american.

+4
source share
1 answer

Well, it seems that Curses.nonl means that when the ENTER key is pressed, "\ r" is written instead of "\ r \ n", so the only way to solve this problem is to disable any output on the screen using Curses.raw and implement the entire data record to the screen.

I do not know why there are not so many constants. Perhaps this is a constant for the other ENTER key on the keyboard (in the lower right corner). It doesn't really matter, I can just put Curses.getch to find out the key number.

+1
source

All Articles