What is a helpful way to think about input? love.keypressed or love.keyboard.isDown?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
alejandroalzate
Citizen
Posts: 67
Joined: Sat May 08, 2021 9:45 pm

Re: What is a helpful way to think about input? love.keypressed or love.keyboard.isDown?

Post by alejandroalzate »

Yolwoocle wrote: Sat Feb 26, 2022 6:34 pm Hi.
I come from a background of making games on PICO-8, and its API has two functions, called "btn" and "btnp", which respectively return whether a button is down or if it's just been pressed (i.e. for the first frame). I have used myself to think about input in terms of checking it in the "update" function. However, LÖVE seems to have a "love.keypressed" function, which is, as far as I know, the only way to check if a button has been just been pressed (as opposed to checking if it's down continuously).
Imagine I have a game with different weapons, some of which are automatic, where you have to hold the "fire" button to fire, while others are manual, where you have to press on this button repeatedly to fire.
What is a helpful way to think about this? Use "love.keyboard.isDown" in my player's update method, and have separate code for "love.keypressed"? Seems weird to me that the same action should be put in different places in the code.
Thanks
Think it like in love update you are ready to calculate something BUT u need user input; then you need to call

Code: Select all

love.keyboard.isDown("keys to check")
to check if a key is held on that frame and u gonna get a boolean but is wastefull and has an delay instead use the callback keypressed

Code: Select all

function love.keypressed(key, ...)
if key == "esc" then
love.event.quit()
end
end
is called in ANY time almost *INSTANLY* (if the game is not lagged) once when a key is pressed (but not released for that use keyreleased)

Code: Select all

function love.keyreleased(key)
--ur stuff
end

Code: Select all

target = boardIndex.getWhosPeekingThisLine()
target:setObey(true)
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: What is a helpful way to think about input? love.keypressed or love.keyboard.isDown?

Post by zorg »

ReFreezed wrote: Mon Feb 28, 2022 6:03 am
zorg wrote: Mon Feb 28, 2022 4:28 am keyconstants are useless and one should use Scancodes
Only for controls. For keyboard shortcuts (like e.g. Ctrl+C to copy) keyconstants ought to be used.
Why?
BrotSagtMist wrote: Mon Feb 28, 2022 4:36 am Then youll end up with a situation that the documentation wont match.
"press x"
"where the heck is x?"
Edit: also forgot that in the custom keyboard scene scanncodes are altered too on the hardware site.
My answer pertained _only_ to the original question; for this *next* issue, you can probably only use love.textinput, for a very big reason:
Yolwoocle wrote: Mon Feb 28, 2022 1:26 pm Isn't there love.keyboard.getKeyFromScancode for this very purpose? Scancodes are great for people like me who use a French keyboard layout :P
MrFariator wrote: Mon Feb 28, 2022 2:05 pm ...
Both scancode and keyconstant tables are based on the US QWERTY layout, now of course the scancode one should work for most if not all regular layouts, but getKeyFromScancode will fail if you'll try to get a "key" (character) from a keyboard that features something not on the "default" US QWERTY layout, (as in, it will return unknown for all such keys; e.g. on my hungarian QWERTZ, i have keys for öüóőúéáűí, and none of those give back the actual letters when using the mentioned function) so keyconstants are useless there too.

textinput, on the other hand, does get the correct symbol (if nothing else is held down, and the key is not a "dead" key that needs to combine, or be pressed twice to write the symbol)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: What is a helpful way to think about input? love.keypressed or love.keyboard.isDown?

Post by zorg »

alejandroalzate wrote: Tue Mar 01, 2022 12:12 am Think it like in love update you are ready to calculate something BUT u need user input; then you need to call

Code: Select all

love.keyboard.isDown("keys to check")
to check if a key is held on that frame and u gonna get a boolean but is wastefull and has an delay instead use the callback keypressed is called in ANY time almost *INSTANLY* (if the game is not lagged) once when a key is pressed (but not released for that use keyreleased)
Not sure where you got the idea that love.keyboard.isDown / isScancodeDown has a delay, because it doesn't (compared to keypressed, i mean); you can look at the default love.run function on the wiki to see how things work.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: What is a helpful way to think about input? love.keypressed or love.keyboard.isDown?

Post by ReFreezed »

zorg wrote: Tue Mar 01, 2022 5:31 am
ReFreezed wrote: Mon Feb 28, 2022 6:03 am
zorg wrote: Mon Feb 28, 2022 4:28 am keyconstants are useless and one should use Scancodes
Only for controls. For keyboard shortcuts (like e.g. Ctrl+C to copy) keyconstants ought to be used.
Why?
Imagine if documentation for software had to have a different version for every keyboard layout. The QWERTY version would say "press C to copy", a Turkish version would say "Press V to copy", and the Dvorak version would say "press J to copy" (they all refer to the same physical key location, or scancode). Wouldn't that be pretty confusing? Using the position on the keyboard doesn't make sense here - it's the actual key that matters, unlike with controls (like using the keys physically located in the top left corner as movement keys).
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: What is a helpful way to think about input? love.keypressed or love.keyboard.isDown?

Post by darkfrei »

Use WASD scancodes to move and ZXCV keys for hotkeys.

Some problems where WASD scancodes have any of overlapping with that keys.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
BrotSagtMist
Party member
Posts: 657
Joined: Fri Aug 06, 2021 10:30 pm

Re: What is a helpful way to think about input? love.keypressed or love.keyboard.isDown?

Post by BrotSagtMist »

There is an universal accepted way to move within a program: Arrow keys.
Oh boy i wish ppl would honor that instead going all idiotic with wasd.
obey
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: What is a helpful way to think about input? love.keypressed or love.keyboard.isDown?

Post by GVovkiv »

BrotSagtMist wrote: Tue Mar 01, 2022 10:28 am There is an universal accepted way to move within a program: Arrow keys.
Oh boy i wish ppl would honor that instead going all idiotic with wasd.
it's not very comfortable to use that
User avatar
BrotSagtMist
Party member
Posts: 657
Joined: Fri Aug 06, 2021 10:30 pm

Re: What is a helpful way to think about input? love.keypressed or love.keyboard.isDown?

Post by BrotSagtMist »

And since when is your comfort zone the standard for the rest of the world?

I do have two sets of arrow keys on my keyboard, one is where wasd used to be. I hate that position tho, i prefer moving using the right hand. Besides Löve/SDL ignores that position anyway.

If you look up at my first post, i proposed a keylist, so you are supposed to have several keys for the same action. That way everyone should be pleased.
obey
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: What is a helpful way to think about input? love.keypressed or love.keyboard.isDown?

Post by GVovkiv »

BrotSagtMist wrote: Tue Mar 01, 2022 12:18 pm And since when is your comfort zone the standard for the rest of the world?
yes
alejandroalzate
Citizen
Posts: 67
Joined: Sat May 08, 2021 9:45 pm

Re: What is a helpful way to think about input? love.keypressed or love.keyboard.isDown?

Post by alejandroalzate »

ReFreezed wrote: Tue Mar 01, 2022 9:49 am
zorg wrote: Tue Mar 01, 2022 5:31 am
ReFreezed wrote: Mon Feb 28, 2022 6:03 am
Only for controls. For keyboard shortcuts (like e.g. Ctrl+C to copy) keyconstants ought to be used.
Why?
Imagine if documentation for software had to have a different version for every keyboard layout. The QWERTY version would say "press C to copy", a Turkish version would say "Press V to copy", and the Dvorak version would say "press J to copy" (they all refer to the same physical key location, or scancode). Wouldn't that be pretty confusing? Using the position on the keyboard doesn't make sense here - it's the actual key that matters, unlike with controls (like using the keys physically located in the top left corner as movement keys).
for me it has is TINY (like 20ms i dunno) but is has
while love.keyPressed/Released is literally an IRQ call
you can try it for yourself;
taking a lot of tests of every key on a sigle love.update and seeing the fps dropping a little bit.
That is critical on reaction based games where the thing is not based on frame perfect thing but rather on delta based is more accurate
When the frames drop just a little it can screw up the thing
I feel love.keyPressed/Released more responsive.

Code: Select all

target = boardIndex.getWhosPeekingThisLine()
target:setObey(true)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], slime and 5 guests