ruby curses: How to get ctrl/meta keys with

江枫思渺然 提交于 2020-01-25 00:29:07

问题


Im trying to Curses.getchr, but keys like Ctrl+s are not captured, is there any lib that would allow me to capture them and best of all something intuitive/readable like

FooBar.bind('Ctrl+s'){ raise "dont save!" }

回答1:


Ctrl+s is usually grabbed by the terminal, so you have to put Curses in raw mode to capture that key. Here is an example:

#!/usr/bin/ruby

require 'curses'

Curses.raw # intercept everything
Curses.noecho
loop do
  case Curses.getch
    when ?q     then break
    when ?b     then Curses.addch ?b
    when ?\C-s  then Curses.addstr "^s" # Ctrl+S
  end
end


来源:https://stackoverflow.com/questions/4607891/ruby-curses-how-to-get-ctrl-meta-keys-with

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