Additional love.keyboard, love.mouse, love.joystick function suggestions.

Issue #121 resolved
Kosie van der Merwe
created an issue

isAnyDown() returns whether any key/button is currently down.

getDownKeys/Buttons() returns a table(array) of keys/buttons that are currently down.

The fact that key/button release events aren't received if the window loses focus, could be problematic.

Comments (16)

  1. Robin Wellner

    You could, as a work-around, use

    anyDown = 0
    function love.keypressed(key,u)
        anyDown = anyDown + 1
    function love.keyreleased(key,u)
        anyDown = anyDown - 1

    Then you can check for anyDown > 0.

    But it's hackish.

  2. Boolsheet
    • changed status to open

    The isDown change to the joystick module needs to handle users being stupid and passing negative buttons. SDL_JoystickGetButton(joystick, button) does not like it at all if button is negative. love.joystick.isDown(0, -2) makes it go kabluie.

    Also, how about using buttonList[0] for the count of buttons inside the array instead of the ending-indicating -1?

  3. Gabe Stilez

    @Marco Trosi: You can use love.keypressed, love.keyreleased, and a table, to keep track of what keys (either based on keyconstants or scancodes, the latter being more portable) have been pressed;

    If you index the table numerically, then you can do something like if #heldKeys > 0 then --[[...]] end if you don't care what keys were pressed, otherwise you'll need to iterate through the table, and do stuff based on the stored keyconstants/scancodes.

    Having said kc/sc be the keys in the table could also work, depending on your use-cases. Also, this way, you can also store more stuff in the table for other functionality, like how long was a key held down, etc.; granting much more power than it was implemented behind the scenes.

  4. Marco Trosi

    I know. This always the proposed solution. But thanks for helping me out. I appreciate it very very much. =D

    For me it was just a matter of convenience and consistency. That is why I gave my also my comment just in case the Löve2D developers need some more reasons to introduce such a feature. So this was my desperate attempt to avoid a workaround. ;-) You know, just because there is a way of doing things doesn't mean that there is no better way.

  5. Gabe Stilez

    True, but i do find that this way is way more expandable than anything in-built. In other words, if one wants any extra functionality with detected (and stored) keypresses (or even mousepresses or anything similar even), then they would still need to do the same thing, doing it themselves, regardless of löve exposing the basic functionality for it, since it wouldn't be expandable. :)

  6. Don Miguel

    I need another possibility: to know the time "how long the key is down", "how many seconds have passes from the last button release" etc.

    now I have to poll all the inputs and calc such data with Lua scripts.

  7. Bart van Strien

    Yes, you can use love.keypressed/keyreleased to do just that. This would take at least as much work (if not more) within love, and would then be enabled for every game, regardless of whether it wants that information or not.

  8. hahawoo

    I think isDown() returning whether any key/button is down would be useful.

    I'm liking isDown with no arguments more than isAnyDown, because no arguments meaning "everything" is used for (i.e. there isn't etc.).

    A function returning a table of down keys/buttons doesn't seem so useful to me, because I'm not sure when it would be preferable over isDown.

    For what it's worth, I discovered that I wanted isDown() when making a simple program which used LOVE's callbacks for input and I didn't care about which mouse button was pressed so I wasn't checking for it, but then I wanted to do something using isDown and suddenly I had to think about specifying mouse buttons when I would have preferred to just type isDown(). So in the sense of "caring about which key/button is down is optional" sense, I think isDown() is consistent with the callbacks.

  9. Log in to comment