Example to get User Input from the Keyboard?

I enjoy getting own code run on my iPad and still find things to enhance in my version of LUA jump. Recent mini-project is to associate the highscores with the name of the highscore owner. Thus I am happy to have the new keyboard functions. Can somebody give me an example, how to use the keyboard functions, because I have to admit, I’ m a kind of copy/paste and modify coder. Thanks a lot in advance!

I’am just busy with programming a standard value input field for making calculations.

Main:

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    watch("cycleTime")
    watch("cable")
    
    Input:init()
    
    --displayMode(FULLSCREEN)    
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(195, 195, 206, 255)
    
    Input:var()
    
    --declaration of input val's
    cable = val[4]

    --calculations
    --printing results on the screen
    
    cycleTime = DeltaTime*1000

end

Class Input:

Input = class()

function Input:init()
--name, default val and unit can be fiiled in here.
--table can be as long as you want
    name = {"Drum Diam:","Drum Width:","Flange Diam:","Cable Diam:"  }
    val  = {1,2,3,4}
    unit = {"[mm]","[mm]","[mm]","[mm]"}
    
    h  = 10
    x1 = 10
    x2 = 275
    x3 = 10
    dy = 25
    i = 0
    a = 0
    
    buffer = ""

end

function Input:var()
    -- Codea does not automatically call this method
    x = CurrentTouch.x
    y = CurrentTouch.y
    z = CurrentTouch.state
    
    textMode(CORNER)
    fill(5, 5, 5, 255)
    font("Verdana")
    fontSize(20)
    
    
    for i = 1, #name do
        y1 = HEIGHT - h - dy*i
        text(name[i],x1,y1)
        t = string.format("%0.2f",val[i])
        text(t,x1+x2-textSize(t),y1)
        text(unit[i],x1+x2+x3,y1)
        
        --if you hit an input field then you can change the val
        --end the input operation with a return
        --no val and return = val unchanged
        --use iPad in portrait
        if x > x1 and y > y1 and y < (y1+dy) and z == BEGAN then
            a = i
        end
        
    end
    
    if a > 0  then
        showKeyboard()
            buffer = keyboardBuffer()
            text("Input "..name[a].. "?  "..buffer,15,300)
                
            function keyboard(key)
                if string.byte(key) == 10 then
                    if #keyboardBuffer() > 0 then
                        val[a] = buffer
                    end
                    hideKeyboard()
                    a = 0
                    
                end 
            end
        
    end    
    
end

function Input:touched(touch)
    -- Codea does not automatically call this method
end

It is a start, but it works
Greetings, Dirk.nl

Pooh, thats not good!
I don’t know how to do!
Second try.

Input = class()

function Input:init()
--name, default val and unit can be fiiled in here.
--table can be as long as you want
    name = {"Drum Diam:","Drum Width:","Flange Diam:","Cable Diam:"  }
    val  = {1,2,3,4}
    unit = {"[mm]","[mm]","[mm]","[mm]"}
    
    h  = 10
    x1 = 10
    x2 = 275
    x3 = 10
    dy = 25
    i = 0
    a = 0
    
    buffer = ""

end

function Input:var()
    -- Codea does not automatically call this method
    x = CurrentTouch.x
    y = CurrentTouch.y
    z = CurrentTouch.state
    
    textMode(CORNER)
    fill(5, 5, 5, 255)
    font("Verdana")
    fontSize(20)
    
    
    for i = 1, #name do
        y1 = HEIGHT - h - dy*i
        text(name[i],x1,y1)
        t = string.format("%0.2f",val[i])
        text(t,x1+x2-textSize(t),y1)
        text(unit[i],x1+x2+x3,y1)
        
        --if you hit an input field then you can change the val
        --end the input operation with a return
        --no val and return = val unchanged
        --use iPad in portrait
        if x > x1 and y > y1 and y < (y1+dy) and z == BEGAN then
            a = i
        end
        
    end
    
    if a > 0  then
        showKeyboard()
            buffer = keyboardBuffer()
            text("Input "..name[a].. "?  "..buffer,15,300)
                
            function keyboard(key)
                if string.byte(key) == 10 then
                    if #keyboardBuffer() > 0 then
                        val[a] = buffer
                    end
                    hideKeyboard()
                    a = 0
                    
                end 
            end
        
    end    
    
end

function Input:touched(touch)
    -- Codea does not automatically call this method
end

Stupid me, the lay-out of my first post is correct, but the second not

hartland, Thanks a lot, I will try it and try to understand it, when I come home and find some time for the good things in life … Yes an own class is much better than just a function I was thinking about …

@hartland I corrected the formatting on your second post.

And I corrected it on the first …

@Andrew_Stacey thanks :slight_smile:

One useful thing to do with keyboard (until we add the ability to query this) is to wrap showKeyboard and hideKeyboard in your own closures, and then add a function to query whether the keyboard is currently on-screen.


function setup()
    redefineKeyboardFuncs()
end

function redefineKeyboardFuncs()
    local oldShowKeyboard = showKeyboard
    local oldHideKeyboard = hideKeyboard
    local isShowingKeyboard = false

    showKeyboard = function()
        if isShowingKeyboad == false then
            oldShowKeyboard()
            isShowingKeyboard = true
        end
    end

    hideKeyboard = function()
        if isShowingKeyboard == true then
            oldHideKeyboard()
            isShowingKeyboard = false
        end
    end

    showingKeyboard = function()
        return isShowingKeyboard
    end
end

This lets you test whether the keyboard is currently on screen by calling showingKeyboard()

Thanks Simeon and Andrew_Stacey,

I have done copy and paste from my Codeo program.
How have you changed the formatting, can I do that by myself, and what is wrong with copy/paste?

Hi Hartland,

I believe you need to fence your code (precede and follow) with three tilde marks ~~~.

See ‘fenced code block’ here for an example:

http://kohanaframework.org/3.0/guide/userguide/markdown

Yes, “fencing” your code is the best way to present code here. That also ensures that entities are properly escaped so that you can do:

<html>

without having to worry. The forum software uses PHP Markdown Extra for formatting, in case you want to look up the details.

I think the combination demonstrated by hartland of keyboardBuffer() to get the players name and function keyboard( key) to check for the line feed will work for me. Now I have to learn how to use it - I had it working, but now calling from Main touched() doesn’t work anymore.
Either my text box vanishes and no input is taken or the Keyboard won’t go away …
I guess my stupideness in coding, siggghh. Thanks so far!

Hi Blanchot, thanks.
But, is it so simple that I must add at the beginning and end of the text three tilde marks?
So,I will try

text with three tilde marks before and after

poeh, I hope

Yes, this works !!,
Thanks, Blanchot!
Greetings, Dirk.nl