Keyboard / String Questions

Is there a way to keep the keyboard buffer after hiding the keyboard? Ie if i hide the keyboard and bring it up again, resume where I left off? Or do I have to use keyboard(key) and design my own keyboard buffer system? Thanks

@Mr_Ninja Showing the keyboard is the only way to clear the buffer. If you want to keep what you had in the buffer after hiding / showing the keyboard, then save the buffer information. Then you should combine your saved info with the new information that’s in the buffer.

I’d go with the second option for full functionality, I’m surprised no one has remade the iOS keyboard in codea… Wouldn’t be too hard either.

But how would you write to the buffer?

Create your own?

@Mr_Ninja Here’s an example of what I think you’re after. The keys you press are displayed between the > < characters. When you press RETURN, whatever is between the > < characters is shown at the top of the screen and cleared from between the > < characters. If you hide the keyboard after keying some characters but before you press RETURN, the keyed characters are hidden. If you tap the screen to bring back the keyboard, those characters are shown and you can continue to add to them.


function setup()
    showKeyboard()
    str=""
    hold=""
    show=""
end

function draw()
    background(40,40,50)
    fill(255)
    
    if rtn then -- return pressed, clear keyboard buffer
        rtn=false
        hideKeyboard()
        showKeyboard()
    else
        str=hold..keyboardBuffer()  -- save keyboard buffer
    end
    
    -- display when the return key is pressed
    text(show,WIDTH/2,HEIGHT-100)
    
    -- display when the keyboard is showing and a key is pressed
    if isKeyboardShowing() then
        text(">"..str.."<",WIDTH/2,HEIGHT/2)
    end
end

function keyboard(k)
    if k==RETURN then   -- return key is pressed
        str=hold..keyboardBuffer()
        show=str
        hold=""
        rtn=true
    end 
end

function touched(t)
    if t.state==BEGAN then
        hold=str
        showKeyboard()  -- show keyboard when screen is touched
    end
end

Thanks @dave1707, thats pretty much exactly what I’m looking for, even better with the way you write something to the screen. With a little tweaking, this will work perfectly for my project.

Here’s the keyboard system that i use in my app. To use it, call instance:keys() to get the return. It avoids the problems involved with keyboardBuffer(), mainly the results clearing everytime you clear it. Thanks to @dave1707, as this code is based on your code, i just simplified it for my project.

KeyboardSystem = class()

function KeyboardSystem:init()
self.data = ""
end

function KeyboardSystem:keys()
return self.data
end

function KeyboardSystem:keyboard(key)
if key == BACKSPACE then
self.data=string.sub(self.data,0,string.len(self.data)-1)
else
self.data = self.data..key
end
end