Android: Is there a way to simulate D-Pad events (API 10)?

The problem is very simple. I have to simulate dpad events (UP, DOWN, RIGHT, LEFT, CENTER) for navigation in my GUI, which consists of many buttons and other elements. Using the D-Pad simulator, I can navigate this graphical interface without a line code. But how can I do this programmatically?

I tried a lot without success:

  • KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_LEFT); View.dispatchKeyEvent(event); Nothing happens (focus should move one element to the right)

  • I also read a lot about windowManager.injectKeyEvent, but found nothing.

  • And Instrumentation can help simulate keyevents, but more for testing, and not for use in the application itself.

I think there is a solution because talkback can mimic the physical D-Pad ( http://code.google.com/p/eyes-free/source/browse/trunk/ime/latinime/src/com/googlecode/eyesfree /inputmethod/latin/LatinIME.java )

+6
source share
3 answers

I found an intelligent solution (e.g. to go down):

 bic=new BaseInputConnection(this.getWindow().getDecorView(),false); KeyEvent event2 = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN,0, KeyEvent.META_SYM_ON, 0, 0, KeyEvent.FLAG_VIRTUAL_HARD_KEY); bic.sendKeyEvent(event2); 

That's all, an internal algorithm for finding the next element in your chosen direction

+2
source

You have two ways to achieve your goal:

At first,

 Instrumentation inst=new Instrumentation(); inst.sendKeyDownUpSync(int keycode); 

Prerequisites: in the same process.

Secondly, for example, wo want to simulate KeyEvent.KEYCODE_DPAD_UP

 getCurrentInputConnection().sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_UP)); 

Prerequisites: Be sure to bind inputmethd

+1
source

Try using KeyEvent.ACTION_UP instead of ACTION_DOWN. Solved a similar question for me.

0
source

Source: https://habr.com/ru/post/904119/


All Articles