Multiple keys as one command

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
lolsasory
Prole
Posts: 21
Joined: Mon Nov 19, 2012 5:27 am

Multiple keys as one command

Post 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?
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Multiple keys as one command

Post 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...
Currently designing themes for WordPress.

Sometimes lurks around the forum.
lolsasory
Prole
Posts: 21
Joined: Mon Nov 19, 2012 5:27 am

Re: Multiple keys as one command

Post 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!:)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Multiple keys as one command

Post by Robin »

Also, love.keyboard.isDown allows this:

Code: Select all

if blaa blaa and love.keyboard.isDown("return", "kpenter") then
    blaa blaa
end
Help us help you: attach a .love.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Multiple keys as one command

Post 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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Multiple keys as one command

Post by bartbes »

Because the capitals aren't buttons.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Multiple keys as one command

Post 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.
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: Multiple keys as one command

Post 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
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Multiple keys as one command

Post 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?
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests