[SOLVED] Key rebinding issue

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
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

[SOLVED] Key rebinding issue

Post by stout »

Code: Select all

function love.load()

	optbox = {
			x = 240,
			y = 70,
			xmin = 240,
			xmax = 490,
			ymin = 70,
			ymax = 270
			} -- ending optbox

	whichkey = "up" -- move these later! don't need to be global
	whichkeyindex = 1 -- move these later! don't need to be global

	rebinding = "no"
	rebindingState = "key"

	controlsGame = {
				key = {
					up = "up",
					down = "down",
					left = "left",
					right = "right",
					trick1 = "w",
					trick2 = "a",
					trick3 = "d",
					trick4 = "s",
					accept = "return",
					cancel = "backspace",
					pause = "escape"
						},

				button = {
					up = "---",
					down = "---",
					left = "---",
					right = "---",
					trick1 = "---",
					trick2 = "---",
					trick3 = "---",
					trick4 = "---",
					accept = "---",
					cancel = "---",
					pause = "---"
						},

				order = { "up", "down", "left", "right", "trick1", "trick2", "trick3", "trick4", "pause", "accept", "cancel" }
	} -- ending controlsGame

end -- ending love.load


function love.draw()
	love.graphics.printf("Controls", 0, 0, 640, "center")
	love.graphics.printf("Highlight and press B for rebind", 0, 20, 640, "center")


	love.graphics.print("Action",0,50)
	love.graphics.print("Key",250, 50)
	love.graphics.print("Button", 500, 50)

	love.graphics.print("Move up", 0, 70)
	love.graphics.print("Move down", 0, 90)
	love.graphics.print("Move left", 0, 110)
	love.graphics.print("Move right", 0, 130)
	love.graphics.print("Trick 1", 0, 150)
	love.graphics.print("Trick 2", 0, 170)
	love.graphics.print("Trick 3", 0, 190)
	love.graphics.print("Trick 4", 0, 210)
	love.graphics.print("Pause", 0, 230)
	love.graphics.print("Accept", 0, 250)
	love.graphics.print("Cancel", 0, 270)

	love.graphics.print(controlsGame.key.up, 250, 70)
	love.graphics.print(controlsGame.key.down, 250, 90)
	love.graphics.print(controlsGame.key.left, 250, 110)
	love.graphics.print(controlsGame.key.right, 250, 130)
	love.graphics.print(controlsGame.key.trick1, 250, 150)
	love.graphics.print(controlsGame.key.trick2, 250, 170)
	love.graphics.print(controlsGame.key.trick3, 250, 190)
	love.graphics.print(controlsGame.key.trick4, 250, 210)
	love.graphics.print(controlsGame.key.pause, 250, 230)
	love.graphics.print(controlsGame.key.accept, 250, 250)
	love.graphics.print(controlsGame.key.cancel, 250, 270)

	love.graphics.print(controlsGame.button.up, 500, 70)
	love.graphics.print(controlsGame.button.down, 500, 90)
	love.graphics.print(controlsGame.button.left, 500, 110)
	love.graphics.print(controlsGame.button.right, 500, 130)
	love.graphics.print(controlsGame.button.trick1, 500, 150)
	love.graphics.print(controlsGame.button.trick2, 500, 170)
	love.graphics.print(controlsGame.button.trick3, 500, 190)
	love.graphics.print(controlsGame.button.trick4, 500, 210)
	love.graphics.print(controlsGame.button.pause, 500, 230)
	love.graphics.print(controlsGame.button.accept, 500, 250)
	love.graphics.print(controlsGame.button.cancel, 500, 270)


	love.graphics.rectangle("line", optbox.x, optbox.y, 100, 15)

	if rebinding == "yes" then
		love.graphics.setColor(255, 0, 0)
		love.graphics.rectangle("fill", 320, 240, 100, 25)
		love.graphics.setColor(255, 255, 255, 255)
		love.graphics.print("Press new " .. rebindingState .. ".", 322, 245)
	end

	love.graphics.print(whichkeyindex,0,0) -- debug
	love.graphics.print(whichkey,0, 10) -- debug


end -- ending love.draw

function love.keypressed(key)

	if rebinding == "no" then
		if key == "escape" then
			love.event.quit()
		end

		if key == controlsGame.key.up then
			if optbox.y > optbox.ymin then
				optbox.y = optbox.y - 20
				whichkeyindex = whichkeyindex - 1
				whichkey = controlsGame["order"][whichkeyindex]
			end
		end

		if key == controlsGame.key.down then
			if optbox.y < optbox.ymax then
				optbox.y = optbox.y + 20
				whichkeyindex = whichkeyindex + 1
				whichkey = controlsGame["order"][whichkeyindex]
			end
		end

		if key == controlsGame.key.left then
			if optbox.x > optbox.xmin then
				optbox.x = optbox.x - 250
			end
		end

		if key == controlsGame.key.right then
			if optbox.x < optbox.xmax then
				optbox.x = optbox.x + 250
			end
		end

		if key == "b" then
			if optbox.x == 240 then
				rebindingState = "key"
			elseif optbox.x == 490 then
				rebindingState = "button"
			end
			rebinding = "yes"
		end
	end

	if rebinding == "yes" then
		if key == "escape" then
			rebinding = "no"
		elseif rebindingState == "key" then
			local newkey = key
			controlsGame["key"][whichkey] = newkey
			rebinding = "no"
		end
	end -- ending rebinding	

end -- ending love.keypressed
When you press 'b', it should bring up a box and await new input. It doesn't do this. Pressing 'b' sets whatever the current whichkey as 'b'. I get why it's happening - it's the newkey = key line - but why doesn't it wait for new input? This is happening whether or not I have setKeyRepeat to true or false.

(also I know the display is unoptimized, I figured out how I wanted to do that after I got through all of this ;)
Last edited by stout on Sun Jan 05, 2014 5:26 pm, edited 1 time in total.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Key rebinding issue

Post by Lafolie »

love.keypressed is called whenever a key is pressed. It's not something that loops or runs every frame unless you tell it to. You'd want to have a separate condition to check that the next keypress is going to be stored, so when you press 'b' set a flag so that the next keypress is the one that's stored.

Speaking of which, your code is a little weird. You can use a boolean (true, false / nil [technically not a bool but works]) instead of strings "yes" and "no". You can also concatenate strings with the '..' operator instead of having a gazillion statements inside your draw function (and having to work out the co-ords to print them at).
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: Key rebinding issue

Post by stout »

Lafolie wrote:love.keypressed is called whenever a key is pressed. It's not something that loops or runs every frame unless you tell it to. You'd want to have a separate condition to check that the next keypress is going to be stored, so when you press 'b' set a flag so that the next keypress is the one that's stored.
Which is what I think it should be doing right now, right?

1) user presses 'b'
2) check to see which column we're in
3) set rebinding and rebindingState variables depending on where the highlight box is
4) draw a box on the screen and wait for new input
5) set new input to appropriate key/value in the appropriate table

But instead it goes from 3 to 5, and skips step 4. I know that step 4 works, because that pops up correctly if you press 'b' in the second column. How do you mean about setting a new flag? How is that different than what I have set up with rebinding and rebindingState?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Key rebinding issue

Post by Robin »

Okay, here's the issue:

Code: Select all

	if rebinding == "no" then
			[...]
		if key == "b" then
			if optbox.x == 240 then
				rebindingState = "key"
			elseif optbox.x == 490 then
				rebindingState = "button"
			end
			rebinding = "yes"
		end
	end

	if rebinding == "yes" then
		if key == "escape" then
			rebinding = "no"
		elseif rebindingState == "key" then
			local newkey = key
			controlsGame["key"][whichkey] = newkey
			rebinding = "no"
		end
	end -- ending rebinding	
So if you press "b", rebinding was "no", which is then changed to "yes". Then you check rebinding again, and surprise! it's now yes, so it rebinds "b".

Solution: change:

Code: Select all

	end

	if rebinding == "yes" then
into

Code: Select all

	else
Help us help you: attach a .love.
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: Key rebinding issue

Post by stout »

Thanks! I'm still not 100% on why it's happening that way but I can poke at it more 'til I understand it.
Post Reply

Who is online

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