How to See if a key is down

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
TheP3
Citizen
Posts: 62
Joined: Tue Dec 20, 2011 9:20 pm

How to See if a key is down

Post 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
I write rules for my computer... I'm not the ruler of the world(yet)... Also, I livestream stuff http://NateAGeek.com/Live
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How to See if a key is down

Post by MarekkPie »

LOL I literally was typing the EXACT same question while you were typing yours.
User avatar
TheP3
Citizen
Posts: 62
Joined: Tue Dec 20, 2011 9:20 pm

Re: How to See if a key is down

Post by TheP3 »

LOL... I have to make an input field...
I write rules for my computer... I'm not the ruler of the world(yet)... Also, I livestream stuff http://NateAGeek.com/Live
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: How to See if a key is down

Post 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!
Kurosuke needs beta testers
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How to See if a key is down

Post 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.
User avatar
TheP3
Citizen
Posts: 62
Joined: Tue Dec 20, 2011 9:20 pm

Re: How to See if a key is down

Post 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! :ultrahappy:
I write rules for my computer... I'm not the ruler of the world(yet)... Also, I livestream stuff http://NateAGeek.com/Live
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: How to See if a key is down

Post by josefnpat »

Maybe I didn't read this thread properly, but don't you want love.keyboard.isDown( key ) ?
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: How to See if a key is down

Post by MarekkPie »

josefnpat wrote:Maybe I didn't read this thread properly, but don't you want love.keyboard.isDown( key ) ?
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.
User avatar
ston
Prole
Posts: 21
Joined: Fri Jan 27, 2012 9:53 am
Location: Holt, a small village in Wiltshire. We have cider!

Re: How to See if a key is down

Post 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 :-)
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: How to See if a key is down

Post by thelinx »

That is a bad solution for text input boxes.
You should use the second argument to keypressed, unicode.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests