KeyPress event in Lua?

36,733

Solution 1

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.

Solution 2

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

(it just wraps getch())

Solution 3

Not in stock Lua. Probably with an additional library.

Share:
36,733
Admin
Author by

Admin

Updated on December 29, 2020

Comments

  • Admin
    Admin over 3 years

    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
    
  • AnnoyinC
    AnnoyinC about 3 years
    This is roblox lua, not pure lua