disable keyboard RETURN pushing text up

Hello guys,

Once again I have a question that has bugged me for a bout 2 years, I use the onboard keyboard to type in forms, and do not want the return key to push up the text to a new row, is there a way to stop this?

I’ve tried things like :

if k == RETURN then
keyboard(BACKSPACE)
end

etc…

The return key doesn’t do anything unless you tell it to:

function keyboard(key)
    if key==RETURN then 
       -- do nothing
        --
    elseif key==BACKSPACE then
        inkey=string.sub(inkey, 1, -2)
    else
        inkey=inkey..key
    end
end

Or rather, you detect return presses but then don’t add them to the input string.

edit: a more elegant version of the above

function keyboard(key)
    if key==BACKSPACE then
        inkey=string.sub(inkey, 1, -2)
    elseif key~=RETURN then
        inkey=inkey..key
    end
end

@archistudent Here’s an example for keyboard input without having to mess with the keyboard function. It strips off the RETURN character when the RETURN key is pressed. Use whatever is in str when the RETURN key is pressed.

function setup()
    showKeyboard()
end

function draw()
    background(0)
    str=keyboardBuffer()
    if string.sub(str,string.len(str))==RETURN then
        str=string.sub(str,1,string.len(str)-1)
        hideKeyboard()   -- this clears the keyboard buffer
        showKeyboard()
        print("="..str.."=")  -- do what you want with str, RETURN was pressed
        -- I print a leading and ending = sign to show that RETURN isn't there
    end
    text(str,WIDTH/2,HEIGHT/2)
end

@dave1707 hello Dave, thanks for the help. I’ve tried your example but it seems to get make a new buffer each time i press enter, I intend for the buffer to stay there even after pressing RETURN. After taking away the hideKeyboard and showKeyboard() functions, the buffer stays there but now the buffer is pushed up a new line every time RETURN is pressed.

@yojimbo2000, thanks for the help again, but I think the problem I am having still persists. My issue is trying to stop it from producing a new line every time RETURN is pressed. This ‘new line’ happens in the keyboardBuffer() level, so it doesn’t matter what is in the keyboard(key) function. The only way I can see it stopping it is to backtrack on the ‘newline’ everytime RETURN is pressed, hence my futile attempt at keyboard(BACKSPACE).

@archistudent I’m not sure what you’re trying to do then. If you don’t clear the keyboard buffer, then it will contain everything that’s been typed. I was thinking that when the RETURN key is pressed, the person is done entering information and you would then do whatever you need to do with that information. The keyboard buffer is cleared and ready for the next input data. The reason the buffer is pushed up a new line every time RETURN is pressed is because the buffer has the RETURN character in it. You can’t do anything to the information that’s in the keyboard buffer. That’s why I move keyboardBuffer to str and then remove the RETURN character from str.

@archistudent If you don’t want to clear the keyboardBuffer, then when you want to use the data, move the keyboardBuffer to a string and then parse the string and replace each RETURN character with a space. Then use that string. But I’m not sure why you don’t want to clear the buffer.

@dave1707 Oh I seeee, I will try that again. The reason I need to disable the return key in this way is because the users of this app are quite out of touch with technology, so I have devised a very stringent method of data input so that they wont get confused, they have gotten use to current method and do not want to change the way it works…

If you use my method you don’t need to bother with the keyboard buffer. Because you create your own buffer each time a key is tapped, you can intercept and filter out unwanted key presses. Here’s a fuller example incorporating the code above. Hitting return does nothing (but you could make it hide the keyboard if you wanted). IMO, if you want precise control over what’s buffered, the keyboard function is the simplest way to go.

-- Cursor

function setup()
    inkey="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam eget hendrerit ligula. Quisque vitae mauris aliquet risus efficitur hendrerit at nec quam. Nulla ut sollicitudin dolor, ut interdum nulla. Quisque malesuada fermentum erat vitae mattis. Suspendisse vitae cursus purus, sit amet dictum risus."
    font("DINAlternate-Bold")
    fill(0, 181, 255, 255)
    fontSize(20)
    textWrapWidth(WIDTH*0.4)
    textMode(CORNER) --necessary to stop cursor displacing text
    showKeyboard()
end

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

    local cursorSpeed = 4
    local cursorLen = (ElapsedTime*cursorSpeed%2)//1 --a number that regularly alternates between 0 and 1 
    local cursor=string.rep("\\u{25af}", cursorLen)..string.rep("\\u{25ae}", 1-cursorLen) --cursor symbol..empty cursor symbol
    text(inkey..cursor, WIDTH*0.3, HEIGHT*0.5)  
end

function keyboard(key)
    if key==RETURN then 
       -- hideKeyboard()
        --end of text input
    elseif key==BACKSPACE then
        inkey=string.sub(inkey, 1, -2)
    else
        inkey=inkey..key
    end
end

@yojimbo2000 @dave1707 Ah I see, you have both gotten rid of keyboardBuffer() giving you precise control, that never crossed my mind

ah alas it works like magic…