0
我想在Ruby中使用诅咒:Ruby + Curses:非阻塞getch()+陷阱为Ctrl + c?
getch()
不能阻止/暂停程序。- 按
q
时,立即退出程序。 Ctrl-C
避免中断的陷阱。
然而,我刚完成可以在第一点:
- 当按
q
,它会等待退出之前一段时间(< 1秒)。 - 似乎
Curses
使陷阱Ctrl-C
不工作...
# -*- coding: utf-8 -*-
require "curses"
Curses.init_screen
Curses.noecho()
Curses.curs_set(0) #invisible cursor
Curses.timeout = 0
Curses.addstr("Press [q] to abort")
sec=0
while true
# if place this outside the while loop, q key will be unable to work
# at all...
if Curses.getch == 'q'
Curses.close_screen #seems unnecessary
exit
end
sec += 1
hello = "Hello World #{sec}"
Curses.setpos(Curses.lines/2, Curses.cols/2 - (hello.length/2))
Curses.addstr(hello)
Curses.refresh
sleep 1
end
# Avoid C-c interruption, but Curses seems to ignore it.
Signal.trap(:INT){
return nil
}