Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 254
Florian Wolters Avatar asked Aug 12 '11 12:08

Florian Wolters


People also ask

What is meta key in Android?

Tablets. The keyboard shortcuts in the Tab S3 refer to the meta key. You are able to hold the meta key and press enter to simulate the home button that you would normally press on the tablet when the keyboard is attached.

How do you use meta keys?

Use. Generally, the Meta key worked similar to Macintosh's Command key, in that when held down it modified letters and symbols into immediate commands (shortcuts). On these keyboards the Control key was placed closest to the space bar, then the Meta key outside Control.

What is the symbolic constant for Menu key?

The menu key for Android keyboard is constant value 82 which if you're using the emulator can be triggered using Ctrl + M .

What is key event?

An event which indicates that a keystroke occurred in a component. This low-level event is generated by a component object (such as a text field) when a key is pressed, released, or typed.


1 Answers

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.

like image 99
曾其威 Avatar answered Sep 17 '22 04:09

曾其威