Put a max of caracter

How to put a max of caracter in a value?

You can’t limit the amount of characters in the keyboard buffer because we have no control over it. You might be able to use the keyboard function to limit what’s keyed. Since you don’t go into detail of what you’re doing, I can’t give more help.

@goodboy2004 Here’s an example that limits the characters pressed to 5 until the RETURN key is pressed.

function setup()
    str1,str2="",""
    showKeyboard()    
end

function draw()
    background(0)
    fill(255)
    text(str1,WIDTH/2,HEIGHT/2)
    text(str2,WIDTH/2,HEIGHT/2-50)
end

function keyboard(k)
    if k==RETURN then
        str2=str1
        str1=""
    elseif #str1<5 then
        str1=str1..k
    end
end

Thanks

In the fact, I juste want put a max of caracte in a value.

I found a solution with KeyboardBuffer. But when I press backspace he have a time before he erase a caracter.
This is the code:

function setup()
    buffer=""
end

-- This function gets called once every frame
function touched(touch)
    --Show keyboard when the screen is touched
    showKeyboard()
end

function draw()
    background(40,40,50)
    fill(255)
    textMode(CORNER)
    buffer2 = keyboardBuffer()
    _,bufferHeight = textSize(buffer) 
    if #buffer2<5 then
        buffer=buffer2
    end
    text( buffer, 10, HEIGHT - 30 - bufferHeight )
end

@goodboy2004 Add this line to the end of the draw function. That shows what’s in the keyboardBuffer as you type. If you key in only 4 characters that go in buffer, then the backspace works correctly. If you key more that 4 characters, then it takes more backspaces in the keyboardBuffer to back out the characters.

    text( keyboardBuffer(), 10, HEIGHT - 100 - bufferHeight )

I found a solution with the function keyboards.
This is the code:


function setup()
    strTab={}
    buffer=""
    showKeyboard()
    textMode(CORNER)
end

function draw()
    background(30,30,0,255)
    fill(255)
    if buffer~="" then
        text(buffer,10,600)
    end
    for z=1,#strTab do
        text(strTab[z],10,420+(#strTab-z)*20)
    end
end

function touched()    
    showKeyboard()
end

function keyboard(key)
    if key == RETURN then
        table.insert(strTab,buffer)
        buffer=""
    elseif key==BACKSPACE then
        buffer=string.sub(buffer,1,string.len(buffer)-1)
    elseif #buffer<5 then
        buffer=buffer..key
    end   
end

@goodboy2004 If you would have done a forum search for keyboard, you would have found a lot of code that does what you show above.

Before to create this discussion, I had search.

@goodboy2004 I did a forum search for keyboard. I found a lot of keyboard stuff, but nothing specific to what you show. I remember posting several code examples similar to yours, but I couldn’t find them. The forum search finds a lot of things, but not everything. I’ve noticed that in the past. There have been a lot of times I search for something I know I posted, but I never could find it.