user1632861
user1632861

Reputation:

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

Upvotes: 5

Views: 56282

Answers (7)

Dioni Padilha
Dioni Padilha

Reputation: 1

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

while true do
    if string.byte(io.read()) == 27 then
        print("You just pressed ESC")
    end
end

Upvotes: 0

Archana devi
Archana devi

Reputation: 1

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

Upvotes: -2

James
James

Reputation: 11

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.

Upvotes: 0

user1108983
user1108983

Reputation:

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.

Upvotes: 3

sylvanaar
sylvanaar

Reputation: 8216

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

(it just wraps getch())

Upvotes: 3

JUST MY correct OPINION
JUST MY correct OPINION

Reputation: 36107

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.

Upvotes: 8

lhf
lhf

Reputation: 72312

Not in stock Lua. Probably with an additional library.

Upvotes: 3

Related Questions