love.keyreleased multiple keys

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.
Post Reply
destinyschilde
Prole
Posts: 7
Joined: Mon Jan 16, 2017 11:48 pm

love.keyreleased multiple keys

Post by destinyschilde »

I have this problem. I'm trying trigger my diagonal idle animations when when two keys are released ('s' and 'd') but this does not seem to work. I could not seem to find an answer on these forums. This is what i have:

Code: Select all

function love.keyreleased (key)
    if key == "s" and key =="d" then
        player.curr_anim = player.sprite.animations_names[1]
        player.curr_frame = 1
    end
end
(sorry about my weird variables. just know its loading a different animation from an array and it works for all the single keys)

Thanks!
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: love.keyreleased multiple keys

Post by raidho36 »

Hi and welcome to the forum.

First of all, consider using scancodes instead of key characters. Former are universal on all keyboards to match standard latin layout, latter are locale-dependent. 2nd argument to the keyreleased function is its scancode.

Second of all, one variable can only contain one value, so key variable can't simultaneously be "s" and "d" so it never would have worked. This is why you need to handle key strokes one at a time. Additionally, events are activated one at a time, so you can't catch simultaneous keystrokes with this anyhow.

Third of all, you probably don't want to detect if keystrokes happen simultaneously because computers have high temporal resolution, what to you look simultaneous, to computer it looks like there's gaping void in between. You may simply end up in a situation where you can't press or release keys in close enough succession to trigger diagonal logic, it'll instead be just cardinal directions. Again, nothing is truly simultaneous because things are processed one at a time - even if they're extremely close. You'll have to use some additional logic to keep animation angle diagonal.

Lastly, if you want to implement such logic, you probably should do that in update function rather than in event handler.
User avatar
yetneverdone
Party member
Posts: 448
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: love.keyreleased multiple keys

Post by yetneverdone »

raidho36 wrote:Hi and welcome to the forum.

First of all, consider using scancodes instead of key characters. Former are universal on all keyboards to match standard latin layout, latter are locale-dependent. 2nd argument to the keyreleased function is its scancode.

Second of all, one variable can only contain one value, so key variable can't simultaneously be "s" and "d" so it never would have worked. This is why you need to handle key strokes one at a time. Additionally, events are activated one at a time, so you can't catch simultaneous keystrokes with this anyhow.

Third of all, you probably don't want to detect if keystrokes happen simultaneously because computers have high temporal resolution, what to you look simultaneous, to computer it looks like there's gaping void in between. You may simply end up in a situation where you can't press or release keys in close enough succession to trigger diagonal logic, it'll instead be just cardinal directions. Again, nothing is truly simultaneous because things are processed one at a time - even if they're extremely close. You'll have to use some additional logic to keep animation angle diagonal.

Lastly, if you want to implement such logic, you probably should do that in update function rather than in event handler.

Thank you, that also helped me out. I've checked the love2d wiki for scancodes, but it doesnt show any scancode in particular, could you give or cite an example of scancode? like A,D ?
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.keyreleased multiple keys

Post by zorg »

[wiki]Scancode[/wiki]
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.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests