Disable key repeat?

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.
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Disable key repeat?

Post by Tjakka5 »

Sounds like a problems with your code, not with the limitations of the callbacks.
Working around your problems will only cause more problems in the long run, so I'd suggest you fix them.

Having multiple actions bound to one key is possible, but you must establish a priority.
That is, if you press up your priority is to enter a room. If that's not possible, you can jump.

Code: Select all

function player.keypressed(key)
  if key == "up" then
    if not player.tryEnterRoom() then
      if not player.tryJump() then
        -- If we aren't allowed to jump for whatever reason we may want to try a different action
      end
    end
  end
end
Besides that, you should not base your game's behavior around keypressed repeating. The delay between repeats can differ from os to os, and may even be customized in some environments.
If you want your player to be able to be constantly jumping while holding the up key, you should instead be using love.keyboard.isDown.

Last but not least, to fix your "defining multiple callbacks" problem, you want to propagate events if they are not satisfied.
For example, when you click on screen you first want to tell your UI code there was a keypress.
It will return true if it "used" it, or false if it didn't click on anything.
If it returns false you can then propagate it to the next system, and the next and the next.

Code: Select all

function love.mousepressed(...)
  if not menu.mousepressed(...) then
    if not player.mousepressed(...) then
      -- Etc
    end
  end
end
Again you're dealing with priorities here.


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

Re: Disable key repeat?

Post by raidho36 »

My input library (see signature) supports "wasDown" function which returns "true" or "false" if the key was depressed or released on the previous frame.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Disable key repeat?

Post by grump »

hasen wrote: Fri Feb 16, 2018 6:09 pm I can't have two love.keypressed functions at the same as one cancels out the other.
In my app framework, I'm using a multiplexing event dispatcher. There is only one place that receives all LÖVE events, as usual. Client code can subscribe to events, and the dispatcher notifies the subscribers of an event in the order they were registered. Each subscriber decides if it wants to consume the event or let it propagate further. Once it was consumed, propagation stops. There can be an unlimited number of subscribers for any kind of event.

It's an implementation of the so-called observer pattern.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Disable key repeat?

Post by hasen »

It's extremely strange to only have love.keyboard.isDown...where's isUp?? It's like having a boolean with only false as a choice.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Disable key repeat?

Post by zorg »

hasen wrote: Sat Feb 17, 2018 2:53 am It's extremely strange to only have love.keyboard.isDown...where's isUp?? It's like having a boolean with only false as a choice.
Yes, because you can't do something like

Code: Select all

if not love.keyboard.isDown(key) then end
right?

Boolean logic functions don't need unnecessary complement functions, although feel free to implement it yourself if your needs are so paramount :3

Code: Select all

function love.keyboard.isUp(...)
    return not love.keyboard.isDown(...)
end
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.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Disable key repeat?

Post by hasen »

By isUp I obviously meant key released. Detecting if the keyboard is not down would detect what happens the entire game apart from when the key is down...

I was just remarking that the other languages I used they all had several stage IF checks for the keyboard presses. So I found it strange that love2d was missing all that. It's like boolean only having false ie incomplete...not that it's literally like boolean.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Disable key repeat?

Post by raidho36 »

LÖVE mainly handles input via events, the "isDown" is for quick and dirty hacks mid-development and you shouldn't rely on it.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Disable key repeat?

Post by zorg »

hasen wrote: Sat Feb 17, 2018 11:49 am By isUp I obviously meant key released. Detecting if the keyboard is not down would detect what happens the entire game apart from when the key is down...

I was just remarking that the other languages I used they all had several stage IF checks for the keyboard presses. So I found it strange that love2d was missing all that. It's like boolean only having false ie incomplete...not that it's literally like boolean.
Yeah, you use keypressed and keyreleased for that, as was stated before. Or you whip up your own solution, as was also stated before.
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.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Disable key repeat?

Post by hasen »

zorg wrote: Sat Feb 17, 2018 1:05 pm Yeah, you use keypressed and keyreleased for that, as was stated before. Or you whip up your own solution, as was also stated before.
Yes you have to use a function which grabs all at once since there are no IF checks for anything other than for keyisdown. Or you can make it yourself like you say or use someone else's library. That's the great thing, you can always make up for stuff that's missing.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Disable key repeat?

Post by hasen »

raidho36 wrote: Sat Feb 17, 2018 12:05 pm LÖVE mainly handles input via events, the "isDown" is for quick and dirty hacks mid-development and you shouldn't rely on it.
I don't know, I see that the most in other people's finished source code. It's even used in Mari0 the most well known Love2d game.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 3 guests