Keyboard height

Can I accurately determine the height of the keyboard when it is open?

As far as I know there isn’t any variable that says what the keyboard height is for each device type. You can determine the height manually on your device and use that value.

I used this to determine that the keyboard height on my iPad Air is 265 pixels in portrait mode and 352 pixels in landscape mode. Just keep tapping the screen to lower the red line. When it disappears behind the keyboard, that’s the height. You might have to change the starting value of val depending on the device.

function setup()
    showKeyboard()
    val=370
    fill(255)
    noSmooth()
end

function draw()
    background(40, 40, 50)
    stroke(255, 0, 0, 255)
    strokeWidth(1)
    line(0,val,WIDTH,val)
    text(val,WIDTH/2,HEIGHT-100)
end

function touched(t)
    if t.state==BEGAN then
        val=val-1
    end
end

Here is a version of this program, but with more comfortable controls

function setup() showKeyboard() val = 370 fill(255) noSmooth() parameter.boolean("TapMode", false) parameter.boolean("Floor", true, function (v) val = v and math.floor(val) or val end) parameter.action("Pasteboard", function () pasteboard.copy(val) end) parameter.action("Show keyboard", showKeyboard) end

function draw() background(0, 0, 0, 255) stroke(255, 0, 0, 255) strokeWidth(1) line(0, val, WIDTH, val) fontSize(40) text(val, WIDTH/2, HEIGHT-100) if TapMode then stroke(255, 128) line(WIDTH/2, 0, WIDTH/2, HEIGHT) fontSize(20) text("decrease", WIDTH/4, HEIGHT-HEIGHT/4) text("increase", WIDTH-WIDTH/4, HEIGHT-HEIGHT/4) else fontSize(20) text("drag up/down", WIDTH/4, HEIGHT-HEIGHT/4) end end

function touched(t) if TapMode then if t.state == BEGAN then val = val + (t.x < WIDTH/2 and -1 or 1) end else val = Floor and math.floor(val + t.deltaY) or val + t.deltaY end end

@Picalines Works Great.

What would be nice is to have a keyboard.height variable that has the keyboard height depending on the device being used.

Here’s another way to get the keyboard height. It’s a little more involved than the above code, but interesting anyways. Run this program and without doing anything, take a screenshot of the black screen and keyboard. Exit the program and go to Assets Dropbox. From photos, select the screenshot of the keyboard and name it keyboard. Run this program again and it will display the photo of the black screen and keyboard. Tap the screen and a red line will appear down the middle of the screen. When it finds the top of the keyboard it will show the top pixel value of the keyboard.

displayMode(FULLSCREEN)

function setup()
    showKeyboard()
    y=0
    fill(255)
end

function draw()
    background(0, 0, 0, 255)
    if img~=nil then 
        sprite(img,WIDTH/2,HEIGHT/2)
        text("top pixel of keyboard is "..y,WIDTH/2,HEIGHT-100)
    end
end

function touched(t)
    if t.state==BEGAN then
        img=readImage("Dropbox:keyboard")
        y=HEIGHT
        x=WIDTH/2
        while true do
            r,g,b,a=img:get(x,y)
            if r~=0 and g~=0 and g~=0 then
                y=y-1
                return
            end
            img:set(x,y,255,0,0,255)
            y=y-1
        end
    end
end

I’ll be adding keyboard height and height change notifications to the layout module soon, sorry for the omission