KeyPress event in Lua?
Asked Answered
M

7

5

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
Mediatory answered 16/4, 2011 at 20:57 Comment(0)
C
8

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.

Childbirth answered 17/4, 2011 at 1:23 Comment(0)
H
3

Not in stock Lua. Probably with an additional library.

Hegumen answered 16/4, 2011 at 23:32 Comment(0)
N
3

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

(it just wraps getch())

Nowadays answered 17/4, 2011 at 1:56 Comment(0)
E
3

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.

Evening answered 29/12, 2011 at 2:51 Comment(0)
A
0

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.

Agueda answered 28/12, 2018 at 11:35 Comment(1)
This is roblox lua, not pure luaNavigator
C
0

You must use string.byte(io.read()):

while true do
    if string.byte(io.read()) == 27 then
        print("You just pressed ESC")
    end
end
Clemmieclemmons answered 7/3, 2023 at 23:21 Comment(0)
T
-2
if keypress=(29)==true then
print("hello")
end
Tampa answered 29/1, 2023 at 5:38 Comment(1)
Hi welcome to stackoverflow. Can you please offer more context to your answer? Without context, it may be difficult to understand how your answer is different from other answer's especially when there is discussion about key events not being native to Lua. stackoverflow.com/help/how-to-answerSpada

© 2022 - 2024 — McMap. All rights reserved.