Get all keys currently pressed

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
clofresh
Citizen
Posts: 87
Joined: Sun Jul 26, 2009 4:21 pm
Contact:

Get all keys currently pressed

Post 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.
----------------------------------------
Sluicer Games
User avatar
tv_user
Citizen
Posts: 57
Joined: Sun Aug 12, 2012 4:39 pm
Location: Portugal

Re: Get all keys currently pressed

Post 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.
Did my comment help/offended you? Use the Karma button!!
LÖVEing each day...
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Get all keys currently pressed

Post 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 332 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.
When I write def I mean function.
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: Get all keys currently pressed

Post 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.
Lua is not an acronym.
User avatar
tv_user
Citizen
Posts: 57
Joined: Sun Aug 12, 2012 4:39 pm
Location: Portugal

Re: Get all keys currently pressed

Post by tv_user »

wow, nice kikito, I hadn't thought of handling it like that! We keep on learning... ^^
Did my comment help/offended you? Use the Karma button!!
LÖVEing each day...
User avatar
clofresh
Citizen
Posts: 87
Joined: Sun Jul 26, 2009 4:21 pm
Contact:

Re: Get all keys currently pressed

Post by clofresh »

Ah! Great solution, thanks kikito!
----------------------------------------
Sluicer Games
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Get all keys currently pressed

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Get all keys currently pressed

Post 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.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Get all keys currently pressed

Post 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.)
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: Get all keys currently pressed

Post 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!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 12 guests