12
Planned

Add Keyboard "Is shown" and "Close" Actions

We currently have an action to show the soft keyboard. However, there is currently no way to detect if the keyboard is already showing. Some way to detect this state has been requested several times on various Tasker forums.

I request an action to close the keyboard, if currently shown, and an action that returns the current state of the keyboard.

8 replies

AB

For anyone interested over years, a workaround may help to close the keyboard if opened..

I just created an "Input Text" behind another item and focused to it; then simulated "back"

PC

Hi guys,

I know I'm a couple of years late in seeing this. This is how I'm doing this. I don't know if it would be helpful for anyone else or not since it's using logcat.

  Profile: Monitor Keyboard
Settings: Restore: no
Event: Logcat Entry [ Output Variables:* Component:StatusBar/NavBarView Filter:showImeSwitcher Grep Filter (Check Help):On ]

Enter Task: Anon

<Keyboard Opened>
A1: If [ %lc_text ~ *showImeSwitcher:true* ]

<Keyboard Closed>
A2: Else
If [ %lc_text ~ *showImeSwitcher:false* ]

A3: End If

RR

@parosen

Most devices will show a "Select keyboard" notification when the keyboard is displayed. You could trap that using Tasker's Notification and Notification Removed events.

P

Thank you Robert. This solution was the first one I have used till 2015 more or less. Then my cell phone stop showing the notification, just put a minikeyboard icon at the bottom. Then I used the Autoinput focus which is not reliable, and then finally found the Java one you have posted here. It was working well till I changed my phone to a new one with Android 11. On the other hand it is supposed that Android 11 has methods that can be called through Java to know whether the keyboard is present, and I hoped you could send light to the darkness 😊  

F

I tried the task "Test keyboard" posted above from , but unfortunately, "Tasker" shows an error "no matching method for isInputMethodShown" :-(

I then tried it with "isInputViewShown()" instead, as Robert mentioned that "In the AOSP the function is called isInputViewShown() whereas in Tasker it is called isInputMethodShown()" - but that produced the same error message...

So the question is: has "Tasker" dropped the support of these Java Functions? I'm running "Tasker v5.6" on Android Pie and using Xiaomi's Stock ROM on the Pocophone F1 smarthone.

Any suggestion?

RR

Still works correctly on my Samsung S9 with Pie and latest Tasker beta. It is more likely that Xiaomi removed the functionality from their ROM.

RR

I may have found a way to do a keyboard state context. Check out this link on stackoverflow.

Thank you, but unfortunately that works only when the app is open, and not in the background like we wanted :)

RR

I know you already declined this, but there is an API available. Sorry for the late reply, but I just discovered that this works. At least it seems to on my Galaxy S9. Returns true if the keyboard is shown and false if it is not

        Test Keyboard
        
    A1: Soft Keyboard 
        
    A2: Wait 
        MS: 100 
        Seconds: 0 
        Minutes: 0 
        Hours: 0 
        Days: 0 
        
    A3: Java Function 
        Return: input 
        Class Or Object: InputMethodManager 
        Function: getInstance
        {InputMethodManager} () 
        
    A4: Java Function 
        Return: %shown 
        Class Or Object: input 
        Function: isInputMethodShown
        {boolean} () 
        
    A5: Flash 
        Text: %shown 
        Long: Off 

Interesting, thanks! :) How did you find out about that function?

But for that to work as a state I would need to add a monitor that would call it every X seconds, which would make it rather inefficient. You don't happen to know of an event or trigger that happens around the same time the keyboard is shown/hidden, do you? :)

RR

After you initially declined, I decided to dig through the Android Open Source Project to see if I could find a workaround. This eventually lead me to this file, which contains the isInputMethodShown() function. I then went to the Java Function action in Tasker to see if this function was supported there.

Unfortunately, I could not find any broadcast intent or event that occurs when the keyboard is shown/hidden. I think you are correct that a state is probably not possible.

Weird, I cannot find "isInputMethodShown" on that page... Is it really there? Sorry if I'm missing something obvious...

RR

Ah, I see what happened. In the AOSP the function is called isInputViewShown() whereas in Tasker it is called isInputMethodShown(). You can find the function at line 1438 of the file.

When I looked up the function in Tasker, I must have made a subconscious leap between the the two names. Also it shows that the AOSP isn't always up to date. Sorry for the confusion.

Where does Tasker get its list of functions from? This isn't the first time I've found a useful function in Tasker that was undocumented.

Tasker looks at what actually is available in a process that's called "Reflection". :) Thanks for the extra info!

So this feature request can be broken down to "Action to check input state?"

RR

Yes, I think a "Test Keyboard" action would meet most user needs.

Maybe add it in the "Test System" action?

RR

Wouldn't it be more logical to put it in the Input category? The System category seems more for actions that have no other home.

Ok, makes sense. Can you please just edit the description of the request, just to say that you want a query and not a state? Thanks again!

RR

I would, but I can't find a way to edit the original post. There is no "edit" button that I can see.

Oh sorry was not aware. I edited it for you :) Is that ok?

Did you happen to find a function to hide the keyboard as well?

RR

It looks like the forceHideSoftInput() method will close the window:

        Test     

    A1: Java Function 

        Return: input 

        Class Or Object: InputMethodManager 

        Function: getInstance

        {InputMethodManager} ()       

    A2: Soft Keyboard   

    A3: Wait 

        MS: 0 

        Seconds: 3 

        Minutes: 0 

        Hours: 0 

        Days: 0  

    A4: Java Function 

        Class Or Object: input 

        Function: forceHideSoftInput

        {boolean} () 

I could not find this function in the AOSP, but it works on my device.

Thank you very much! :)

P

HI Robert, I was using your method till upgrading to Android 11. It's not working anymore (it seems like isInputMethodShown is gone). Do you know an alternative? Thanks in advance!

RR

This is not reliable since a text element can have focus with the keyboard closed. Plus these really should be native actions and states under the Input category.

I understand, but there is no native API to detect this as far as I know. I think the AutoInput workaround is your best bet unfortunately..

You can kind of do this with the AutoInput UI Action event condition, by monitoring the "Input Element Focused" and "Focus Lost" types. Check out this example.

S

Another workaround to check keyboard visibility is using the Notification/Notification removed events for the 'Select keyboard' notification. However, this is not language independent.

But Tasker could look for notifications from Android System.Triggered by this it could do the check with the API functions described above to implement keyboard shown/hidden events :)