Lua中的KeyPress事件?

| 是否可以使用户在lua上按键? fe。
while true do
    if keyPress(27)==true then
        print(\"You just pressed ESC\")
    end
end
    
已邀请:
Lua具有极高的可移植性。因此,它本质上是基于功能方面提供的,仅基于ANSI C中提供的功能。 (我认为唯一的例外是动态链接,动态链接是一种非ANSI功能,并非在所有平台上都可用,但是它是如此有用,以至于它们已经被许多人使用。) ANSI C不提供按键功能,因此默认的Lua库也不提供。 话虽如此,LuaRocks存储库可能会将您带到具有此功能的库。例如,可能是在那里的LuaRocks页面上找到的ltermbox具有所需的功能。 (记住,您可能必须删除不需要的位。)可能还有其他可用的库。去挖吧 失败的是,Lua的重点是可扩展性。这是一种可扩展的扩展语言。手动滚动提供所需功能的扩展程序实际上并不那么困难。     
NTLua项目中的getkey()有一个绑定。您可以从那里获得一些资源。 (它只包装getch())     
似乎您正在尝试制作游戏。对于2D游戏,您可能需要考虑love2d。它看起来有些怪异,但是它可以正常工作,并且与其他语言(例如C)相比,它相对容易。     
没有库存Lua。可能有一个额外的库。     
首先,如果您使用的是我的方法,则需要将使用的脚本放在LocalScript中。不这样做将导致密钥不显示在控制台中(按F9键查看控制台)。 好了,现在我们知道它在LocalScript中,这是脚本:
local player = game.Players.LocalPlayer -- Gets the LocalPlayer
local mouse = player:GetMouse() -- Gets the player\'s mouse

mouse.KeyDown:connect(function(key) -- Gets mouse, then gets the keyboard
    if key:lower() == \"e\" or key:upper() == \"E\" then -- Checks for selected key (key:lower = lowercase keys, key:upper = uppercase keys)
        print(\'You pressed e\') -- Prints the key pressed
    end -- Ends if statement
end) -- Ends function
如果您只想发送一个键(仅小写或仅大写),请检查以下内容。 仅小写:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == \"e\" then
        print(\'You pressed e\')
    end
end)
仅大写:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == \"E\" then
        print(\'You pressed E\')
    end
end)
或者,如果您只想发出任何信号,通常也可以这样做:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    print(\'You pressed \'..key)
end)
希望我能回答您的问题。     

要回复问题请先登录注册