Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send KeyEvents through an input method service to a Dialog, or a Spinner menu?

I'm trying to implement an input method service that receives intents sent by a remote client, and in response to those sends an appropriate KeyEvent.

I'm using in the input method service this method

private void keyDownUp(int keyEventCode) 
{
    getCurrentInputConnection().sendKeyEvent(
              new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
    getCurrentInputConnection().sendKeyEvent(
              new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));
}

to send KeyEvents as in the Simple Sofykeyboard Sample, and it works in the home, in Activities... but it doesn't works when a Dialog or the menu of a Spinner is in foreground.

The events is sent to the parent activity behind the Dialog.

Is there any way to send keys and control the device like using the hardware keys from an input method?

Better explanation on what I'm trying to do:

I am kind of writng an Input Method that allows to control the device from remote.

I write in a client (a java application on my desktop pc) a command (for example "UP"), a server on the device with sendBroadcast() sends the intent with the information, and a receiver in the input method gets it and call keyDownUp with the keycode of the DPAD_UP key. It generally works, but when I go to an app that shows a dialog, the keyDownUp method don't sends the key event to the dialog, for example for select the yes or not buttons, but it keeps to control the activty behind the Dialog.

Here I have found someone with my same problem... but no answer...

like image 868
shutdown11 Avatar asked Nov 14 '22 11:11

shutdown11


1 Answers

First, let me explain what I did understand.

  • You go to an app
  • You open a Dialog in that activity (For example go to sms app, long press a thread)
  • You press the HOME key.
  • Go to a different application that sends the intent
  • The IME gets the intent, and writes something down.

I don't understand how the activity with the Dialog gets opened again.

AFAIK, when you press a key in the softKeyboard, IME's onKey(int primaryCode, int[] keyCodes) is called and when you press a hard key the IME is called at:

  • public boolean onKeyUp(int keyCode, KeyEvent event)
  • public boolean onKeyDown(int keyCode, KeyEvent event)

You can try calling that methods instead, but I don't know if emulating a hard key would fix it. I guess it's a focus issue. Did you tried getting the text from getCurrentInputConnection() to see where it is standing?

like image 99
Macarse Avatar answered Jan 03 '23 21:01

Macarse