通过命令行发送ctrl-c时如何在lua中捕获ctrl-c [英] How catch ctrl-c in lua when ctrl-c is sent via the command line
本文介绍了通过命令行发送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.
这篇关于通过命令行发送ctrl-c时如何在lua中捕获ctrl-c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文