Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

showKeyboard covers input field

edited September 2014 in Questions Posts: 20

Hi,
I'm creating a user input form that has a long list of text boxes.
The problem I have created is if I showKeyboard() to edit text in a box, the keyboard can cover over the text boxes I'm trying to edit.
I notice the code editor solves this by moving up the code being edited. Is this a public function I can also use?
What are possible solutions for a keyboard covering over the text field under edit?

Thanks

Comments

  • dave1707dave1707 Mod
    Posts: 7,612

    You can add code to move the input box, allow the whole screen to be moved around, or write your own keyboard that can be moved around.

  • dave1707dave1707 Mod
    Posts: 7,612

    See this link for an example of your own keyboard.

    http://codea.io/talk/discussion/5619/key-board#Item_6
    
  • Just test for isKeyboardShowing() and if the text box is on the lower half of the screen, and if so move the textbox up temporarily.

  • Unfortunately that may then introduce a problem for input fields at top of the screen.
    If the user is entering data in box at bottom then pushing text up is fine, if they are entering text at top, then the text box shouldn't move up.

    I'll need to deal with both cases.
    Problems like these really bloat my code :-(

  • dave1707dave1707 Mod
    Posts: 7,612

    @Famous You should know which input field is receiving the keyed data. If that's the case, then you'll know if you should push the screen up or not.

  • edited September 2014 Posts: 20

    Example solution
    self.focus is set to true in touched if self was hit

        -- move stuff around so keyboard doesn't cover it
        posx = self.pos.x
        posy = self.pos.y
        if isKeyboardShowing() == true and self.focus == true then
            --Highlight and center box under edit
            fill(69, 206, 14, 255)  
            posx=200
            posy=HEIGHT/2        
        end
        if isKeyboardShowing() == true and self.focus == false then
        --grey out and move away box not under edit
            fill(145, 156, 141, 255)  
            if posy-50<HEIGHT/2 then
                posy=0
            end
        end
    
  • @Famous Side note - you don't need to check if a boolean is true, i.e.

    a = true
    if a == true then...
    

    can just be

    a = true
    if a then...
    

    (and instead of a == false you can use not a)

  • Posts: 2,042

    @SkyTheCoder, yes but those also become true when the variable is nil/not nil

  • @JakAttak I said, when checking a boolean. isKeyboardShowing() will never return nil (and it would make sense to be false if it was), and I don't see how self.focus could be nil either (again, it would make sense to be false if it was).

  • Posts: 2,042

    @SkyTheCodee, I understand. I just wanted to make sure that @Famous knows all of this if he didn't already know he could do it (in case later he uses it with a variable that could be nil and needs to recognize that specifically)

  • dave1707dave1707 Mod
    Posts: 7,612

    @JakAttak I disagree with your response "those also become true when the variable is nil/not nil". In Lua, a variable is false only if it's nil or false, otherwise it's true for all other values.

  • dave1707dave1707 Mod
    Posts: 7,612

    @JakAttak That kind of surprised me because I would have thought 0 would be considered false and any other number true.

  • @dave1707 It surprised me as well, because 0 is false in other languages such as C++.

  • Posts: 2,042

    @dave1707, right that's what I'm saying. So if I have a variable that could be 15 or could be true (not sure why, but it's possible) and I want to check that it is true, if a then won't work.

    if not a then will work if a is nil or false, which you could also potential want to distinguish between.

  • dave1707dave1707 Mod
    Posts: 7,612

    @Saturn031000 I used to write in C and I always used 0 for false and non 0 for true. @JakAttak It's good to know exactly how things work to avoid coding problems, especially when you're coming from a different language.

  • @JakAttak Why would your variable at one point be a number and another a boolean?

  • dave1707dave1707 Mod
    Posts: 7,612

    @SkyTheCoder I don't think a variable would be both. I think he was just making a point about testing for true, false, or nil.

  • Re: original post: is there a way to test for keyboard height? With iOS 8 the keyboard isn't always the same height.

Sign In or Register to comment.