Page 1 of 2

Get all keys currently pressed

Posted: Wed Sep 26, 2012 12:35 pm
by clofresh
Hello,

I've been following Love's progress for a while and finally got a chance to start a game with it. I was wondering if there was a way to get all the keys currently pressed, instead of having to check each key that you cared about? I couldn't find anything on the wiki or searching the forums.

Re: Get all keys currently pressed

Posted: Wed Sep 26, 2012 12:53 pm
by tv_user
As far as I'm aware, you can't do it in one go. It's possible (but perhaps unnecessary) to test for EVERY key with

Code: Select all

love.keyboard.isDown( key )
but I don't think that would help at all. Honestly, I like the way LOVE handles keyboard input, because you end up not having to test for keypress every frame as it just triggers the event with the callback function love.keypressed( key)
For a different perspective on dealing with keypress, there's a nice article here. There are also some libraries made by LOVE users, to simplify key binding, etc - just check the Wiki.
Good luck.

Re: Get all keys currently pressed

Posted: Wed Sep 26, 2012 3:24 pm
by kikito
Forget about love.keyboard. Use love.keypressed and love.keyreleased to update a table instead.

Example (download and change extension to .zip to see the contents):
keys_pressed.love
(331 Bytes) Downloaded 325 times
Note that the amount of simultaneously pressed keys is limited to between 3 and 6 - that's a limitation on your hardware or operative system that love can't overcome.

Re: Get all keys currently pressed

Posted: Wed Sep 26, 2012 3:51 pm
by qaisjp

Code: Select all

KeysPressed = {}
function love.keypressed(a, b)
    KeysPressed[a]=b
end
function love.keyreleased(a)
    KeysPressed[a] = nil
end
then to check, just do KeysPressed["w"] etc. the value is the unicode value.

Re: Get all keys currently pressed

Posted: Wed Sep 26, 2012 3:53 pm
by tv_user
wow, nice kikito, I hadn't thought of handling it like that! We keep on learning... ^^

Re: Get all keys currently pressed

Posted: Wed Sep 26, 2012 4:36 pm
by clofresh
Ah! Great solution, thanks kikito!

Re: Get all keys currently pressed

Posted: Wed Sep 26, 2012 6:19 pm
by Nixola
kikito wrote:Forget about love.keyboard. Use love.keypressed and love.keyreleased to update a table instead.

Example (download and change extension to .zip to see the contents):
keys_pressed.love
Note that the amount of simultaneously pressed keys is limited to between 3 and 6 - that's a limitation on your hardware or operative system that love can't overcome.
I managed to press 9 keys at once. :3

Re: Get all keys currently pressed

Posted: Thu Sep 27, 2012 6:53 am
by Lafolie
I think it depends on your individual hardware. Macbook Pro keyboards seem to have some strange rules here, I seem to be able to press combinations depending no the x and y, if you will, of the keys already pressed. Also the arrow keys only seems to allow 2 arrow keys to be down at a time.

Re: Get all keys currently pressed

Posted: Fri Sep 28, 2012 10:01 pm
by bartbes
Most keyboards divide the keys into groups, and there's a limit to the amount of keys you can press within a group. (And therefore the amount of keys in total.)

Re: Get all keys currently pressed

Posted: Fri Jan 03, 2014 11:05 pm
by stout
kikito wrote:Forget about love.keyboard. Use [wiki]love.keypressed[/wiki] and [wiki]love.keyreleased[/wiki] to update a table instead.

Example (download and change extension to .zip to see the contents):
keys_pressed.love
Note that the amount of simultaneously pressed keys is limited to between 3 and 6 - that's a limitation on your hardware or operative system that love can't overcome.
How would you adapt this for the joystick module to see what buttons are currently being pressed?

EDIT: ah, just found love.joystickpressed and released, nevermind!