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
)?
also: how can distinguish ctrl+j / ctrl+m from ctr+enter / enter, which give the same key-codes (10/13)
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".
精彩评论