Question

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!" }
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top