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

7 Answers7

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.

JUST MY correct OPINION
  • 35,674
  • 17
  • 77
  • 99
3

Not in stock Lua. Probably with an additional library.

lhf
  • 70,581
  • 9
  • 108
  • 149
3

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

(it just wraps getch())

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
sylvanaar
  • 8,096
  • 37
  • 59
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.

Arlen
  • 6,641
  • 4
  • 29
  • 61
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.

James
  • 11
  • 1
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
James Risner
  • 5,451
  • 11
  • 25
  • 47
-2
if keypress=(29)==true then
print("hello")
end
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 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. https://stackoverflow.com/help/how-to-answer – DJSDev Feb 04 '23 at 16:31