Reputation: 587
How can I programmatically simulate a key press on a Droid? I would like to mimic a manual key press (appearing on the droid that someone is pressing a key but it is being done programmatically).
There are solutions out there involving IWindowManager
, but that isn't an option anymore in the new SDK.
Upvotes: 35
Views: 51745
Reputation: 43
Using instrumentation in my opinion doesn't work as intended, when editText is focused it sometimes causes soft keyboard to pop.
In my project i have a numeric keyboard fragment which should act like a normal keyboard, that's my way of achieving desired solution:
I tested this solution on 3 devices with android 7+:
Keyboard fragment onClick():
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button0:
simulateKeyPress(KeyEvent.KEYCODE_0);
break;
case R.id.button1:
simulateKeyPress(KeyEvent.KEYCODE_1);
break;
case R.id.button2:
simulateKeyPress(KeyEvent.KEYCODE_2);
break;
case R.id.button3:
simulateKeyPress(KeyEvent.KEYCODE_3);
break;
case R.id.button4:
simulateKeyPress(KeyEvent.KEYCODE_4);
break;
case R.id.button5:
simulateKeyPress(KeyEvent.KEYCODE_5);
break;
case R.id.button6:
simulateKeyPress(KeyEvent.KEYCODE_6);
break;
case R.id.button7:
simulateKeyPress(KeyEvent.KEYCODE_7);
break;
case R.id.button8:
simulateKeyPress(KeyEvent.KEYCODE_8);
break;
case R.id.button9:
simulateKeyPress(KeyEvent.KEYCODE_9);
break;
}
}
public void simulateKeyPress(int key){
Activity a = (Activity) getContext();
a.getWindow().getDecorView().getRootView();
BaseInputConnection inputConnection = new BaseInputConnection(a.getWindow().getDecorView().getRootView(),
true);
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, key);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, key);
inputConnection.sendKeyEvent(downEvent);
inputConnection.sendKeyEvent(upEvent);
}
This way i send the event to the root view of an activity and there it goes to the desired focused editText.
It's a bit rough solution but works fine.
Upvotes: 2
Reputation: 42427
If you're running a UI Automator test, there are two techniques you can use depending on the device's Android version:
If you only target API 18 or higher, than you can just use the shell:
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
device.executeShellCommand("input text 1234"); // Type '1234'
device.executeShellCommand("input keyevent 66"); // Press the Enter key
If you also support API 18-19, then you cannot use the shell because it's not available, and you cannot use instrumentation key injection if you're interacting with an app that's not your own, such as the system UI. Instead, use UiAutomation.injectInputEvent().
Grab an instance of UiAutomation
and store is somewhere:
UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
Then define some helper methods:
private void sendKey(int keyCode) {
sendKeyEvent(keyCode, KeyEvent.ACTION_DOWN);
sendKeyEvent(keyCode, KeyEvent.ACTION_UP);
}
private void sendKeyEvent(int keyCode, int action) {
long downTime = SystemClock.uptimeMillis();
KeyEvent event = new KeyEvent(
downTime,
downTime,
action,
keyCode,
0,
0,
KeyCharacterMap.VIRTUAL_KEYBOARD,
0,
KeyEvent.FLAG_FROM_SYSTEM,
InputDevice.SOURCE_KEYBOARD
);
uiAutomation.injectInputEvent(event, true);
}
Then use it like this:
sendKey(KeyEvent.KEYCODE_1);
sendKey(KeyEvent.KEYCODE_2);
sendKey(KeyEvent.KEYCODE_3);
sendKey(KeyEvent.KEYCODE_4);
sendKey(KeyEvent.KEYCODE_ENTER);
Upvotes: 3
Reputation: 676
If you have a view that want to consume the event you can use BaseInputConnection class and its sendKeyEvent method.
To use it you will need to specify a target view (e.g an EditText) that will receive the KeyEvent. For example:
EditText editText;
BaseInputConnection inputConnection = new BaseInputConnection(editText, true);
inputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POUND));
The result of this is like user would actually pressed # key (while having the edit text focused).
Upvotes: 15
Reputation: 50036
You can use instrumentation, ie following code called from onCreate of your activity will cause menu to be opened and closed multiple times:
new Thread(new Runnable() {
@Override
public void run() {
try {
Instrumentation inst = new Instrumentation();
for ( int i = 0; i < 10; ++i ) {
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
Thread.sleep(2000);
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
Thread.sleep(2000);
}
}
catch(InterruptedException e){
}
}
}).start();
...but I am not sure if this is what you are after
Upvotes: 48