TL;DR
如何让 Ruby 诅咒正确响应箭头键? KEY_UP 常量似乎与我的输入不匹配。
环境及问题描述
I am running Ruby 2.1.2 with the curses 1.0.1 gem https://github.com/ruby/curses. I'm trying to enable arrow-key navigation with curses. I've enabled Curses#getch http://www.ruby-doc.org/stdlib-2.0/libdoc/curses/rdoc/Curses.html#method-c-getch to fetch a single key without waiting for the carriage return by calling Curses#cbreak http://www.ruby-doc.org/stdlib-2.0/libdoc/curses/rdoc/Curses.html#method-c-cbreak, and this is working fine for the k character. However, I really want to enable arrow key navigation, and not just HJKL for movement.
目前,向上箭头在我的程序中打印 27,这似乎是正确的顺序值 http://www.ruby-doc.org/core-2.1.2/String.html#method-i-ord我的键盘给出了向上箭头键:
"^[[A".ord
#=> 27
and which should be matched by the Curses KEY_UP constant http://www.ruby-doc.org/stdlib-2.0/libdoc/curses/rdoc/Curses/Key.html. It isn't, and so falls through to the else statement to display the ordinal value. The up-arrow key also leaves [A as two separate characters at the command prompt when the ruby program exits, which might indicate that Curses#getch http://www.ruby-doc.org/stdlib-2.0/libdoc/curses/rdoc/Curses.html#method-c-getch isn't capturing the key press properly.
我的红宝石代码
require 'curses'
include Curses
begin
init_screen
cbreak
noecho
keypad = true
addstr 'Check for up arrow or letter k.'
refresh
ch = getch
addch ?\n
case ch
when KEY_UP
addstr "up arrow \n"
when ?k
addstr "up char \n"
else
addstr "%s\n" % ch
end
refresh
sleep 1
ensure
close_screen
end
在启用键盘的行中,您实际上创建了一个名为“keypad”的局部变量,因为该方法位于类中Curses::Window
。
由于您没有制作自己的窗户(除了init_screen
),你可以直接使用stdscr方法来参考标准的。如果我将第 8 行更改为:
stdscr.keypad = true
那么你的示例代码对我有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)