How to create some kind of input textbox?

Hello!

I’ve searched the discussion board here for a while now and didn’t find a answer to my question: is there any simple way in Codea to create some kind of input textbox?

I would like to ask the user of my project a few questions (like playername, age, …). The inputbox should come up after the intro of my project an the keyboard should be automatically shown. How can I do this within Codea?

Thanks in advance for your suggestions.

Regards

Markus

Hi all.
I am new to the forum and am happy with CODEA.
I join the request for a facilde create input forms for my projects. Sorry for my English is not my native language. Is there any detailed documentation of CODEA in Spanish. Thank you,

I did some basic textboxing in my windowing library awhile ago. I’ve been slowly cleaning up the code and fixing some stuff that isn’t on my GitHub yet, but you can take a look at the code.

https://github.com/deamos/Codea-Window-Manager

also got text input on my apple style UI library: https://github.com/ruilov/LIB-LUI2

(requires this as well: https://github.com/ruilov/LIB-Table)

http://www.youtube.com/watch?v=7vaelVOW1Wo

To get the keyboard to show up you want to use the showkeyboard() command

as for input you also need the keyboard (key) function
and a stringto concatenate each keystoke with something like

function Keyboard(key)

input = input…key

end
will get the general input and when your done you want to use the hidekeyboard()

if key == RETURN then
hidekeyboard()
end

you could have text be located in the area of the screen you want them to click with a prompt message in the form of another string which would change to show them what they are typing
something like

buffer = keyboardBuffer(key)

if input == “” then

prompt = “Enter your name”

elseif input ~= “” and string.len( input) > 3 then

prompt = input

end

text(prompt, Width + 20, Height - 10)

The Spritely example project, included with Codea, includes code that implements a text box. See class TextBox, the implementation of the function keyboard(key) in the Main tab, and parts of the LoadScreen class.

Ok. So I see that there is no really easy way of getting this done. I’ve thought that there might be some kind of simple input command like it was and is in older BASIC Programming languages.

Thanks a lot for your suggestions and tips. I’ll have a look at the scripts mentioned above…

MaFreRade

If I understand your question correctly, here is some code I put together. It shows an input box. If you tap it, it will bring up the keyboard. Type what you want into the box. When you press return, what you typed will be in “str” and show in the output screen. The keyboard will also close. If you tap the input box again, it will clear and allow you to input another value. Maybe you can modify this if it isn’t exactly what your after.

EDIT: changed one line and added another.


function setup()
    str = ""
    bcolor= 0
    l=100
    r=300
    b=575
    t=625
end

function draw()

    background(30,30,0,255)
    stroke(255)
    strokeWidth(6)
    
    fill(255)
     text("Enter value",150,650)  
  
    if bcolor == 0 then
        fill(255, 0, 0, 255)
    else
        fill(128,0,0,255)
    end

    rectMode(CENTER) 
    textMode(CENTER) 
    rect(200,600,200,50)

    buffer = keyboardBuffer()
    
    fill(255)
    if string.len(buffer)>0 then    -- this line was changed
        text(buffer,200,600)
    else
        text(str,200,600)
    end

    rectMode(STANDARD)
    textMode(STANDARD)
end

function touched()
    if CurrentTouch.x > l and CurrentTouch.x < r then
        if CurrentTouch.y > b and CurrentTouch.y < t then
            showKeyboard()
            str=""   -- this line was added
            bcolor = 1
        end
    end
end

function keyboard(key)
    if key == RETURN then
        str = buffer
        hideKeyboard()
        bcolor = 0
        print(str)
    end
end

How do you add extra boxes and add up and display a total on the screen

use Excel?

or you can search for the Cider “windows” style control library on the forum (use normal google search, it goes back further in time).

@dave1707, how would you make it so when you press enter the word that you typed stays on the screen?

@Andrewsimpson4 Here’s another example of some input boxes. Just tap the box you want to select, type in a number. Tap another input box and enter a number. The total of the 4 boxes will be in the “Total” box. Not sure if this is what you’re after. Pressing return is not required.


displayMode(FULLSCREEN)

function setup()
    showKeyboard()
    iTab={}
    table.insert(iTab,number(100,600,100,30,"number 1"))  
    table.insert(iTab,number(300,600,100,30,"number 2"))  
    table.insert(iTab,number(500,600,100,30,"number 3"))  
    table.insert(iTab,number(700,600,100,30,"number 4")) 
end

function draw()
    background(40,40,50)
    for a,b in pairs(iTab) do
        b:draw()
    end
    noFill()
    stroke(255,0,0)
    strokeWidth(2)
    rect(400,500,100,30)
    fill(255)
    text("Total",400,540)
    text(iTab[1].val+iTab[2].val+iTab[3].val+iTab[4].val,400,500)
end

function touched(t)
    for a,b in pairs(iTab) do
        if b:touched(t) then
            return
        end
    end   
end

number=class()

function number:init(x,y,w,h,txt)
    self.x=x
    self.y=y
    self.w=w
    self.h=h  
    self.txt=txt 
    self.val=0
    self.sel=false
end

function number:draw()
    rectMode(CENTER)
    if self.sel then
        if keyboardBuffer() ~= nil and keyboardBuffer() ~= "" then
            self.val=tonumber(keyboardBuffer())
        end
    end
    if self.val==nil then
        self.val=0
    end
    fill(0)
    if self.sel then
        fill(85, 121, 125, 255)
    end
    stroke(255)
    strokeWidth(2)
    rect(self.x,self.y,self.w,self.h)  
    fill(255)
    text(self.txt,self.x,self.y+self.h)
    text(self.val,self.x,self.y)
end

function number:touched(t)
    if t.state==BEGAN then
        for z=1,#iTab do
            iTab[z].sel=false
        end
        if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
                t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
            hideKeyboard()
            showKeyboard()
            self.sel=true
            return true
        end
    end 
end

@Andrewsimpson4 If you were talking about the code I posted farther up, I modified it so what was typed stays in the input box until you tap it to enter something else.

@Badegg The last code might be what you’re after.

@dave1707, but I need it to display letters to

@dave 1707 never mind i got it!

@Andrewsimpson4 Did you try the code I changed before the last one. It’s the one with the single input box. I changed that one to keep the text until you select it to key something else.

yes that is what I used. I tried to add another box but I couldn’t get it to work right. @dave1707

@Andrewsimpson4 Here’s the single input text code where I added a second input box. Is this what you were after.


function setup()
    iTab={}    -- input box x,y,w,h
    table.insert(iTab,vec4(200,600,200,50))
    table.insert(iTab,vec4(200,500,200,50))
    strTab={}  -- initial input text
    table.insert(strTab,"Enter value")
    table.insert(strTab,"Enter value")
    rectMode(CENTER) 
end

function draw()
    background(30,30,0,255)
    stroke(255)
    strokeWidth(6)   
    for a,b in pairs(iTab) do
        fill(255, 0, 0, 255)
        if a==selected then
            fill(128,0,0,255)
            strTab[a] = keyboardBuffer()   
        end
        rect(b.x,b.y,b.z,b.w)
        fill(255)
        text(strTab[a],b.x,b.y)
    end
end

function touched()
    for a,b in pairs(iTab) do
        if CurrentTouch.x > b.x-b.z/2 and CurrentTouch.x < b.x+b.z/2 and
            CurrentTouch.y > b.y-b.w/2 and CurrentTouch.y < b.y+b.w/2 then
            showKeyboard()
            selected=a
        end
    end
end

function keyboard(key)
    if key == RETURN then
        print(strTab[selected])
        selected=0
        hideKeyboard()
    end
end