Handling improper Unicode...

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
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Handling improper Unicode...

Post by Jasoco »

I'm writing a GUI for my game and am working on text input and it works pretty well. In order to handle properly I check the key string against a "allowed characters" string then use the Unicode for building the output string. But when input certain combinations Löve errors. For instance, if I hold Option/Alt + something like F or something it will error explaining that the code is not allowed in string.char().

So I added a pcall check to the string.char line and it kind of works, except if I hold Option + S it crashes when I try to get the width of the string saying "Out of space". What space? What character am I sending? And how can I prevent it? When I print out the character being returned from string.char being passed Option+S it prints a question mark as if it can't tell what it is, but it still allows the character to pass through which causes problems later when it tries to get the width of the string.

I've even tried converting the character to a string first. The Unicode is 223. Is there a way to figure out which Unicode values are proper and which will cause problems? When I type Option+S here I get ß. And that's not the only combination that causes problems. It seems different ones cause different problems in different ways. I just want a catch-all.

Or basically a list of codes that are proper characters. A better text input function than what I have.

Code: Select all

function guiElement.textinput:keypressed(k, u)
	local sval = self.value
	if isChar(k, self.chars) then
		local c = ""
		if pcall(string.char, u) then
			c = tostring(string.char(u))
		end
		if #("x"..sval.."x") - 2 < self.maxlength then
			self.value = sval .. c
		end
	elseif k == "backspace" then
		self.value = string.sub(sval, 0, #sval - 1)
	end
end

Code: Select all

function isChar(k, chars)
    local ic = false
    chars = chars or "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`~!@#$%^&*()-=_+,./<>?;':\"[]{}\\| "
    for i = 1, #chars do
        if string.sub(chars, i, i) == k then
            ic = true
        end
    end
    return ic
end
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 43 guests