开发者

KeyPress event in Lua?

is it possible to get users keypress on lua? 开发者_运维技巧fe.

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


Lua is predicated on extreme portability. As such it's based on supplying, essentially, only that which is available in ANSI C in terms of capabilities. (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 C doesn't provide keypress functionality so the default Lua library doesn't either.

That being said, the LuaRocks repository might lead you to a library with this capability. For example it could be that ltermbox, found on the LuaRocks page there, has the functionality you need. (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. 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.


Not in stock Lua. Probably with an additional library.


There is a binding to getkey() in the NTLua project. You can get some sources from there.

(it just wraps getch())


It seems like you are trying to make a game. For 2D games you might want to consider love2d. It looks a little weird, but it works and it's relatively easy compared to other languages such as C.


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. Not doing this will cause the key(s) to not show up in the console (F9 to see console).

Alright, now that we know it's in a LocalScript, here's the script:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜