【发布时间】:2017-07-11 13:25:29
【问题描述】:
我有一个 Ruby curses 应用程序,我想在其中捕获箭头键和功能键。问题是使用STDIN.getch 时某些击键会生成多个值。当我输入像a-z 这样的“常规”键时,我会返回一个值。当我键入 [F] 键或箭头键时,我会返回三个值。
是否有专门用于处理键盘输入的 gem 或更好的方法来完成读取击键?
#!/usr/bin/ruby
require 'curses'
require 'io/console'
Curses.noecho
Curses.init_screen
main_window = Curses::Window.new(24, 40, 1, 0)
num_keys = 0
loop do
ch = STDIN.getch
num_keys = num_keys + 1
main_window.addstr(' key:' + ch.inspect + ' count:' + num_keys.to_s)
main_window.refresh
break if ch == 'q'
end
Curses.close_screen
【问题讨论】: