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
}