Page 1 of 1

Multiple keys as one command

Posted: Tue Nov 20, 2012 3:53 am
by lolsasory
is there a way to do something like love.keyboard.isDown("return") love.keyboard.isDown("kpenter") have the same command
ive tried

Code: Select all

 if blaa blaa and
love.keyboard.isDown("return") or
love.keyboard.isDown("kpenter") then
blaa blaa
end
but that doesnt work

i also want to do it so W and the up arrow do the same thing
any thoughts?

Re: Multiple keys as one command

Posted: Tue Nov 20, 2012 4:15 am
by substitute541
lolsasory wrote:is there a way to do something like love.keyboard.isDown("return") love.keyboard.isDown("kpenter") have the same command
ive tried

Code: Select all

 if blaa blaa and
love.keyboard.isDown("return") or
love.keyboard.isDown("kpenter") then
blaa blaa
end
but that doesnt work
You should have done this :

Code: Select all

if abooleanvalue and (keyDown1 or keyDown2) then
    makecake
end
Don't forget the parentheses. They group it all together.
Replace those random names with the real statements...

Re: Multiple keys as one command

Posted: Tue Nov 20, 2012 5:00 am
by lolsasory
You should have done this :

Code: Select all

if abooleanvalue and (keyDown1 or keyDown2) then
    makecake
end
Don't forget the parentheses. They group it all together.
Replace those random names with the real statements...[/quote]

Thanks works!:)

Re: Multiple keys as one command

Posted: Tue Nov 20, 2012 4:19 pm
by Robin
Also, love.keyboard.isDown allows this:

Code: Select all

if blaa blaa and love.keyboard.isDown("return", "kpenter") then
    blaa blaa
end

Re: Multiple keys as one command

Posted: Fri Nov 30, 2012 3:35 pm
by Ref
Question: why aren't capital letters supported?
Seems rather clunky to have to do:

Code: Select all

if ky.isDown('lshift','rshift') and ky.isDown('z') then zoom = zoom + dt end
if ky.isDown( 'z' ) and not ky.isDown('lshift','rshift') then zoom = zoom > 0.1  and zoom - dt or dt end
Is there a better alternative?

Re: Multiple keys as one command

Posted: Fri Nov 30, 2012 3:59 pm
by bartbes
Because the capitals aren't buttons.

Re: Multiple keys as one command

Posted: Fri Nov 30, 2012 7:12 pm
by Inny
In the love.keypressed handler, the two parameters it receives, k and u, correspond to the Key that was pressed to create the event (meaning what you'd check with love.keyboard.isDown), and the Unicode codepoint that matches. So, if they were holding left-shift, and pressed z, you get love.keypressed("z", 90). Lowercase z for the key pressed, and 90 for the unicode codepoint for upppercase Z. When you run the unicode codepoint through string.char, for the first 126 codepoints, you get the corresponding english character/number/punctuation. This is handy for english keyboard input. However, it doesn't work with anything above 126, as Lua lacks unicode support.

Re: Multiple keys as one command

Posted: Fri Nov 30, 2012 8:11 pm
by Przemator
Inny wrote:This is handy for english keyboard input. However, it doesn't work with anything above 126, as Lua lacks unicode support.
You can convert the unicode into string, you can try a function, like this:

Code: Select all

function love.load()
	tab = {}
	txt = ""
	font = love.graphics.newFont("consola.ttf", 20)
	love.graphics.setFont(font)
end

function love.draw()
	love.graphics.print(txt, 0, 0)
end

function love.keypressed(key, code)
	table.insert(tab, code)
	txt = conv2utf8(tab)
end

function conv2utf8(unicode_list)
	local result = ""
	local w,x,y,z = 0,0,0,0
	for i,v in ipairs(unicode_list) do
		if v ~= 0 and v ~= nil then
			if v <= 0x7F then -- same as ASCII
				result = result .. string.char(v)
			elseif v >= 0x80 and v <= 0x7FF then -- 2 bytes
				y = math.floor((v % 0x000800) / 64)
				z = v % 0x000040
				result = result .. string.char(0xC0 + y, 0x80 + z)
			elseif (v >= 0x800 and v <= 0xD7FF) or (v >= 0xE000 and v <= 0xFFFF) then -- 3 bytes
				x = math.floor((v % 0x010000) / 4096)
				y = math.floor((v % 0x001000) / 64)
				z = v % 0x000040
				result = result .. string.char(0xE0 + x, 0x80 + y, 0x80 + z)
			elseif (v >= 0x10000 and v <= 0x10FFFF) then -- 4 bytes
				w = math.floor((v % 0x200000) / 262144)
				x = math.floor((v % 0x040000) / 4096)
				y = math.floor((v % 0x001000) / 64)
				z = v % 0x000040
				result = result .. string.char(0xF0 + w, 0x80 + x, 0x80 + y, 0x80 + z)
			end
		end
	end
	return result
end

Re: Multiple keys as one command

Posted: Sat Dec 01, 2012 6:11 am
by Inny
Przemator wrote:You can convert the unicode into string, you can try a function, like this: ...
That looks like an awesome candidate for https://love2d.org/wiki/Category:Snippets , Care to put it up there?