dimanche 3 février 2019

Dispatch soft keyboard IME action with injectInputEvent

According to this answer I'm trying to dispatch keyboard IME action with InputManager and reflection.

private void pressSearch(){
    Class[] paramTypes = new Class[2];
    paramTypes[0] = InputEvent.class;
    paramTypes[1] = Integer.TYPE;

    Object[] params = new Object[2];
    params[0] = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SEARCH);
    params[1] = 0;//INJECT_INPUT_EVENT_MODE_ASYNC

    try {
        Method hiddenMethod = inputManager.getClass().getMethod("injectInputEvent", paramTypes);
        hiddenMethod.invoke(inputManager, params);
    } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        e.printStackTrace();
    }


    params[0] = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_SEARCH);
    params[1] = 0;//INJECT_INPUT_EVENT_MODE_ASYNC

    try {
        Method hiddenMethod = inputManager.getClass().getMethod("injectInputEvent", paramTypes);
        hiddenMethod.invoke(inputManager, params);
    } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        e.printStackTrace();
    }

}

When I use it, I'm getting this error:

W/InputDispatcher: Asynchronous input event injection failed.
I/InputDispatcher: Dropped event because it is stale.





Aucun commentaire:

Enregistrer un commentaire