Keyboard handling not working

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
SpeedySloth
Prole
Posts: 9
Joined: Thu Jul 19, 2018 7:52 pm

Keyboard handling not working

Post by SpeedySloth »

So I've spent the last few hours trying to get some sort of keyboard handling working, and I can't seem to find out why it's not. I didn't really want to post here, as I have another idea that I'm pretty sure will work, but I quite like the system I'm currently using, and would like to use it. Basically my problem is that I'm trying to use love.keypressed() to add any keys pressed to a table, and love.keyreleased() to remove keys, and then make another method that loops through that list when called, and returns whether a given key is pressed for that frame.

The problem is that somewhere along the line, that list is being cleared. I'm guessing this is to do with references (I'm directly adding key from love.keypressed(key) to the table, does it get nullified after the method ends?) If you want me to post any code, I will (But this post is more of a question of how to get around references to key), or if you want me to explain my issue more (I don't think this post has been very clear), I will. If you know how to get around these references (Or if it's actually possible), say, but don't spend ages looking for the answer. Thanks!
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Keyboard handling not working

Post by zorg »

Code: Select all

keystate = {}
function love.keypressed(key,scancode) keystate[scancode] = true end
function love.keyreleased(key, scancode) keystate[scancode] = false end
About the simplest state system one can write that works; not sure why you'd want to loop over all of them; usually you'd just want to query for a specific key (or key combination) but that's already doable with using love.keyboard.isScancodeDown in love.update or something (you can also use love.keyboard.isDown but scancodes should always be used unless you want grumpy non-americans and/or dvorak layout users to shout at you :3)

but yes, do post your code since from what i've read, you most certainly don't erase the contents of the table... unless you define it in love.update or keypressed or something, since in that case, it will always be cleared when those functions get called.
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: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Keyboard handling not working

Post by pgimeno »

What zorg said. I presume you're doing something along these lines:

Code: Select all

local keys = {}

local function down(k)
  for i = 1, #keys do
    if keys[i] == k then return i end
  end
  return false
end

function love.keypressed(k)
  if not down(k) then table.insert(keys, k) end
end

function love.keyreleased(k)
  local index = down(k)
  if index then table.remove(keys, index) end
end

function love.draw()
  love.graphics.print(table.concat(keys, "\t"))
end
but as you can see, it works without problems, so there's something else in your code that is somehow clearing the table. Anyway zorg's advice about using isScancodeDown is quite sound. And if you definitely want a table, then consider also his advice of using the keynames as indices instead of using the table as a list. The only advantage of a list is that it preserves the order in which the keys were pressed, while they are held, but that has seldom any use. Perhaps a fighting game with combos that require the keys being pressed in a specific order and held, or something like that.
SpeedySloth
Prole
Posts: 9
Joined: Thu Jul 19, 2018 7:52 pm

Re: Keyboard handling not working

Post by SpeedySloth »

Thanks for your response! I did consider a similar approach to what you posted, but I think I'll stick to what I've currently got, unless I spend ages on it.
zorg wrote: Tue Aug 07, 2018 4:08 pm but yes, do post your code since from what i've read, you most certainly don't erase the contents of the table... unless you define it in love.update or keypressed or something, since in that case, it will always be cleared when those functions get called.
I've attached a code file. Look through it if you can, but please don't spend ages looking for the problem.
Some of the code is over-engineered, but the general layout of the code should be fairly obvious. The main files to look in (I think, anyway) are world.lua (States/Gamestate/world.lua) and input.lua (Models/input.lua). Thanks for helping! :)

P.S. Don't get a heart attack when you see what's at the bottom of world.lua. I promise, I didn't type them all out manually, I made an auto-tiler, something I'm very proud of :)
Attachments
topdown1.love.zip
(16.95 KiB) Downloaded 58 times
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Keyboard handling not working

Post by pgimeno »

What makes you think that the table is being cleared? I see nothing wrong with the table. When pressing a second key while holding the first, it correctly prints the held key in love.keypressed (after disabling the print in input:update to not confuse both).

On a secondary note, the case in Sprites/Entities/player.lua is wrong for the require of Models/input. The 'i' of 'input' is in upper case.
SpeedySloth
Prole
Posts: 9
Joined: Thu Jul 19, 2018 7:52 pm

Re: Keyboard handling not working

Post by SpeedySloth »

pgimeno wrote: Tue Aug 07, 2018 11:28 pm On a secondary note, the case in Sprites/Entities/player.lua is wrong for the require of Models/input. The 'i' of 'input' is in upper case.
Yeah, it was that. I guess because I corrected a typo in that line already (There must've been 2), I didn't think myself stupid enough to have made another. Sorry for wasting your time :/ But thanks anyway!
Post Reply

Who is online

Users browsing this forum: _JM_ and 28 guests