How to know if the keyboard is visible?

vecino

Well-Known Member
Licensed User
Longtime User
Hi, I can turn visible or invisible virtual keyboard with "HideKeyboard" and "ShowKeyboard".
But how I can know if it is visible or not?

I would do something like:
B4X:
keyboard.visible = not (keyboard.visible)
Thank you very much and greetings.
 

vecino

Well-Known Member
Licensed User
Longtime User
Hi, thanks for the reply.
Could please any example?, I do not understand how to detect if it is visible or invisible by that event.
Thank you very much and greetings.
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
You can intercept this event and if NewHeight is smaller than OldHeight then the keyboard has just become visible.

Erel, i hate to correct you, but this is not technically correct. if NewHeight < OldHeight the keyboard IS visible, not necessarily JUST BECOME visible. i mention this only because some keyboards (I use smart keyboard pro) will not display the suggestion line above the keyboard until the user starts entering characters, thus causing the event to fire twice with NewHeight < OldHeight, once when the keyboard pops up and again when the user starts typing. this actually injects even more complication into an already convoluted mess. i wish ime was more friendly >sigh<
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Thanks, I think I understand, I really think it is not possible to know if the keyboard is visible or it is not without clicking anywhere.
Thank you very much and greetings.

What good would be something simple as a:
B4X:
if k.keyboard.isvisible then ...
:)
 
Upvote 0

TomA

Active Member
Licensed User
Longtime User
Hi, I can turn visible or invisible virtual keyboard with "HideKeyboard" and "ShowKeyboard".

Rather than worry about whether or not it is visible, an alternative is to first check whether or not there is a hardware keyboard. If there is, then the virtual keyboard can be ignored, if there is not, then use the Hidekeyboard and Showkeyboard in your app to control when the virtual keyboard shows and that way you know when it is actually visible. I do that in my Hunt the Wumpus app (https://play.google.com/store/apps/details?id=com.amansoft.huntthewumpus) and otherapps along with intercepting the HeightChanged event so I can adjust the display height accordingly and it works very well.
Tom Aman
 
Upvote 0

TomA

Active Member
Licensed User
Longtime User
Check for keyboard present

Under Globals, define a reflector:

B4X:
   Dim ref As Reflector

Then add a sub such as:

B4X:
Sub HardwareKeyboardPresent As Boolean
    ref.Target = ref.GetContext
    ref.Target = ref.RunMethod("getResources")
    ref.Target = ref.RunMethod("getConfiguration")
    Dim keyboard As Int = ref.GetField("keyboard")
    Return keyboard <> 1 'KEYBOARD_NOKEYS - return true if keyboard, else return false 
End Sub

Call the sub the first time through Activity_Create and store the result in a variable under Process_Globals (I used "Dim KeyboardPresent As Boolean") for future reference to use anytime you need to check if a hardware keyboard is present - using code such as:

To store whether or not the keyboard is present:

B4X:
     KeyboardPresent = HardwareKeyboardPresent

Then, when needed, use that to control whether or not to display the keyboard:

B4X:
If KeyboardPresent = False Then
     IME.ShowKeyboard (TextEdit)
    .
    .
End If

Intercepting the height changed event then lets you make any necessary adjustments to the display.

TomA
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello, I think I have not explained well, what I need is to know if it is visible the keyboard displayed on the screen.

Thanks and regards.
 

Attachments

  • betterkeyboard.jpg
    betterkeyboard.jpg
    16.5 KB · Views: 261
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
I've had problems with the soft keyboard by forcing it up using .showkeyboard when it was present already. its a good idea to first ensure its not present before issuing .showkeyboard.

I am building a class to wrap the ime library and add more functionality. I believe I will add the check for a hardware keyboard. thank you Tom for your example :)

Hello, I think I have not explained well, what I need is to know if it is visible the keyboard displayed on the screen.
vecino, I am working on that. I will publish it in a few days.
 
Last edited:
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
Vecino, paste the code from this post into a new class module in your code. I would name the new class module xIME. Then, in your code, replace your

B4X:
Dim [identifier] As IME

with

B4X:
Dim [identifier] As xIME

This way none of your existing code that uses the IME library will be broken. there will be three new properties: .ScreenHeight, .Visible, and .HardwareKeyboard. You can see the documentation in the code.

If your code does not use the IME library at all, it must be downloaded from this thread, then unzipped into your Additional libraries folder, then the list of libraries (the [Libs] tab in the lower right corner of the Basic4Android editor screen) must be refreshed, and the IME library checked with a checkmark. Then in your code, add to Sub Globals

B4X:
Dim IME as xIME

Add to Sub Activity_Create (not inside a FirstTime block)

B4X:
IME.Initialize("IME")
IME.AddHeightChangedEvent

then use IME.Visible anywhere in your code.


Please pay attention to the bug report in my post directly above the post that has the class code in it. Even if your code is not affected directly, the new .ScreenHeight and .Visible properties will no longer provide valid values.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello, in the testing I've done has worked well, it's just what I needed.
Thank you so much for the help, all comments have been very helpful me.
Greetings.
:)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
This is an old thread, I know, but maybe it can be still useful.

Combining these two informations...
You can intercept this event and if NewHeight is smaller than OldHeight then the keyboard has just become visible.

(@JesseW)
"A little helpful info concerning the HeightChanged event: the NewHeight parameter is the height of the now available part of the screen, not the new height of the keyboard itself."

the code should be:
B4X:
 Sub Ime1_HeightChanged (NewHeight As Int, OldHeight As Int)
    mKeyboardIsVisible = (NewHeight < 100%y)
End Sub
 
Upvote 0
Top