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
Changing keyboard input into characters.
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Party member
- Posts: 101
- Joined: Mon Aug 11, 2008 5:19 am
Re: Changing keyboard input into characters.
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.
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 :
Best Regards,
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
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)
-
- Party member
- Posts: 101
- Joined: Mon Aug 11, 2008 5:19 am
Re: Changing keyboard input into characters.
Awesome. That gets me most of the way there.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.
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.
Mr. Strange wrote:Awesome. That gets me most of the way there.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.
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.
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.
It is.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?
Re: Changing keyboard input into characters.
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.
I didn't know that!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?
Will investigate and expose.
Re: Changing keyboard input into characters.
uh! investigate and expose. This sounds promising Personally, i couldn't get it to work myself, but maybe i was doing something wrong
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests