通过命令行发送ctrl-c时如何在lua中捕获ctrl-c
问题描述:
我想知道命令行中的用户何时按下Ctrl-c,以便保存一些东西.
I would like to know when the user from a command line presses control-c so I can save some stuff.
我该怎么做?我看了看,但还没真正看过.
How do I do this? I've looked but haven't really seen anything.
注意:我对lua有点熟悉,但是我不是专家.我主要使用lua来使用库Torch( http://torch.ch/)
Note: I'm somewhat familiar with lua, but I'm no expert. I mostly use lua to use the library Torch (http://torch.ch/)
答
实现 SIGINT
处理程序使用出色的 luaposix 库很简单:
local signal = require("posix.signal")
signal.signal(signal.SIGINT, function(signum)
io.write("\n")
-- put code to save some stuff here
os.exit(128 + signum)
end)
有关更多信息,请参见 posix.signal 模块的API文档.
Refer to the posix.signal module's API documentation for more information.