Detect keyboard height

#build.settings settings = { orientation = { default = "portrait", supported = { "portrait", } }, iphone = { plist = { UIStatusBarHidden = false, UIPrerenderedIcon = true, -- set to false for "shine" overlay CoronaWindowMovesWhenKeyboardAppears = true, } }, android = { CoronaWindowMovesWhenKeyboardAppears = true, }, } #config.lua application = { content = { width = 640, height = 960, scale = "letterBox", fps = 60, }, #main.lua local rect1=display.newRect(0,0,300,100) local rect2=display.newRect(0,0,300,100) local textfield=native.newTextField(0,0,300,100) rect1.fill={0.6,0.6,0.6} rect2.fill={0.6,0.6,0.6} rect1.x=display.contentCenterX rect2.x=rect1.x textfield.x=rect1.x rect1.y=rect1.height/2 rect2.y=display.contentCenterY+200 textfield.y=display.contentCenterY+200 textfield.hasBackground=false local path=system.pathForFile("log.txt",system.DocumentsDirectory) local f=io.open(path,"w") f:write("standart textfield y: "..textfield.y.." "..textfield.x) f:write("\n") f:close() local function inputevent(event) if event.phase=="began" then local f=io.open(path,"a+") f:write("began textfield y: "..textfield.y.." "..textfield.x) f:write("\n") f:close() elseif event.phase=="submitted" then local f=io.open(path,"a+") f:write("submitted textfield y: "..textfield.y.." "..textfield.x) f:write("\n") f:close() native.setKeyboardFocus(nil) elseif event.phase=="ended" then local f=io.open(path,"a+") f:write("ended textfield y: "..textfield.y.." "..textfield.x) f:write("\n") f:close() end end textfield:addEventListener("userInput",inputevent)

Rob
Every event.phase i have textfield.y=680

For those who have read this post and are:

  • Confused about how to write code that does this
  • Don’t understand the differences between iOS and Android
  • Do not know how to handle fields that slide off screen

Download this example and run it on your device:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/10/keyboardPanning.zip

https://www.youtube.com/watch?v=hLbUW0ekuRQ&feature=youtu.be

By default, line 7 in main.lua is set to ‘true’.   This tells the example to automatically cycle through the focused fields so you can watch how the devices handle panning and off-screen fields.  Turn it to false if you want to interact with the example.

local enableAutoFocus = true

Please note: On iOS, if a field slides offscreen, you can access it by setting the keyboard focus first to ‘nil’, then to the field you want to access.  On android you do not want to set the focus to ‘nil’ or the keyboard will wiggle a bit which is ugly.

-Ed

Since you are using the method were we scroll the active scrollView into where it’s just above the keyboard, you should be able to know which field that is. Lets say its a field named emailField. Then it’s vertical location would be found at:

emailField.y

Rob

I’m not sure if this is a bug report or if I should submit this as a feature request, but when I use: CoronaWindowMovesWhenKeyboardAppears=true

it works fine with the default keyboard, but some of my users have reported that the window does not shift up when they use their own custom keyboards like “Swift Key” or “Gboard” on iOS 9. .

CoronaWindowMovesWhenKeyboardAppears is an experimental feature. If it works for you great, if not, then we suggest finding alternate ways to move your native input objects. They can be inserted into groups and the whole group now.

There are no plans to go back and work more on this auto-panning feature.

Rob

Thanks for the quick response Rob. Just to clarify there is still no way to get the keyboard height at the moment right? It’s kind of a guessing game on where to move the native input object?

Correct

CoronaWindowMovesWhenKeyboardAppears=true

Rob, How can i detect offset of view when my textfieild is focused?

You should be able to get the .x and .y of the text field.

Rob

#build.settings settings = { orientation = { default = "portrait", supported = { "portrait", } }, iphone = { plist = { UIStatusBarHidden = false, UIPrerenderedIcon = true, -- set to false for "shine" overlay CoronaWindowMovesWhenKeyboardAppears = true, } }, android = { CoronaWindowMovesWhenKeyboardAppears = true, }, } #config.lua application = { content = { width = 640, height = 960, scale = "letterBox", fps = 60, }, #main.lua local rect1=display.newRect(0,0,300,100) local rect2=display.newRect(0,0,300,100) local textfield=native.newTextField(0,0,300,100) rect1.fill={0.6,0.6,0.6} rect2.fill={0.6,0.6,0.6} rect1.x=display.contentCenterX rect2.x=rect1.x textfield.x=rect1.x rect1.y=rect1.height/2 rect2.y=display.contentCenterY+200 textfield.y=display.contentCenterY+200 textfield.hasBackground=false local path=system.pathForFile("log.txt",system.DocumentsDirectory) local f=io.open(path,"w") f:write("standart textfield y: "..textfield.y.." "..textfield.x) f:write("\n") f:close() local function inputevent(event) if event.phase=="began" then local f=io.open(path,"a+") f:write("began textfield y: "..textfield.y.." "..textfield.x) f:write("\n") f:close() elseif event.phase=="submitted" then local f=io.open(path,"a+") f:write("submitted textfield y: "..textfield.y.." "..textfield.x) f:write("\n") f:close() native.setKeyboardFocus(nil) elseif event.phase=="ended" then local f=io.open(path,"a+") f:write("ended textfield y: "..textfield.y.." "..textfield.x) f:write("\n") f:close() end end textfield:addEventListener("userInput",inputevent)

Rob
Every event.phase i have textfield.y=680

Since you are using the method were we scroll the active scrollView into where it’s just above the keyboard, you should be able to know which field that is. Lets say its a field named emailField. Then it’s vertical location would be found at:

emailField.y

Rob

I’m not sure if this is a bug report or if I should submit this as a feature request, but when I use: CoronaWindowMovesWhenKeyboardAppears=true

it works fine with the default keyboard, but some of my users have reported that the window does not shift up when they use their own custom keyboards like “Swift Key” or “Gboard” on iOS 9. .

CoronaWindowMovesWhenKeyboardAppears is an experimental feature. If it works for you great, if not, then we suggest finding alternate ways to move your native input objects. They can be inserted into groups and the whole group now.

There are no plans to go back and work more on this auto-panning feature.

Rob

Thanks for the quick response Rob. Just to clarify there is still no way to get the keyboard height at the moment right? It’s kind of a guessing game on where to move the native input object?

Correct