Changing keyboard input into characters.

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.
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

Changing keyboard input into characters.

Post 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
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Changing keyboard input into characters.

Post 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.
User avatar
TsT
Party member
Posts: 161
Joined: Thu Sep 25, 2008 7:04 pm
Location: France
Contact:

Re: Changing keyboard input into characters.

Post 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 270 times
Best Regards,
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

Re: Changing keyboard input into characters.

Post 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
User avatar
Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

Re: Changing keyboard input into characters.

Post 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
User avatar
Lord Tim
Prole
Posts: 30
Joined: Sun Jul 20, 2008 4:07 am

Re: Changing keyboard input into characters.

Post 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?
User avatar
Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

Re: Changing keyboard input into characters.

Post 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.
User avatar
muku
Prole
Posts: 18
Joined: Wed Aug 20, 2008 11:35 am

Re: Changing keyboard input into characters.

Post 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?
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Changing keyboard input into characters.

Post 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.
u9_
Citizen
Posts: 54
Joined: Thu Oct 23, 2008 7:12 am

Re: Changing keyboard input into characters.

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

Who is online

Users browsing this forum: Google [Bot] and 47 guests