Java Code Examples for android.view.InputDevice#SOURCE_KEYBOARD

The following examples show how to use android.view.InputDevice#SOURCE_KEYBOARD . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 *
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int source = event.getSource();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(event);
    newEvent.setTime(downTime, eventTime);
    newEvent.setSource(source);
    newEvent.setFlags(event.getFlags() | KeyEvent.FLAG_FROM_SYSTEM);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 2
Source File: InteractionController.java    From za-Farmer with MIT License 6 votes vote down vote up
/**
 * Send keys and blocks until the first specified accessibility event.
 *
 * Most key presses will cause some UI change to occur. If the device is busy, this will
 * block until the device begins to process the key press at which point the call returns
 * and normal wait for idle processing may begin. If no events are detected for the
 * timeout period specified, the call will return anyway with false.
 *
 * @param keyCode
 * @param metaState
 * @param eventType
 * @param timeout
 * @return true if events is received, otherwise false.
 */
public boolean sendKeyAndWaitForEvent(final int keyCode, final int metaState,
        final int eventType, long timeout) {
    Runnable command = new Runnable() {
        @Override
        public void run() {
            final long eventTime = SystemClock.uptimeMillis();
            KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN,
                    keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
                    InputDevice.SOURCE_KEYBOARD);
            if (injectEventSync(downEvent)) {
                KeyEvent upEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP,
                        keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
                        InputDevice.SOURCE_KEYBOARD);
                injectEventSync(upEvent);
            }
        }
    };

    return runAndWaitForEvents(command, new WaitForAnyEventPredicate(eventType), timeout)
            != null;
}
 
Example 3
Source File: InteractionController.java    From za-Farmer with MIT License 6 votes vote down vote up
public boolean sendKey(int keyCode, int metaState) {
    if (DEBUG) {
        Log.d(LOG_TAG, "sendKey (" + keyCode + ", " + metaState + ")");
    }

    final long eventTime = SystemClock.uptimeMillis();
    KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN,
            keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
            InputDevice.SOURCE_KEYBOARD);
    if (injectEventSync(downEvent)) {
        KeyEvent upEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP,
                keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
                InputDevice.SOURCE_KEYBOARD);
        if(injectEventSync(upEvent)) {
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: InteractionController.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Send keys and blocks until the first specified accessibility event.
 * 
 * Most key presses will cause some UI change to occur. If the device is
 * busy, this will block until the device begins to process the key press at
 * which point the call returns and normal wait for idle processing may
 * begin. If no events are detected for the timeout period specified, the
 * call will return anyway with false.
 * 
 * @param keyCode
 * @param metaState
 * @param eventType
 * @param timeout
 * @return true if events is received, otherwise false.
 */
public boolean sendKeyAndWaitForEvent(final int keyCode,
		final int metaState, final int eventType, long timeout) {
	Runnable command = new Runnable() {
		@Override
		public void run() {
			final long eventTime = SystemClock.uptimeMillis();
			KeyEvent downEvent = new KeyEvent(eventTime, eventTime,
					KeyEvent.ACTION_DOWN, keyCode, 0, metaState,
					KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
					InputDevice.SOURCE_KEYBOARD);
			if (injectEventSync(downEvent)) {
				KeyEvent upEvent = new KeyEvent(eventTime, eventTime,
						KeyEvent.ACTION_UP, keyCode, 0, metaState,
						KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
						InputDevice.SOURCE_KEYBOARD);
				injectEventSync(upEvent);
			}
		}
	};

	return runAndWaitForEvents(command, new WaitForAnyEventPredicate(
			eventType), timeout) != null;
}
 
Example 5
Source File: InteractionController.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
public boolean sendKey(int keyCode, int metaState) {
	if (DEBUG) {
		Log.d(LOG_TAG, "sendKey (" + keyCode + ", " + metaState + ")");
	}

	final long eventTime = SystemClock.uptimeMillis();
	KeyEvent downEvent = new KeyEvent(eventTime, eventTime,
			KeyEvent.ACTION_DOWN, keyCode, 0, metaState,
			KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
			InputDevice.SOURCE_KEYBOARD);
	if (injectEventSync(downEvent)) {
		KeyEvent upEvent = new KeyEvent(eventTime, eventTime,
				KeyEvent.ACTION_UP, keyCode, 0, metaState,
				KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
				InputDevice.SOURCE_KEYBOARD);
		if (injectEventSync(upEvent)) {
			return true;
		}
	}
	return false;
}
 
Example 6
Source File: AndroidInputHandler.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
    public boolean onKey(View view, int keyCode, KeyEvent event) {
        if (view != getView()) {
            return false;
        }

        boolean consumed = false;

        int source = event.getSource();
//        logger.log(Level.INFO, "onKey source: {0}", source);

        boolean isTouch =
                ((source & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) ||
                ((source & InputDevice.SOURCE_KEYBOARD) == InputDevice.SOURCE_KEYBOARD);
//        logger.log(Level.INFO, "onKey source: {0}, isTouch: {1}",
//                new Object[]{source, isTouch});

        if (touchInput != null) {
            consumed = touchInput.onKey(event);
        }

        return consumed;

    }
 
Example 7
Source File: MyInteractionController.java    From PUMA with Apache License 2.0 6 votes vote down vote up
/**
 * Send keys and blocks until the first specified accessibility event.
 *
 * Most key presses will cause some UI change to occur. If the device is busy, this will
 * block until the device begins to process the key press at which point the call returns
 * and normal wait for idle processing may begin. If no events are detected for the
 * timeout period specified, the call will return anyway with false.
 *
 * @param keyCode
 * @param metaState
 * @param eventType
 * @param timeout
 * @return true if events is received, otherwise false.
 */
public boolean sendKeyAndWaitForEvent(final int keyCode, final int metaState, final int eventType, long timeout) {
	Runnable command = new Runnable() {
		@Override
		public void run() {
			final long eventTime = SystemClock.uptimeMillis();
			KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD);
			if (injectEventSync(downEvent)) {
				KeyEvent upEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP, keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD);
				injectEventSync(upEvent);
			}
		}
	};

	return runAndWaitForEvents(command, new WaitForAnyEventPredicate(eventType), timeout) != null;
}
 
Example 8
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 9
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 10
Source File: Instrumentation.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 11
Source File: InputHelper.java    From XposedSmsCode with GNU General Public License v3.0 5 votes vote down vote up
/**
 * refer: com.android.commands.input.Input#sendText()
 *
 * @throws Throwable throwable throws if the caller has no android.permission.INJECT_EVENTS permission
 */
public static void sendText(String text) throws Throwable {
    int source = InputDevice.SOURCE_KEYBOARD;

    StringBuilder sb = new StringBuilder(text);

    boolean escapeFlag = false;
    for (int i = 0; i < sb.length(); i++) {
        if (escapeFlag) {
            escapeFlag = false;
            if (sb.charAt(i) == 's') {
                sb.setCharAt(i, ' ');
                sb.deleteCharAt(--i);
            }
        }
        if (sb.charAt(i) == '%') {
            escapeFlag = true;
        }
    }

    char[] chars = sb.toString().toCharArray();

    KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
    KeyEvent[] events = kcm.getEvents(chars);
    for (KeyEvent keyEvent : events) {
        if (source != keyEvent.getSource()) {
            keyEvent.setSource(source);
        }
        injectKeyEvent(keyEvent);
    }
}
 
Example 12
Source File: KeyButtonView.java    From GravityBox with Apache License 2.0 5 votes vote down vote up
void sendEvent(int action, int flags, long when, boolean applyDefaultFlags) {
    try {
        final int repeatCount = (flags & KeyEvent.FLAG_LONG_PRESS) != 0 ? 1 : 0;
        if (applyDefaultFlags) {
            flags |= KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY;
        }
        final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, repeatCount,
                0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, flags,
                InputDevice.SOURCE_KEYBOARD);
        final Object inputManager = XposedHelpers.callStaticMethod(InputManager.class, "getInstance");
        XposedHelpers.callMethod(inputManager, "injectInputEvent", ev, 0);
    } catch (Throwable t) {
        XposedBridge.log(t);
    }
}
 
Example 13
Source File: Instrumentation.java    From droidel with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 14
Source File: AndroidInputHandler14.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
    public boolean onKey(View view, int keyCode, KeyEvent event) {
        if (view != getView()) {
            return false;
        }

        boolean consumed = false;

//        logger.log(Level.INFO, "onKey keyCode: {0}, action: {1}, event: {2}",
//                new Object[]{KeyEvent.keyCodeToString(keyCode), event.getAction(), event});
        int source = event.getSource();
//        logger.log(Level.INFO, "onKey source: {0}", source);

        boolean isTouch =
                ((source & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) ||
                ((source & InputDevice.SOURCE_KEYBOARD) == InputDevice.SOURCE_KEYBOARD);
        boolean isJoystick =
                ((source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) ||
                ((source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK);
        boolean isUnknown =
                (source & android.view.InputDevice.SOURCE_UNKNOWN) == android.view.InputDevice.SOURCE_UNKNOWN;

        if (touchInput != null && (isTouch || (isUnknown && this.touchInput.isSimulateKeyboard()))) {
//            logger.log(Level.INFO, "onKey source: {0}, isTouch: {1}",
//                    new Object[]{source, isTouch});
            consumed = touchInput.onKey(event);
        }
        if (isJoystick && joyInput != null) {
//            logger.log(Level.INFO, "onKey source: {0}, isJoystick: {1}",
//                    new Object[]{source, isJoystick});
            // use inclusive OR to make sure the onKey method is called.
            consumed = consumed | ((AndroidJoyInput14)joyInput).onKey(event);
        }

        return consumed;

    }
 
Example 15
Source File: MyInteractionController.java    From PUMA with Apache License 2.0 5 votes vote down vote up
public boolean sendKey(int keyCode, int metaState) {
	if (DEBUG) {
		Log.d(LOG_TAG, "sendKey (" + keyCode + ", " + metaState + ")");
	}

	final long eventTime = SystemClock.uptimeMillis();
	KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD);
	if (injectEventSync(downEvent)) {
		KeyEvent upEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP, keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD);
		if (injectEventSync(upEvent)) {
			return true;
		}
	}
	return false;
}