开发者

Convert unicode value to KeyEvent constant in OnKeyboardActionListener.onKey

开发者 https://www.devze.com 2023-03-27 09:45 出处:网络
I am new to Android development and currently programming an Input Method Editor (IME) for the Google Android operating system (API level 11 == version 3.0).

I am new to Android development and currently programming an Input Method Editor (IME) for the Google Android operating system (API level 11 == version 3.0).

(The following code is simplified to point out the problem.)

I am able to send characters to the underlying application via:

@Override
public final void onKey(final int primaryCode, final int[] keyCodes) {    
    this.getCurrentInputConnection().commitText(String.valueOf((char) primaryCode), 1);
}

Now I want to send special key combinations (e.g. SHIFT + A). The Java code to reach this goal is the following (for the special case of SHIFT + A):

@Override
public final void onKey(final int primaryCode, final int[] keyCodes) {
        long eventTime = SystemClock.uptimeMillis();    
        this.sendDownKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT);

        KeyEvent keyEvent = new KeyEvent(
            eventTime,
            eventTime,
            KeyEvent.ACTION_DOWN,
            KeyEvent.KEYCODE_A, // ToDo: How to convert primaryCode to the corresponding KeyEvent constant?
            KeyEvent.META_SHIFT_ON
        );
        this.getCurrentInputConnection().sendKeyEvent(keyEvent);
}

public final void sendDownKeyEvent(final int keyEventCode) {
    InputConnection ic = this.getCurrentInputConnection();
    if (ic != null) {
        long eventTime = SystemClock.uptimeMillis();
        ic.sendKeyEvent(
            new KeyEvent(
                eventTime, eventTime,
                KeyEvent.ACTION_DOWN, keyEventCode, 0, 0, 0, 0,
                KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE
            )
        );
    }
}

The comment in the previous code sample shows my problem. To send key combina开发者_C百科tions via a KeyEvent object I have to convert the variable primaryCode (which contains the unicode code of the pressed key) to a constant of the class KeyEvent.

Does a convenient method for this case exist already or do I have to write it by myself? In general: Is the above solution to send combinations of keys elegant or do better approaches exist? (It isn't easy to find examples for Android IMEs on the Internet...)

Thank you in advance!


A little late it seems. I found the following solution to the same issue, translating a primaryCodeinto a sequence of KeyEvents.

The solution involves loading a KeyCharacterMap and using its KeyCharacterMap.getEvents() method to get a KeyEvent list that can reproduce the character.

//This snippet tries to translate the glyph 'primaryCode' into key events

//retrieve the keycharacter map from a keyEvent (build yourself a default event if needed)
KeyCharacterMap myMap=KeyCharacterMap.load(event.getDeviceId()); 

//event list to reproduce glyph
KeyEvent evs[]=null;

//put the primariCode into an array
char chars[]=new char[1];
chars[0]=primaryCode;

// retrieve the key events that could have produced this glyph
evs=myMap.getEvents(chars);

if (evs != null){
    // we can reproduce this glyph with this key event array
    for (int i=0; i< evs.length;i++) MySendKeyEventHelperMethod(evs[i]);
}
else { /* could not find a way to reproduce this glyph */ }
0

精彩评论

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