如何在 Curses 中获取 Shift+X / Alt+X 键?
我目前正在使用此代码来获取击键,但我缺少例如 Shift/Alt 键,例如 Ctrl+Shift+S,Ctrl+Shift+↑< /kbd>、Alt+S 等。
require 'curses'
Curses.noecho
Curses.raw
Curses.stdscr.keypad(true)
Curse.nonl
count = 0
loop do
count = (count + 1) % 20
key = Curses.getch
break if key == ?\C-c
Curses.setpos(count,0)
Curses.addstr("#{key.inspect} ");
end
有什么方法可以捕获它们吗?
另外:如何区分 Ctrl+J / Ctrl+M 和 Ctrl +Enter / Enter,它们给出相同的键码(10
/13
)?
I am currently using this code to grab key-strokes, but I am missing e.g. Shift/Alt keys like
Ctrl+Shift+S, Ctrl+Shift+↑, Alt+S, etc.
require 'curses'
Curses.noecho
Curses.raw
Curses.stdscr.keypad(true)
Curse.nonl
count = 0
loop do
count = (count + 1) % 20
key = Curses.getch
break if key == ?\C-c
Curses.setpos(count,0)
Curses.addstr("#{key.inspect} ");
end
Is there any way to capture them all ?
Also: how can I distinguish Ctrl+J / Ctrl+M from Ctrl+Enter / Enter, which give the same key-codes (10
/13
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
长话短说 - 你不能。终端几乎肯定会为每个产生相同的字节。请阅读
http://www.leonerd.org.uk/hacks/fixterms/
也就是说,如果您感觉特别勇敢,可以尝试我的
libtermkey
http://www.leonerd.org.uk/code/libtermkey/
至少可以正确解析
Ctrl-arrow
等内容。它(还)没有 Ruby 绑定,但 Perl 和 Python 绑定的存在表明编写一个绑定应该很容易。最后,如果您感觉更勇敢,可以运行我编写的终端
pangoterm
,它具有对任意修改的 Unicode 键进行编码的通用方法,因此它可以区分 Ctrl-m 和 Enter 等。 但是https://launchpad.net/pangoterm
,除此之外,答案仍然是“你不能”。
Long story short - you cannot. The terminal will almost-certainly yield the same bytes for each. Please read
http://www.leonerd.org.uk/hacks/fixterms/
That said, if you are feeling especially brave, you can try my
libtermkey
http://www.leonerd.org.uk/code/libtermkey/
which will at least correctly parse things like
Ctrl-arrow
. It doesn't (yet) have a Ruby binding, but the presence of both Perl and Python ones would suggest it should be quite easy to write one.Finally if you're feeling even braver you can run the terminal I wrote,
pangoterm
, which has generic ways to encode any arbitrarily modified Unicode keys, so it can distinguish Ctrl-m from Enter, etc...https://launchpad.net/pangoterm
However, outside of these, the answer remains "you cannot".