Code example for KeyEvent

        sendKeySync(new KeyEvent(
            KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN));
        sendKeySync(new KeyEvent(
            KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN));
        sendKeySync(new KeyEvent(
            KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER));
        sendKeySync(new KeyEvent(
            KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
        // Was the expected activity started? 
        if (checkMonitorHit(am, 1)) {
            Log.i("ContactsSelectInstrumentation", "Activity started!");
        } else { 
            Log.i("ContactsSelectInstrumentation", "*** ACTIVITY NOT STARTED!");
        // And we are done! 
        Log.i("ContactsSelectInstrumentation", "Done!");
        finish(Activity.RESULT_OK, null);
Connect your IDE to all the code out there  Get Codota for Java