7

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

I have tried a lot with no success:

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

  • I have also read a lot about windowManager.injectKeyEvent but found nothing that works.

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

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

mkj
  • 2,761
  • 5
  • 24
  • 28
NitroBoarder
  • 93
  • 1
  • 5
  • I have found the talkback solution, they works with InputConnection. final InputConnection ic = getCurrentInputConnection(); But i have to send the D-Pad keyevents from an activity. – NitroBoarder Dec 19 '11 at 22:24

4 Answers4

3

Given

InputConnection ic = getCurrentInputConnection();

You can move the cursor in an EditText by simulating DPAD down/up events. The up event would probably be enough, but I am simulating the full event to be safe.

Left

ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT));
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_LEFT));

Right

ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_RIGHT));

Up

ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP));
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_UP));

Down

ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN));
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN));
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
2

You have two way to achieve your goal:

First,

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

Prerequisites:In the same process.

Second,for examble,wo want to simulate KeyEvent.KEYCODE_DPAD_UP

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

Prerequisites:Must bind inputmethd

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
lincoln
  • 21
  • 1
2

I found the smart solution (e.g for 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 to find the next element in your selected direction

mkj
  • 2,761
  • 5
  • 24
  • 28
NitroBoarder
  • 93
  • 1
  • 5
  • That doesn't work for me. sendKeyEvent always returns false which indicates the "BaseInputConnection is no longer valid". Whatever that is suppose to mean. – vangorra Apr 02 '14 at 21:05
1

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

markussvensson
  • 1,630
  • 1
  • 14
  • 18