简体   繁体   English

Lua 中的按键事件?

[英]KeyPress event in Lua?

is it possible to get users keypress on lua?是否有可能让用户按下 lua? fe.铁。

while true do
    if keyPress(27)==true then
        print("You just pressed ESC")
    end
end

Lua is predicated on extreme portability. Lua以极端可移植性为前提。 As such it's based on supplying, essentially, only that which is available in ANSI C in terms of capabilities. 因此,它基于仅提供ANSI C中可用的功能。 (I think the sole exception to that is dynamic linking which is a non-ANSI feature not available on all platforms, but is so useful that they've put it in for many.) (我认为唯一的例外是动态链接,它是一种非ANSI功能,并非在所有平台上都可用,但非常有用,以至于它们已经为许多平台提供了它。)

ANSI C doesn't provide keypress functionality so the default Lua library doesn't either. ANSI C不提供按键功能,因此默认的Lua库也不提供。

That being said, the LuaRocks repository might lead you to a library with this capability. 话虽这么说, LuaRocks存储库可能会引导您进入具有此功能的库。 For example it could be that ltermbox , found on the LuaRocks page there, has the functionality you need. 例如,它可能是ltermbox ,该LuaRocks页那里发现,有你需要的功能。 (You'll probably have to remove the bits you don't want, mind.) There may be other libraries available. (您可能不得不删除您不想要的位。)可能还有其他库可用。 Go digging. 去挖掘。

Failing that, the whole point of Lua is extensibility. 如果做不到这一点 ,Lua的重点就是可扩展性。 It's an extensible extension language. 它是一种可扩展的扩展语言。 It's not actually all that hard to hand-roll your own extension that provides the functionality you want. 实际上,提供您想要的功能的扩展并不是那么难。

There is a binding to getkey() in the NTLua project. NTLua项目中绑定了getkey()。 You can get some sources from there. 你可以从那里得到一些资料。

(it just wraps getch()) (它只是包裹getch())

It seems like you are trying to make a game. 看起来你正在尝试制作游戏。 For 2D games you might want to consider love2d . 对于2D游戏,您可能需要考虑love2d It looks a little weird, but it works and it's relatively easy compared to other languages such as C. 它看起来有点奇怪,但它的工作原理与C等其他语言相比相对容易。

Not in stock Lua. 没有库存Lua。 Probably with an additional library. 可能还有一个额外的图书馆。

First thing's first: if you're using my method of doing this, you need to put the script(s) you use in a LocalScript. 第一件事是第一件事:如果您使用我的方法,您需要将您使用的脚本放在LocalScript中。 Not doing this will cause the key(s) to not show up in the console (F9 to see console). 不执行此操作将导致密钥不显示在控制台中(F9以查看控制台)。

Alright, now that we know it's in a LocalScript, here's the script: 好吧,现在我们知道它是在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

If you're wanting to signal only one key (lowercase only, or uppercase only) check below. 如果您只想发出一个键(仅限小写,或仅大写),请在下方查看。

Lowercase only: 仅限小写:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == "e" then
        print('You pressed e')
    end
end)

Uppercase only: 仅限大写:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == "E" then
        print('You pressed E')
    end
end)

Or, if you want to just signal any key in general, you can also do this: 或者,如果您只想发出任何关键信号,您也可以这样做:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    print('You pressed '..key)
end)

I hope I helped answer your question. 我希望我帮助回答你的问题。

if keypress=(29)==true then
print("hello")
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM