Skip to content

Commit

Permalink
read_line: correctly detect newline/enter char
Browse files Browse the repository at this point in the history
  • Loading branch information
gauteh committed Jan 29, 2014
1 parent 5a558b5 commit 741c15c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion examples/read_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
# Written 2003, 2004 by Tobias Peters
# No warranties
# Share and enjoy!
#
# 2014: Updated by Gaute Hope
# Note: This is not wide char / unicode safe - take a look at
# form_get_wch.rb for a better example.

require "ncursesw"

Expand All @@ -26,7 +30,7 @@ def read_line(y, x,
cursor_pos = [0, cursor_pos-1].max
when Ncurses::KEY_RIGHT
# similar, implement yourself !
when Ncurses::KEY_ENTER, ?\n, ?\r
when Ncurses::KEY_ENTER, "\n".ord
return string, cursor_pos, ch # Which return key has been used?
when Ncurses::KEY_BACKSPACE
string = string[0...([0, cursor_pos-1].max)] + string[cursor_pos..-1]
Expand All @@ -38,9 +42,11 @@ def read_line(y, x,
string[cursor_pos,0] = ch.chr
cursor_pos += 1
else
Ncurses.mvprintw(15, 10, "A: Got: #{ch.inspect} (#{[ch].pack("U")})")
Ncurses.beep
end
else
Ncurses.mvprintw(16, 10, "B: Got: #{ch.inspect} (#{[ch].pack("U")})")
Ncurses.beep
end
end
Expand All @@ -54,6 +60,7 @@ def read_line(y, x,

# recognize KEY_ENTER, KEY_BACKSPACE etc
Ncurses.keypad(Ncurses.stdscr, true)
#Ncurses.nonl()

y = 10
x = 2
Expand Down

0 comments on commit 741c15c

Please sign in to comment.