Page 1 of 2

Changing keyboard input into characters.

Posted: Wed Nov 26, 2008 7:53 am
by Mr. Strange
It's probably late, but for some reason I'm unable to grab the key pressed as a character, rather than a character code. For example:

function keypressed(key)

string = string..key

end

function draw()
love.graphics.print(string, 100,100)
end

Just gives me "112" when I press "p". I hope I'm missing something obvious.

--Mr. Strange

Re: Changing keyboard input into characters.

Posted: Wed Nov 26, 2008 10:43 am
by rude
The value passed to keypressed is a virtual key code (i.e. an integer). Appending it to a string will not convert it into the correct character. The function string.char will, however.

Re: Changing keyboard input into characters.

Posted: Wed Nov 26, 2008 12:39 pm
by TsT
I have made a small lib that remap (copy) the love input constants to allow to work easilly with them.

I'm using it like that :

Code: Select all

love.filesystem.require("lib/love.const.lua")

function keypressed( key )
    print(string.format("key %s pressed (code %d)", loveKeycodeToKeyname(key, "key"), key))
end
love.const.zip
(r292) v0.1.0 20081025
(13.16 KiB) Downloaded 298 times
Best Regards,

Re: Changing keyboard input into characters.

Posted: Wed Nov 26, 2008 4:52 pm
by Mr. Strange
rude wrote:The value passed to keypressed is a virtual key code (i.e. an integer). Appending it to a string will not convert it into the correct character. The function string.char will, however.
Awesome. That gets me most of the way there.

Unfortunately, I can't simply call string.char(key) on everything, because some valid keys (Ctrl, Shift) don't have characters associated with them, which causes an exception. What's the cleanest way to filter out the non-character keys?

--Mr. Strange

Re: Changing keyboard input into characters.

Posted: Wed Nov 26, 2008 5:25 pm
by Kaze
Mr. Strange wrote:
rude wrote:The value passed to keypressed is a virtual key code (i.e. an integer). Appending it to a string will not convert it into the correct character. The function string.char will, however.
Awesome. That gets me most of the way there.

Unfortunately, I can't simply call string.char(key) on everything, because some valid keys (Ctrl, Shift) don't have characters associated with them, which causes an exception. What's the cleanest way to filter out the non-character keys?

--Mr. Strange

Code: Select all

	if ( Key >= 33 and Key <= 122 ) then
		local char = string.char( Key )
		if ( love.keyboard.isDown( love.key_lshift ) or love.keyboard.isDown( love.key_rshift ) ) then
			char = string.upper( char )
		end
	end

Re: Changing keyboard input into characters.

Posted: Fri Nov 28, 2008 4:45 am
by Lord Tim
What about stuff like parenthesis? I mean you could go through and do an if statement for each key, but surely it's not that bad, is it?

Re: Changing keyboard input into characters.

Posted: Fri Nov 28, 2008 3:38 pm
by Kaze
Lord Tim wrote:What about stuff like parenthesis? I mean you could go through and do an if statement for each key, but surely it's not that bad, is it?
It is.

Re: Changing keyboard input into characters.

Posted: Sat Nov 29, 2008 1:59 pm
by muku
Translating key codes into characters directly is a bad idea because it doesn't take the user's keyboard layout into account; international users are out of luck, for example. SDL actually provides automatic translation of key hits into Unicode characters, but I guess Löve doesn't expose that functionality?

Re: Changing keyboard input into characters.

Posted: Sat Nov 29, 2008 3:07 pm
by rude
muku wrote:Translating key codes into characters directly is a bad idea because it doesn't take the user's keyboard layout into account; international users are out of luck, for example. SDL actually provides automatic translation of key hits into Unicode characters, but I guess Löve doesn't expose that functionality?
I didn't know that! :shock:

Will investigate and expose.

Re: Changing keyboard input into characters.

Posted: Sat Nov 29, 2008 6:34 pm
by u9_
uh! investigate and expose. This sounds promising :) Personally, i couldn't get it to work myself, but maybe i was doing something wrong :o