Asynchronous 卢阿。io.read有时间限制

Asynchronous 卢阿。io.read有时间限制,asynchronous,input,lua,Asynchronous,Input,Lua,可以设置读取Lua终端输入的时间限制 例如,您只有1秒的时间来写一封信,否则程序将跳过此操作 谢谢你的提示;) lcurses(针对Lua的ncurses)Lua库可能提供此功能。您必须下载并安装它。有一个示例说明了如何仅在处检查按键,它是C语言,但在Lua中,ncurses API是相同的 否则,您必须使用C/C++API创建一个Lua扩展模块:您将创建从Lua调用的C函数,然后该C函数可以访问操作系统的常用函数,如getch、select等(取决于您是在Windows还是Linux上) 您可

可以设置读取Lua终端输入的时间限制

例如,您只有1秒的时间来写一封信,否则程序将跳过此操作

谢谢你的提示;)

lcurses(针对Lua的ncurses)Lua库可能提供此功能。您必须下载并安装它。有一个示例说明了如何仅在处检查按键,它是C语言,但在Lua中,ncurses API是相同的


否则,您必须使用C/C++API创建一个Lua扩展模块:您将创建从Lua调用的C函数,然后该C函数可以访问操作系统的常用函数,如getch、select等(取决于您是在Windows还是Linux上)

您可以使用(显然,仅限POSIX机器上)更改终端设置(请参阅):

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 )