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