Can I check the keyboardBuffer ?

Hey guys,
I’m looking for a function that the customer can only enter numbers with the keyboard. I want to use it for a input of a value for my game. Can i check the keyboardBuffer() or is there a function to check a string ?

Stoneberger

I’m a little confused. You want to only accept numbers from the keyboard?

If so:

function setup()
    input = ""
    showKeyboard()
end

function draw()
    background(40, 40, 50)

    text(input, WIDTH / 2, HEIGHT / 2)
end

function keyboard(k)
    if tonumber(k) ~= nil then
        input = input .. k 
    end
end

@Stoneberger Here’s something I had from long ago. Allows the use of “return” and “backspace”.


function setup()
    rectMode(CENTER)
    showKeyboard()
    str=""
    strDisp = "Enter a number, press return"
end

function draw()
    background(80,80,80)

    fill(0)
    noStroke()
    rect(WIDTH/2+10,HEIGHT/2+90,300,50)
    rect(WIDTH/2+10,HEIGHT/2-10,300,50)

    fill(255)
    stroke(255,0,0)
    strokeWidth(5)
    rect(WIDTH/2,HEIGHT/2+100,300,50)

    fill(150,150,150)
    stroke(255)
    strokeWidth(5)    
    rect(WIDTH/2,HEIGHT/2,300,50)

    fill(255,0,0)
    text(strDisp,WIDTH/2,HEIGHT/2+100)

    fill(255)
    text(str,WIDTH/2,HEIGHT/2) 
end

function keyboard(k)
    if k==RETURN then
        strDisp = str
        str=""
    elseif k==BACKSPACE then
        str=str:sub(1,str:len()-1)
    elseif k>="0" and k<="9" then
        str=str..k
    end
end

function touched(t)
    if t.state == ENDED then
        showKeyboard()
    end
end

Is this keyboard function also at a class available ?

The keyboard function is just that, a function. Anything else you want, you’ll have to write yourself unless someone else posts a class they created.

Okay, no problem. I tryed my best and now it works very fine. Thank you very much . :wink: