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 » Wed Nov 26, 2008 7:53 am

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: 1051
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Changing keyboard input into characters.

Post by rude » Wed Nov 26, 2008 10:43 am

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 » Wed Nov 26, 2008 12:39 pm

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 139 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 » Wed Nov 26, 2008 4:52 pm

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 » Wed Nov 26, 2008 5:25 pm

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 » Fri Nov 28, 2008 4:45 am

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 » Fri Nov 28, 2008 3:38 pm

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 » Sat Nov 29, 2008 1:59 pm

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: 1051
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Changing keyboard input into characters.

Post by rude » Sat Nov 29, 2008 3:07 pm

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_ » Sat Nov 29, 2008 6:34 pm

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: bobbymcbobface, YounYokel and 4 guests