Send the key combination (meta key and code key) using the Android API

I cannot figure out how to send a combination of a meta key (e.g. CTRL) and a key code (e.g. for RETURN) with Android (I use API level 11 = version 3.0).

The KeyEvent class documentation mentions constants, such as META_CTRL_ON, and also supports key encoding constants (for example, KEYCODE_CTRL_LEFT) for meta keys.

I use the Javascript Key Event Tester to check the output generated by my input method editor (IME). BTW, my goal is to develop a software keyboard.

If I understand the documentation correctly, it is enough to execute the following code to send the CTRL key only :

this.sendDownUpKeyEvents(KeyEvent.KEYCODE_CTRL_RIGHT); 

But when it runs against the Javascript Key Event Tester (see above), nothing happens.

Therefore, I need to understand 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 (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. ) ); } 

It also encounters the same problem as above: The only recognized key is ENTER.

For several hours I searched the Internet for several examples of how to use the KeyEvent class with meta keys and / or key combinations, but could not find at least one example code.

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

Thank you in advance!

+6
source share
1 answer

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

for example KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON .

0
source

Source: https://habr.com/ru/post/894909/


All Articles