Page 1 of 2
How to See if a key is down
Posted: Mon Feb 13, 2012 12:56 am
by TheP3
I want to see what key is being pressed. Without having to check isDown("a" || "b"...). Does love have some kind of function for that? Thx
Re: How to See if a key is down
Posted: Mon Feb 13, 2012 1:01 am
by MarekkPie
LOL I literally was typing the EXACT same question while you were typing yours.
Re: How to See if a key is down
Posted: Mon Feb 13, 2012 1:04 am
by TheP3
LOL... I have to make an input field...
Re: How to See if a key is down
Posted: Mon Feb 13, 2012 1:08 am
by tentus
I don't really get the question. You want to know what is pressed, but not what is down. In my mind, the two are synonymous.
Now, in regards to making a input field: use
love.keypressed to get input. if it's a valid character, add it to a string. If not, do other things with it (for example, backspace would remove chars using string.sub). Profit!
Re: How to See if a key is down
Posted: Mon Feb 13, 2012 1:09 am
by MarekkPie
For your situation, I think there is a snippet in love.keypressed() for inputting text:
https://love2d.org/wiki/love.keypressed
And since its game-specific, I doubt you'd have a problem with just following that. Mine, on the other hand, has a few more issues to handle.
Re: How to See if a key is down
Posted: Mon Feb 13, 2012 1:12 am
by TheP3
tentus wrote:I don't really get the question. You want to know what is pressed, but not what is down. In my mind, the two are synonymous.
Now, in regards to making a input field: use
love.keypressed to get input. if it's a valid character, add it to a string. If not, do other things with it (for example, backspace would remove chars using string.sub). Profit!
Thanks!
Re: How to See if a key is down
Posted: Mon Feb 13, 2012 4:19 pm
by josefnpat
Maybe I didn't read this thread properly, but don't you want
love.keyboard.isDown( key ) ?
Re: How to See if a key is down
Posted: Mon Feb 13, 2012 4:44 pm
by MarekkPie
You've got the order a bit wrong. For love.keyboard.isDown(k), you already know what key you want to check, and the return value is whether or not that specific key is pressed. It does not TELL you which key is pressed. You could supposedly use this to get it to work, but it is EXTREMELY bulky. Something like:
Code: Select all
if love.keyboard.isDown("a") then
-- do something
elseif love.keyboard.isDown("b") then
-- do something
elseif love.keyboard.isDown("c") then
-- ...
Doing what everyone here suggests gets rid of the tediousness of this approach. Just catch the key parameter from love.keypressed(key) and add it to a string.
Re: How to See if a key is down
Posted: Tue Feb 14, 2012 6:28 pm
by ston
It might be worth adding that if you need to check for modifiers (or more generally, more than any one key being pressed) at 'key event time' (i.e. when a key is pressed), then as far as I understand it, you'll need to add some 'is down?' checks to the keypressed() method, e.g. noddy code:
Code: Select all
function love.keypressed(key)
-- check for modifiers
CTRL_KEY = ""
SHIFT_KEY = ""
ALT_KEY = ""
if love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl") then
CTRL_KEY = "CTRL"
end
if love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") then
SHIFT_KEY = "SHIFT"
end
if love.keyboard.isDown("lalt") or love.keyboard.isDown("ralt") then
ALT_KEY = "ALT"
end
key_pressed = CTRL_KEY .. " " .. SHIFT_KEY .. " " .. ALT_KEY .. " " .. key
end
I'm just setting strings here, to illustrate the point (in .draw(), I was just printing out the 'key_pressed' string); you could set flags or take some other action as required or course :-)
Re: How to See if a key is down
Posted: Wed Feb 15, 2012 1:20 pm
by thelinx
That is a bad solution for text input boxes.
You should use the second argument to keypressed, unicode.