开发者

Send a key combination (meta key and keycode) via the Android API

开发者 https://www.devze.com 2023-03-27 11:33 出处:网络
I can\'t figure out how to send a combination of a meta key (e.g. CTRL) and a keycode (e.g. for RETURN) with Android (I am using API level 11 = version 3.0).

I can't figure out how to send a combination of a meta key (e.g. CTRL) and a keycode (e.g. for RETURN) with Android (I am using API level 11 = version 3.0).

The documentation of the class KeyEvent mentions constants like META_CTRL_ON and also supports keycode constants (e.g. KEYCODE_CTRL_LEFT) for meta keys.

I am using the Javascript Key Event Tester to test the output that is generated by my Input Method Editor (IME). BTW, my goal is to develop a software keyboard.

If I understand the documentation correct, it would be sufficient to execute the following code to send the CTRL key only:

this.sendDownUpKeyEvents(KeyEvent.KEYCODE_CTRL_RIGHT);

But when this is executed against the Javascript Key Event Tester (see above), nothing happens.

So I need to get a clue how to send meta keys only and to send meta keys in combination with another key. I also tried the following to send SHIFT+ENTER (a concrete example):

private void _sendShiftEnter() {
    this.sendDownKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT);
    final long eventTime = SystemClock.uptimeMillis();
    this.getCurrentInputConnection().sendKeyEvent(
        new KeyEvent(
            eventTime, // The time (in uptimeMillis()) at which this key code originally went down.
            eventTime, // The time (in uptimeMillis()) at which this event happened.
            KeyEvent.ACTION_DOWN, // Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
            KeyEvent.KEYCODE_ENTER, // The key code.
            0, // A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.
            KeyEvent.META_SHIFT_ON, // Flags indicating which meta keys are currently pressed.
            0, // The device ID that generated the key event.
            0, // Raw device scan code of the event.
            KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE, // The flags for this key event.
            InputDevice.SOURCE_KEYBOARD // The input source such as SOURCE_KEYBOARD.
        )
    );
}

The same problem as above occurs here, too: The only recognized key is ENTER.

I already searched the Internet 开发者_如何转开发for several hours for examples, how to use the KeyEvent class with meta keys and/or key combinations, but couldn't find at least one example of code.

So, in conclusion: Has anyone experience with the KeyEvent class and can demonstrate me how to send a simple combination of keys (e.g. SHIFT+ENTER) via the Android API?

Thank you in advance!


I just put both meta key modifiers at the same time, and it worked...

for example KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON.

0

精彩评论

暂无评论...
验证码 换一张
取 消