0
A
回答
2
您可以通过更改终端设置做到这一点使用luaposix(POSIX上唯一的机器,很明显)(参见man termios
):
local p = require("posix")
local function table_copy(t)
local copy = {}
for k,v in pairs(t) do
if type(v) == "table" then
copy[ k ] = table_copy(v)
else
copy[ k ] = v
end
end
return copy
end
assert(p.isatty(p.STDIN_FILENO), "stdin not a terminal")
-- derive modified terminal settings from current settings
local saved_tcattr = assert(p.tcgetattr(p.STDIN_FILENO))
local raw_tcattr = table_copy(saved_tcattr)
raw_tcattr.lflag = bit32.band(raw_tcattr.lflag, bit32.bnot(p.ICANON))
raw_tcattr.cc[ p.VMIN ] = 0
raw_tcattr.cc[ p.VTIME ] = 10 -- in tenth of a second
-- restore terminal settings in case of unexpected error
local guard = setmetatable({}, { __gc = function()
p.tcsetattr(p.STDIN_FILENO, p.TCSANOW, saved_tcattr)
end })
local function read1sec()
assert(p.tcsetattr(p.STDIN_FILENO, p.TCSANOW, raw_tcattr))
local c = io.read(1)
assert(p.tcsetattr(p.STDIN_FILENO, p.TCSANOW, saved_tcattr))
return c
end
local c = read1sec()
print("key pressed:", c)
0
的lcurses(ncurses的为lua)的Lua库可能提供这一点。你将不得不下载并安装它。有一个例子,如何检查按键只在Create a function to check for key press in unix using ncurses,它是在C中,但在Lua中的ncurses API是相同的。否则,你将不得不使用C/C++ API创建一个Lua扩展模块:你将创建你从Lua调用的C函数,然后这个C函数可以访问操作系统通常的函数,比如getch,select,等等(取决于你是否在Windows或Linux上)。
相关问题
- 1. node.js:具有时间间隔的货币限制并行执行(速率限制)
- 2. 实现具有时间限制代码的类很难(C++)
- 3. 从具有时间限制的程序执行程序(Java)
- 4. 带有时间限制的HTML文本
- 5. 如何在lua中中断io.read(),充当“等待”功能
- 6. PHP:制作时间限制
- 7. 数限制的时间内
- 8. Ffmpeg的时间限制
- 9. 有时间限制共享软件
- 10. ruby IO.read()锁吗?
- 11. 具有多个限制
- 12. AppleScript具有行数限制。
- 13. newQuad函数限制为5? Lua的
- 14. 限制时间间隔的比例
- 15. Java EventHander限制(时间)
- 16. 时间限制查询
- 17. adhoc发布时间限制
- 18. 限制测验时间
- 19. max_execution时间限制php
- 20. RealmResult处理时间限制
- 21. didReceiveNotificationRequest:withContentHandler - 执行时间限制?
- 22. iOS限制后台时间
- 23. 命令限制时间
- 24. Drools:时间限制规则
- 25. Freeswitch:限制通话时间
- 26. While循环限制时间
- 27. nginx auth_basic时间限制
- 28. 操作时间限制
- 29. 具有响应时间限制的异步请求 - 响应算法
- 30. PHP:无限循环和时间限制!