Where do you check for keyboard input

General discussion about LÖVE, Lua, game development, puns, and unicorns.

Where do you check for keyboard input

In the keypressed/keyreleased callbacks with if key == myKey then
3
18%
In the update callback with if love.keyboard.isDown(myKey) then
3
18%
In both places depending on the specific check
10
59%
Some other way
1
6%
 
Total votes: 17

TacticalPenguin
Prole
Posts: 15
Joined: Thu Dec 11, 2008 5:44 am

Where do you check for keyboard input

Post by TacticalPenguin »

Just a consensus poll for how everyone does it. I am currently arguing with myself over which to go with and neither side is making much progress. Vote and post what you think...
User avatar
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

Re: Where do you check for keyboard input

Post by Xcmd »

I check both as they have entirely and incredibly different uses.

When you're checking it inside of a function, like update or draw, you're checking it each time that loop is passed through. When you check the keypressed function, you're checking it only when the key is pressed. They have entirely different uses, although mostly you use keypressed when you want to check one key press at a time and update when you want to be able to press multiple keys.

But you have to use if / end loops properly as well. If you do if / else / end loops with your key presses, it's going to function the same as if you used keypressed. But if you use if / end for each keypress you should be able to achieve the results of multiple button presses.

Anyway, I hope that makes sense.
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
TacticalPenguin
Prole
Posts: 15
Joined: Thu Dec 11, 2008 5:44 am

Re: Where do you check for keyboard input

Post by TacticalPenguin »

That makes sense and I'd thought of that before but I just did a test which showed me that keypressed only runs when the key is first pressed down, not any time it is pressed down, which I should've figured but never did for some reason, and now knowing that makes the reasons to use different places for different things make a lot more sense.

Maybe I should change the question to be asking which one people prefer when one is not necessary over the other...
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: Where do you check for keyboard input

Post by osgeld »

TacticalPenguin wrote:Maybe I should change the question to be asking which one people prefer when one is not necessary over the other...
I use the functions over the callbacks, usually if im looking for a keypress that means im going to update something, and it seems more logical to just do "if left then move (X * dt)" rather than spreading it out and making a mess
TacticalPenguin
Prole
Posts: 15
Joined: Thu Dec 11, 2008 5:44 am

Re: Where do you check for keyboard input

Post by TacticalPenguin »

osgeld wrote:
TacticalPenguin wrote:Maybe I should change the question to be asking which one people prefer when one is not necessary over the other...
I use the functions over the callbacks, usually if im looking for a keypress that means im going to update something, and it seems more logical to just do "if left then move (X * dt)" rather than spreading it out and making a mess
The bit with the dt makes a lot of sense for movement control.

It seems each one has its own separate uses which I suppose will just have to annoy me, having the same general functionality split into two different spots....here's to less prestructuredness in 0.6.0.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Where do you check for keyboard input

Post by bartbes »

0.6.0: It's still there, however, you can enable key repeat for the keypressed, and it will return the unicode equivalent of the button(s) pressed, so, keypressed is more of text input and commands, and isDown for code which needs to be run while the key is down.

And I voted both.

EDIT: Thought it might be unclear, key repeat and the unicode character are not triggered by each other, they are both new features.
User avatar
Zorbatron
Citizen
Posts: 78
Joined: Wed May 27, 2009 6:58 pm

Re: Where do you check for keyboard input

Post by Zorbatron »

TacticalPenguin wrote:Just a consensus poll for how everyone does it. I am currently arguing with myself over which to go with and neither side is making much progress. Vote and post what you think...
You always want to check for key presses in the keypressed and keyreleased events! Always! There is no point in doing it within the update loop, this adds a preformance drop that is unnecessary and the exact results can be achieved faster with keypressed/keyreleased.

That is the reason keypressed and keyreleased callbacks where added, so you do not have to write a lua loop that runs everyframe. This is cpu expensive people.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Where do you check for keyboard input

Post by bartbes »

This is a matter of personal style, and one could argue that saving every pressed character presents other overhead, which might outweigh the overhead created by using love.keyboard.isDown. Furthermore, on most modern computers resources are not that limited that it really matters.

Once again, despite my tone, I mean no offense.
User avatar
Zorbatron
Citizen
Posts: 78
Joined: Wed May 27, 2009 6:58 pm

Re: Where do you check for keyboard input

Post by Zorbatron »

bartbes wrote:This is a matter of personal style, and one could argue that saving every pressed character presents other overhead, which might outweigh the overhead created by using love.keyboard.isDown. Furthermore, on most modern computers resources are not that limited that it really matters.

Once again, despite my tone, I mean no offense.
I'm going by what I learn from websites, personal tests, and concrete evidence, not my opinion.

Source: http://lua-users.org/lists/lua-l/2004-04/msg00164.html (scroll to the section "Binding Lua to C++")
Source wrote: Avoid using Lua in "every frame" actions as performance drops
dramatically. For example, don't use Lua to do animation updates; set
up parameters for the animation in Lua and do the updates in C/C++
code.
Just want you to realise alot of this stuff isn't my opinion as much as it is the opinions of people who've done alot of research.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Where do you check for keyboard input

Post by bartbes »

The piece you quoted doesn't have anything to do with this particular situation.
Post Reply

Who is online

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