Keyboard Undo Glitch

Hello!

I’ve been having a problem with the keyboard input. On the on-screen keyboard there are undo and redo shortcuts in the top right corner. I found that if you type something in and hit undo it will backspace, but then if you hit redo right after that, it will copy everything you’ve typed already to the end of the string.

Here’s my code:

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

function draw()
    background(40, 40, 50)
    strokeWidth(5)
    fill(255, 255, 255, 255)
    text(var,WIDTH/2,HEIGHT*2/3)
end

function keyboard(key)
    if key == RETURN then
        hideKeyboard()
    else
        var = var..key
    end
    if key == BACKSPACE then
        var = string.sub(var,1,string.len(var)-1)
    end
    print(key)
end

function touched(touch)
    if not isKeyboardShowing() then showKeyboard() end
end

Is this a problem with Codea, or a problem with my code? I doubt there’s a way to just disable the shortcut bar.

Thanks for the help!

@Dwins I don’t think it’s a problem with Codea. I’m not sure which shortcuts you’re referring to. My onscreen keyboard has the undo and redo keys in the lower left of the keyboard. There’s a curved arrow to the left and a curved arrow to the right on the upper left of my keyboard along with a clipboard icon. Not sure what you’re seeing in the top right corner. From what I’m seeing, undo removes 1 character and redo inserts everything keyed which is what you’re saying. I think the intent of undo and redo is for you to keep track of how many characters you want to undo and how many characters to redo. Undo backspaces 1 character, but you can backspace as much as you want. Redo gives you everything you keyed, so you can redo as much as you want.

Oops, sorry, I meant left…

@dave1707 so is there a way to prevent it from reinserting everything?

I found a second problem too. If you type something in, and then type a character like and hit backspace it deletes the whole string and doesn’t let you type anything after that

I played around with it a bit, and I found a solution that fixes both problems. Replace the part that says

else
    var = var..key

with

elseif string.len(key) == 1 then
    var = var..key

With the help of print() statements, I noticed that the redo button returned the whole string is the key parameter, so anything longer than one character will not be returned. This also prevents special characters like £ and ¥ from being typed

I’m pretty happy with how it turned out, but if anyone has another suggestion, I’d love to hear it!