Page 1 of 2

NörmalTyping lib

Posted: Tue Aug 17, 2010 8:54 pm
by Tesselode

Code: Select all

function love.keypressed(key)
      string=string..key
end
That should be fine for typing stuff out, right? Wrong. Want to type the word "The" with capitals? You'll get:

Code: Select all

rshiftthe
Want to fix a mistake you made?

Code: Select all

rshiftthebackspacebacksapce
That's where the NörmalTyping lib comes in. It automatically adds characters to a string as if they were being typed in a word processor.

The thing is, it's not done yet. Not even close. A script like this takes longer and more tedious code than usual. Not to mention how well I can screw stuff up (like misspelling "currentstring" repeatedly). So I'm going to try something out. I'm open-sourcing it. Well yeah, it's already open source, but I'm going to let everyone on the forum work on it.

So here's the idea: one of you takes my code, adds to it, posts it, lets someone else add more to it, etc. That way this library will get done faster and easier. Besides, I'm sure this script will be helpful to a lot of you.

I'll go ahead and post the code and the intended usage of the library:

___________________________________________________________________________________________________________

Code: Select all

normaltyping(currentstring,key)
currentstring - the string to add characters to/remove characters from
key - the key to "type"

Code: Select all

function normaltyping(currentstring,key)
	local bannedkey={} --keys that should never be typed
	bannedkey[1]="up"
	bannedkey[2]="down"
	bannedkey[3]="left"
	bannedkey[4]="right"
	bannedkey[5]="home"
	bannedkey[6]="end"
	bannedkey[7]="pageup"
	bannedkey[8]="pagedown"
	bannedkey[9]="insert"
	bannedkey[10]="clear"
	bannedkey[11]="delete"
	bannedkey[12]="f1"
	bannedkey[13]="f2"
	bannedkey[14]="f3"
	bannedkey[15]="f4"
	bannedkey[16]="f5"
	bannedkey[17]="f6"
	bannedkey[18]="f7"
	bannedkey[19]="f8"
	bannedkey[20]="f9"
	bannedkey[21]="f10"
	bannedkey[22]="f11"
	bannedkey[23]="f12"
	bannedkey[24]="f13"
	bannedkey[25]="f14"
	bannedkey[26]="f15"
	bannedkey[27]="numlock"
	bannedkey[28]="capslock"
	bannedkey[29]="scrollock"
	bannedkey[30]="rshift"
	bannedkey[31]="lshift"
	bannedkey[32]="rctrl"
	bannedkey[33]="lctrl"
	bannedkey[34]="ralt"
	bannedkey[35]="lalt"
	bannedkey[36]="rmeta"
	bannedkey[37]="lmeta"
	bannedkey[38]="lsuper"
	bannedkey[39]="rsuper"
	bannedkey[40]="mode"
	bannedkey[41]="compose"
	bannedkey[42]="pause"
	bannedkey[43]="escape"
	bannedkey[44]="help"
	bannedkey[45]="print"
	bannedkey[46]="sysreq"
	bannedkey[47]="break"
	bannedkey[48]="menu"
	bannedkey[49]="power"
	bannedkey[50]="euro"
	bannedkey[51]="undo"
	
	local a
	local stop=0
	for a=1,#bannedkey do if key==bannedkey[a] then stop=1 break else stop=0 end end
	
	if stop==0 then
		if key=="backspace" then if #currentstring>1 then currentstring=string.sub(currentstring,1,#currentstring-1) else currentstring="" end end
	end
	
	return currentstring
end

Re: NörmalTyping lib

Posted: Tue Aug 17, 2010 9:51 pm
by knorke
Hey
thats a quite usefull idea.
I would do it different though. Instead of checking for forbidden keys, check what letters are allowed, might be less work.
The long list of keys at the top does not seem like good style.
misspelling "currentstring" repeatedly
Oh yes, i hate names like that :?

I cooked this up:

Code: Select all

local text = ""

function love.load()
love.graphics.setMode(600, 400)
end

function love.draw()
love.graphics.print ("type something:" .. text,100,100)
end

function love.update(dt)
end

function love.keypressed(key, unicode)
text = normaltyping (text, key)
end

function normaltyping (typedtext, key)
local newletter = "DONOTADD"
if (key == nil) then return typedtext end
if (key >= 'a' and key <= 'z') then newletter=key end	--letters a to z
if (key >= '0' and key <= '9') then newletter=key end	--numbers 0 to 9
if (key==' ') then newletter = " " end					--space
if (love.keyboard.isDown( 'rshift' ) or love.keyboard.isDown( 'lshift' )) then newletter = newletter:upper() end	--to upper case if shift key is pressed
if (key:len() > 1) then return typedtext end	--no adding of 'escape' etc
if (newletter:len() > 1) then return typedtext end
return typedtext .. newletter
end
(copy&paste, its a working example how to use)

You can type letters a-z and numbers 0-9 as well as space. Upper case works too.
Sorry, except for the function head not much is left but thats the danger of open sourcing ;)
Hope it gives you some ideas!

Re: NörmalTyping lib

Posted: Tue Aug 17, 2010 10:06 pm
by bartbes
Ahem, take a look at http://love2d.org/wiki/love.keypressed again, and specifically the unicode argument.

Re: NörmalTyping lib

Posted: Tue Aug 17, 2010 10:36 pm
by Tesselode
bartbes wrote:Ahem, take a look at http://love2d.org/wiki/love.keypressed again, and specifically the unicode argument.
What does it do?

Re: NörmalTyping lib

Posted: Tue Aug 17, 2010 11:00 pm
by bartbes
It provides you with a unicode character that represents the generated character. So, shift is in there, special characters, etc.

Re: NörmalTyping lib

Posted: Tue Aug 17, 2010 11:14 pm
by knorke
i tested and unicode gives the unicode number not character.
ie 97 for a, 98 for b and 13 for the return key.
I find

Code: Select all

if (key >= 'a' and key <= 'z')
more readable than

Code: Select all

if (unicode>= 97 and unicode <= 122)
bartbes wrote:So, shift is in there, special characters, etc.
Yup, thats the problem ;)
filtering what numbers represent "typeable" letters is needed as well as "delete last letter" if backspace is pressed etc.

Re: NörmalTyping lib

Posted: Wed Aug 18, 2010 8:53 am
by bartbes
Nonononononono, in most cases this should be enough:

Code: Select all

function love.keypressed(key, u)
    if key == "backspace" then
        str = str:sub(1, -2)
    elseif u then
        str = str .. string.char(u)
    end
end

Re: NörmalTyping lib

Posted: Wed Aug 18, 2010 9:27 am
by knorke
uhm did you try it?
pressing escape or return adds funny [] characters to the string, captital letters dont work and char makes Löve give "bad value" error on the € (euro) sign.
after pressing shift or any other special key (page up, F1 etc) it stops adding to the string but the string still grows in length :brows:
Maybe messes with string termination sign if Lua has something like this.

Behold, The Great Typing Competion!

Code: Select all

local text = ""
local text2 = ""
local str = "" 

function love.load()
love.graphics.setMode(600, 400)
end

function love.draw()
love.graphics.print ("(knorkes normaltyping) type something: <" .. text:len() .. ">" .. text ,10,100)
love.graphics.print ("(just adding unicode) type something: <" .. text2:len() .. ">" .. text2,10,150)
love.graphics.print ("(bartbes thing) type something <".. str:len() ..">"..  str,10,200)
end

function love.update(dt)
end

function love.keypressed(key, unicode)
    if key == "backspace" then
        str = str:sub(1, -2)
    elseif unicode then
        str = str .. string.char(unicode)
    end
text = normaltyping (text, key)
text2 = text2 .. unicode
end

function normaltyping (typedtext, key)
local newletter = "DONOTADD"
if (key == nil) then return typedtext end
if (key >= 'a' and key <= 'z') then newletter=key end	--letters a to z
if (key >= '0' and key <= '9') then newletter=key end	--numbers 0 to 9
if (key==' ') then newletter = " " end					--space
if (key == 'backspace') then typedtext = typedtext:sub(1, -2) end --backspace removes last char. copied from bartbes
if (love.keyboard.isDown( 'rshift' ) or love.keyboard.isDown( 'lshift' )) then newletter = newletter:upper() end	--to upper case if shift key is pressed
if (key:len() > 1) then return typedtext end	--no adding of 'escape' etc
if (newletter:len() > 1) then return typedtext end
return typedtext .. newletter
end

Re: NörmalTyping lib

Posted: Wed Aug 18, 2010 9:45 am
by bartbes
It messes up when drawing, indeed, as LÖVE can't print unicode characters, but everything in the ascii range should work just fine.

Re: NörmalTyping lib

Posted: Thu Aug 19, 2010 7:25 pm
by ljdp
Take a look at textinput in Goo:

Code: Select all

function goo.textinput:keypressed(key,unicode)
	if not self.focus then return false end
	if key == 'backspace' then
		self:keyBackspace()
	elseif key == 'return' then
		self:keyReturn()
	elseif key == 'left' then
		self:keyLeft()
	elseif key == 'right' then
		self:keyRight()
	elseif key == 'up' then
		self:keyUp()
	elseif key == 'down' then
		self:keyDown()
	elseif unicode ~= 0 and unicode < 1000 then
		self:keyText(key,unicode)
	end
	if self.onKeypressed then self:onKeypressed( key, unicode ) end
	return true
end

function goo.textinput:keyText(key,unicode)
	self:insert(string.char(unicode), self.caretPos)
	self.caretPos = self.caretPos + 1
end

function goo.textinput:keyBackspace()
	if self.caretPos == 1 and self.linePos > 1 then
		if not self.multiline then return end
		self:backspaceLine( self.linePos )
	else
		self:remove(self.caretPos,1)
		self.caretPos = self.caretPos - 1
	end
end