Page 1 of 1

love.keyreleased multiple keys

Posted: Mon Jan 16, 2017 11:58 pm
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!

Re: love.keyreleased multiple keys

Posted: Tue Jan 17, 2017 12:27 am
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.

Re: love.keyreleased multiple keys

Posted: Tue Jan 17, 2017 4:34 am
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 ?

Re: love.keyreleased multiple keys

Posted: Tue Jan 17, 2017 4:40 am
by zorg
[wiki]Scancode[/wiki]