simple keyboard class

Not sure if this was done before or not. Did a search on keyboard and there were pages and pages of results. But here is a small keyboard for anyone who needs one. The keys values can be changed to match your needs. The only keys hard coded are the RTN, SP, ← keys which correspond to return, space, backspace. Also, the x,y position of the keyboard can be modified. I didn’t put a whole lot of effort into this because everyone needs something different, so I’ll let you modify it to what you need.

EDIT: Added code to switch between lower case and upper case using the “cap” key.


displayMode(FULLSCREEN)

function setup()
    value=""
    k1=keybrd(0,100)
end

function draw()
    background(40,40,50)
    k1:draw()
    if value~=nil then
        text(value,WIDTH/2,HEIGHT-100)
    end
end

function touched(t)
    if t.state==BEGAN then
        value=k1:touched(t)
    end
end

keybrd=class()

function keybrd:init(x,y)
    self.keys="-/:;()$&@.,?!'  ..STUVWXYZ1234567890ABCDEFGHIJKLMNOPQR"    
    self.size=42
    self.str=""   
    self.x=x-self.size
    self.y=y-self.size
    self.caps=false
    self.hide=false
end

function keybrd:draw()
    background(40,40,50)
    if self.hide then
        return
    end
    local c=0
    for y=1,3 do
        for x=1,18 do
            c=c+1
            fill(255)
            rect(x*self.size+self.x,y*self.size+self.y,self.size,self.size)
            fill(255,0,0)
            local val=string.sub(self.keys,c,c)
            if c==15 then val="CAP" end
            if c==16 then val="SP" end
            if c==17 then val="<--" end
            if c==18 then val="RTN" end
            if not self.caps then
                val=string.lower(val)
            end
            text(val,x*self.size+self.size/2+self.x,y*self.size+self.size/2+self.y)           
        end
    end
    text(self.str,WIDTH/2,self.y+200)
end

function keybrd:touched(t)
    if self.hide then
        return
    end
    if t.state==BEGAN then
        local tx=math.floor((t.x-self.x)/self.size)
        local ty=math.floor((t.y-self.y)/self.size)
        local pos=(ty-1)*18+tx
        local val=string.sub(self.keys,pos,pos)
        if not self.caps then
            val=string.lower(val)
        end
        if pos==15 then
            self.caps= not self.caps
            return
        end
        if pos==17 then
            self.str=string.sub(self.str,1,string.len(self.str)-1)
            return           
        end
        if pos==18 then
            local str1=self.str
            self.str="" 
            return(str1)         
        end
        self.str=self.str..val
    end
end

@Famous You might be able to use this.

I made updates to the keybrd class. You can adjust the x/y position, the width/height, the size of the keys, which table to use and whether you want the space, backspace, caps, and return keys to show or the order of them. The space, backspace, caps, and return keys have to be at the end of the keyboard table. You can have all of them or none of them or any combination. To position them, use c for caps, s for space, b for backspace, and r for return. You can also define different keyboard layouts with different keys for different keyboards. The width times the height should match the size of the keyboard table used. I show an example with 4 different keyboards with different tables, sizes, positions and keys. Not sure how useful this is, but it was something to do.


--# Main

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    -- x,y,w,h,size,keyboard to use
    k1=keybrd(100,300,4,14,30,1)
    k2=keybrd(50,50,27,2,35,2)
    k3=keybrd(400,300,3,5,60,3)
    k4=keybrd(650,400,9,2,35,4)
end

function draw()
    background(40,40,50)
    fill(255)
    k1:draw()
    if k1.value~=nil then
        text(k1.value,800,HEIGHT-50)
    end
    k2:draw()
    if k2.value~=nil then
        text(k2.value,800,HEIGHT-80)
    end
    k3:draw()   
    if k3.value~=nil then
        text(k3.value,800,HEIGHT-110)
    end
    k4:draw()   
    if k4.value~=nil then
        text(k4.value,800,HEIGHT-140)
    end
end

function touched(t)
    if t.state==BEGAN then
        k1:touched(t)
        k2:touched(t)
        k3:touched(t)
        k4:touched(t)
    end
end

keybrd=class()

function keybrd:init(x,y,w,h,size,kb)    
    self.keys={"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-/:;()$&@.,?!'<>csbr",
               "QWERTYUIOPASDFGHJKLZXCVBNM[]{}#%^*+=_|~<>.,?!'-/()rscb",
               "123456789.0-brs",  
               "0123456789ABCDEFrc"}   
    self.kb=kb
    self.w=w
    self.h=h
    self.size=size
    self.str=""   
    self.x=x-self.size
    self.y=y-self.size
    self.caps=false
    self.hide=false
    self.val=""
    self.value=""
end

function keybrd:draw()
    pushStyle()
    fontSize(self.size/2)
    if self.hide then return end
    local c=0
    for y=self.h,1,-1 do
        for x=1,self.w do
            c=c+1
            fill(255)
            rect(x*self.size+self.x,y*self.size+self.y,self.size,self.size)
            fill(255,0,0)
            self.val=string.sub(self.keys[self.kb],c,c)
            local len=string.len(self.keys[self.kb])
            if c>=len-3 then
                if self.val=="c" then self.val="CAP" end
                if self.val=="s" then self.val="SP" end
                if self.val=="b" then self.val="BSP" end
                if self.val=="r" then self.val="RTN" end
            end
            if not self.caps then 
                self.val=string.lower(self.val) 
            end
            text(self.val,x*self.size+self.size/2+self.x,
                        y*self.size+self.size/2+self.y)    
        end
    end
    fill(255)
    text(self.str,self.x+2*self.size,self.y+self.h)
    popStyle()
end

function keybrd:touched(t)
    if self.hide then return end
    if t.state==BEGAN then
        local tx=math.floor((t.x-self.x)/self.size)
        local ty=math.floor((t.y-self.y)/self.size)
        local pos=(self.h-ty)*self.w+tx
        local len=string.len(self.keys[self.kb])
        if tx<1 or tx>self.w or ty<1 or ty>self.h then 
            return 
        end
        self.val=string.sub(self.keys[self.kb],pos,pos)
        if not self.caps then 
            self.val=string.lower(self.val) 
        end
        if pos>=len-3 then
            if self.val=="c" then   -- caps
                self.caps = not self.caps 
                return
            end
            if self.val=="s" then   -- space
                self.str=self.str.." " 
                return
            end
            if self.val=="b" then   -- back space
                self.str=string.sub(self.str,1,string.len(self.str)-1) 
                return
            end
            if self.val=="r" then   -- return
                self.value=self.str
                self.str=""
                return
            end
        end
        self.str=self.str..self.val
    end
end