Accepting user strings through the keyboard

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

Accepting user strings through the keyboard

Post 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
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: Accepting user strings through the keyboard

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

Re: Accepting user strings through the keyboard

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

Re: Accepting user strings through the keyboard

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Accepting user strings through the keyboard

Post by bartbes »

The null characters are easy, add this:

Code: Select all

if not u then return end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Accepting user strings through the keyboard

Post by Robin »

no u
Help us help you: attach a .love.
User avatar
rhezalouis
Party member
Posts: 100
Joined: Mon Dec 07, 2009 10:27 am
Location: Indonesia
Contact:

[Response]My Approach

Post 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:
Aargh, I am wasting my posts! My citizenshiiiip... :o
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [Response]My Approach

Post 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.
Help us help you: attach a .love.
User avatar
rhezalouis
Party member
Posts: 100
Joined: Mon Dec 07, 2009 10:27 am
Location: Indonesia
Contact:

Re: [Response]My Approach

Post 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. ^^
Aargh, I am wasting my posts! My citizenshiiiip... :o
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [Response]My Approach

Post 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...
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 13 guests