I want my players to name things and save them to a file. I've got saving and loading working, and naming works somewhat. I have the following:
if(key == "backspace" or key == "delete") then
if(string.len(tempString) > 0) then
tempString = string.sub(tempString,1,string.len(tempString) - 1)
elseif(string.len(tempString) < 12) then
tempString = tempString..key
else
end
This works pretty well, but if players press lshift, or tab, or some non-character key they get "lshift" (or whatever) entered into the text box. Obviously this is suboptimal.
Is there a better way to have players enter strings? Is there some functionality I'm not applying? Or is there an easy filter for "that key is a character" which I'm not using?
--Mr. Strange
Accepting user strings through the keyboard
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: Accepting user strings through the keyboard
I'm assuming you're doing this in the love.keypressed() callback?
In the most recent versions of LÖVE, there is a second argument that gets passed to love.keypressed - love.keypressed(k, u), where u is the Unicode codepoint of the key that was pressed. You can then use Lua's built-in function string.char(b) to produce the printable version of that key. So it'd look something like the following:
This should prevent lshift and its brethren from showing up, though as a disclaimer, I haven't tested the above recently.
In the most recent versions of LÖVE, there is a second argument that gets passed to love.keypressed - love.keypressed(k, u), where u is the Unicode codepoint of the key that was pressed. You can then use Lua's built-in function string.char(b) to produce the printable version of that key. So it'd look something like the following:
Code: Select all
function love.keypressed(k, u)
-- stuff
tempString = tempString..string.char(u)
-- more stuff
end
-
- Party member
- Posts: 101
- Joined: Mon Aug 11, 2008 5:19 am
Re: Accepting user strings through the keyboard
Hmmm... doesn't really work. For some characters (like tab) I get a box, while others (lshift, ctrl) simply seem to provide a null character, which love then gets confused about, and prints nothing after. I can still delete characters (as per the first part of the if) and get back to a good state, but it's not what I want.
Hmmm - maybe I should add a check like this:
if(string.len(string.char(key)) > 1) then
-- don't append the character
else
-- do append.
end
That will probably work, though it's not especially elegant.
Hmmm - maybe I should add a check like this:
if(string.len(string.char(key)) > 1) then
-- don't append the character
else
-- do append.
end
That will probably work, though it's not especially elegant.
-
- Party member
- Posts: 101
- Joined: Mon Aug 11, 2008 5:19 am
Re: Accepting user strings through the keyboard
Indeed, the following code in keypressed(key, unicode) works fine.
Using the unicode gives me capitals and other good characters, so that's a nice improvement.
Code: Select all
if(key == "backspace" or key == "delete") then
if(string.len(tempString) > 0) then
tempString = string.sub(tempString,1,string.len(tempString) - 1)
end
elseif((string.len(tempString) < 12) and (string.len(key) == 1))then
tempString = tempString..string.char(unicode)
else
end
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Accepting user strings through the keyboard
The null characters are easy, add this:
Code: Select all
if not u then return end
- rhezalouis
- Party member
- Posts: 100
- Joined: Mon Dec 07, 2009 10:27 am
- Location: Indonesia
- Contact:
[Response]My Approach
I check the length of the key like you have mentioned. I think that's quite nice enough. Oh, and I use a look-up table to handle the shifted characters. You could see my code in the programme posted here: [RLC]Data Execution Prevention?.
I hope that's helpful.
I hope that's helpful.
Aargh, I am wasting my posts! My citizenshiiiip...
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: [Response]My Approach
That's not necessary anymore -- we have the unicode argument now, which works for every key and (IIRC) for different keyboard layouts as well.rhezalouis wrote:Oh, and I use a look-up table to handle the shifted characters.
Help us help you: attach a .love.
- rhezalouis
- Party member
- Posts: 100
- Joined: Mon Dec 07, 2009 10:27 am
- Location: Indonesia
- Contact:
Re: [Response]My Approach
Ow, sorry, I am still working in 0.6.0 and haven't read the new features. Waa, I really have to catch up.Robin wrote:That's not necessary anymore -- we have the unicode argument now, which works for every key and (IIRC) for different keyboard layouts as well.rhezalouis wrote:Oh, and I use a look-up table to handle the shifted characters.
Aargh, I am wasting my posts! My citizenshiiiip...
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: [Response]My Approach
The unicode argument was added in 0.6.0, so unless you meant you are still working in 0.5.0...rhezalouis wrote:Ow, sorry, I am still working in 0.6.0 and haven't read the new features. Waa, I really have to catch up.
Help us help you: attach a .love.
Who is online
Users browsing this forum: Ahrefs [Bot] and 4 guests