Page 1 of 1

Accepting user strings through the keyboard

Posted: Thu Apr 29, 2010 9:43 pm
by Mr. Strange
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

Re: Accepting user strings through the keyboard

Posted: Thu Apr 29, 2010 10:13 pm
by bmelts
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:

Code: Select all

function love.keypressed(k, u)
   -- stuff
   tempString = tempString..string.char(u)
   -- more stuff
end
This should prevent lshift and its brethren from showing up, though as a disclaimer, I haven't tested the above recently.

Re: Accepting user strings through the keyboard

Posted: Thu Apr 29, 2010 11:25 pm
by Mr. Strange
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.

Re: Accepting user strings through the keyboard

Posted: Thu Apr 29, 2010 11:30 pm
by Mr. Strange
Indeed, the following code in keypressed(key, unicode) works fine.

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
Using the unicode gives me capitals and other good characters, so that's a nice improvement.

Re: Accepting user strings through the keyboard

Posted: Fri Apr 30, 2010 9:31 am
by bartbes
The null characters are easy, add this:

Code: Select all

if not u then return end

Re: Accepting user strings through the keyboard

Posted: Fri Apr 30, 2010 10:00 am
by Robin
no u

[Response]My Approach

Posted: Sat May 01, 2010 11:13 am
by rhezalouis
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.:megagrin:

Re: [Response]My Approach

Posted: Sat May 01, 2010 12:27 pm
by Robin
rhezalouis wrote:Oh, and I use a look-up table to handle the shifted characters.
That's not necessary anymore -- we have the unicode argument now, which works for every key and (IIRC) for different keyboard layouts as well.

Re: [Response]My Approach

Posted: Mon May 03, 2010 1:25 am
by rhezalouis
Robin wrote:
rhezalouis wrote:Oh, and I use a look-up table to handle the shifted characters.
That's not necessary anymore -- we have the unicode argument now, which works for every key and (IIRC) for different keyboard layouts as well.
Ow, sorry, I am still working in 0.6.0 and haven't read the new features. Waa, I really have to catch up. ^^

Re: [Response]My Approach

Posted: Mon May 03, 2010 8:04 am
by Robin
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. ^^
The unicode argument was added in 0.6.0, so unless you meant you are still working in 0.5.0...