How to get Shift+X / Alt+X keys in Curses ?

眉间皱痕 提交于 2019-12-13 02:27:01

问题


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)?


回答1:


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".



来源:https://stackoverflow.com/questions/4684739/how-to-get-shiftx-altx-keys-in-curses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!