Get all keys of KeyConstant ?

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.
vincentg
Prole
Posts: 15
Joined: Wed Apr 18, 2018 3:26 pm

Get all keys of KeyConstant ?

Post by vincentg »

Hello,
Is there a way to get all the keys from KeyConstant while running love?

I would like to avoid using love.keypressed but check all the keys through something like:

Code: Select all

for _, key in ipairs(getAllKeyConstant()) do
    if love.keyboard.isDown(key) then
        print(key)
    end
Is there already a function for that?
User avatar
steVeRoll
Party member
Posts: 140
Joined: Sun Feb 14, 2016 1:13 pm

Re: Get all keys of KeyConstant ?

Post by steVeRoll »

I don't see why you would need to check all of the keys. You can instead make a table with the keys you need to check, and iterate through that.
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Get all keys of KeyConstant ?

Post by pgimeno »

Why do you want to avoid keypressed/keyreleased? You can use them to update your own table so you can query all pressed keys, with the advantage of not needing to loop over each and every existing key. For example:

Code: Select all

local keys = {}
local scancodes = {}

function love.keypressed(k, s)
  keys[k] = true
  scancodes[s] = true
end

function love.keyreleased(k, s)
  keys[k] = nil
  scancodes[s] = nil
end

local function isDown(k)
  return keys[k] or false
end

local function isScancodeDown(s)
  return scancodes[s] or false
end

function love.update(dt)
  for k in pairs(keys) do
    if isDown(k) then -- this check is actually redundant because the table only contains pressed keys
       print(k)
    end
  end
end
vincentg
Prole
Posts: 15
Joined: Wed Apr 18, 2018 3:26 pm

Re: Get all keys of KeyConstant ?

Post by vincentg »

Thanks for your answer. I prefer to not use love.keypressed because I want to use it in a independant library. Using it twice or more per project doesn't seems possible without overriding it. Indeed I could store the table like pgimeno shows but it means that some 'logic' of the library would not be in the module itself. So my first idea was to iterate over all key constants inside the module and that way I could avoid the use of love.keypressed but that's maybe not a good idea?

How do you manage this kind of things?
User avatar
steVeRoll
Party member
Posts: 140
Joined: Sun Feb 14, 2016 1:13 pm

Re: Get all keys of KeyConstant ?

Post by steVeRoll »

You can't really avoid using love.keypressed for something like this. Even if you want to use an "independent library", you will eventually have to use love's callback functions. You can call the library's functions from within the callbacks, and the logic will still be inside the library.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Get all keys of KeyConstant ?

Post by zorg »

You don't necessarily have to use the keypressed/released callbacks at all, but you should realize that your current method of using love.keyboard.isDown(key) isn't in any way different behind the scenes performance-wise than the callback.

That said, your proposed method would probably perform worse, for the reasons steVeRoll said; it's pointless to iterate over every key; at worst, i'd iterate over the inputs i'd define for what the player would use as the game's controls.

...also, do use isScancodeDown instead, unless you want the ire of the people not using us-english keyboards. :3
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
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Get all keys of KeyConstant ?

Post by pgimeno »

Needing to call events is not uncommon, depending on the kind of library. See Gspöt for an example: https://notabug.org/pgimeno/Gspot/wiki/Implementation - note how the docs specify that you need to call the library's events yourself.
vincentg
Prole
Posts: 15
Joined: Wed Apr 18, 2018 3:26 pm

Re: Get all keys of KeyConstant ?

Post by vincentg »

I can show you exactly what I'm doing: https://github.com/vincentgires/lovegam ... einput.lua
This is a small wrapper around inputs coming from mouse, keybord or joysticks.
For the moment, we can set "actions" with

Code: Select all

input = Input:new()
input:bind_action('left', {device='keybord', value='space'})
input:bind_action('left', {device='joystick', number=1, event='hat', index=1, value='l'})
And use it in in game with

Code: Select all

if input:is_active('left') then
    -- is down
end
if input:is_pressed('left') then
    -- is pressed
end
if input:is_released('left') then
    -- is released
end
For the moment, the only callback that is needed is the love.update(). I don't use any pressed/released callback of love to have it very easy to deploy and use in multiple projects and I compute it in the module.
Something else I'd like to do is the listen() function I start to implement to listen which key/button/hat/axis is pressed in the context of setting the key from a menu or something like that. Right now, I did all the events of joysticks scanning all the buttons, axis, etc. Is that so bad? It doesn't seem too heavy, and even if it is, this is only for key configuration, so I'm not sure that is so wrong. What do you think? The only thing I didn't do yet is the keyboard. That's why I start this thread, because I didn't found a way to scan all keys inside the update/listen function without using love callbacks. But if you tell me that's a very bad idea, I should maybe not do it that way.

Other library exists like that one which seems good but look inside the main.lua: this is something I did want to avoid: all the callbacks are "sent" to the module file which makes the library not as convenient to use instead of just not having to do that.
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Get all keys of KeyConstant ?

Post by slime »

That approach will miss actions (presses and releases) which happen inside a single frame. It's not very common for that to happen when a game is running at 60hz constantly, but both frame drops and long frames due to loading etc. are a reality and then it becomes much more common (and your library can't control the framerate of the game).

Even when uncommon, it will be an issue that's hard to deal with because a few users will randomly experience it and think the game simply controls poorly, rather than thinking there's a real bug in the game's input handling.

And if you do discover that those user symptoms are caused by this issue, it might not be trivial to replace all your input action code with the correct implementation after the broken implementation has been used all over the place, so it's much better to start from a correct implementation even if it means your library is slightly less elegant.
vincentg
Prole
Posts: 15
Joined: Wed Apr 18, 2018 3:26 pm

Re: Get all keys of KeyConstant ?

Post by vincentg »

This is an important thing. Thanks to point it me out, I didn't realize this could happens.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 13 guests