From 581f324ed8314befdf7d8cf1c923791455cc11a5 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Fri, 21 Dec 2012 19:01:19 +0900 Subject: [PATCH] Initial implementation for decoding HW key events (B3) Bug: 5037589 Change-Id: I829f70a258de58ef8d2b836a9b435198a226e37f --- .../com/android/inputmethod/event/Event.java | 48 +++++++++++++++++++ .../event/HardwareKeyboardEventDecoder.java | 24 +++++++++- .../event/SoftwareEventDecoder.java | 7 ++- 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/java/src/com/android/inputmethod/event/Event.java b/java/src/com/android/inputmethod/event/Event.java index c96a3362d..827c0e792 100644 --- a/java/src/com/android/inputmethod/event/Event.java +++ b/java/src/com/android/inputmethod/event/Event.java @@ -29,8 +29,56 @@ package com.android.inputmethod.event; * The combiner should figure out what to do with this. */ public class Event { + // Should the types below be represented by separate classes instead? It would be cleaner + // but probably a bit too much + // An event we don't handle in Latin IME, for example pressing Ctrl on a hardware keyboard. + final public static int EVENT_NOT_HANDLED = 0; + // A character that is already final, for example pressing an alphabetic character on a + // hardware qwerty keyboard. + final public static int EVENT_COMMITTABLE = 1; + // A dead key, which means a character that should combine with what is coming next. Examples + // include the "^" character on an azerty keyboard which combines with "e" to make "ê", or + // AltGr+' on a dvorak international keyboard which combines with "e" to make "é". This is + // true regardless of the language or combining mode, and should be seen as a property of the + // key - a dead key followed by another key with which it can combine should be regarded as if + // the keyboard actually had such a key. + final public static int EVENT_DEAD = 2; + // A toggle event is triggered by a key that affects the previous character. An example would + // be a numeric key on a 10-key keyboard, which would toggle between 1 - a - b - c with + // repeated presses. + final public static int EVENT_TOGGLE = 3; + // A mode event instructs the combiner to change modes. The canonical example would be the + // hankaku/zenkaku key on a Japanese keyboard, or even the caps lock key on a qwerty keyboard + // if handled at the combiner level. + final public static int EVENT_MODE_KEY = 4; + + final private static int NOT_A_CODE_POINT = 0; + + private int mType; // The type of event - one of the constants above + // The code point associated with the event, if relevant. This is a unicode code point, and + // has nothing to do with other representations of the key. It is only relevant if this event + // is the right type: COMMITTABLE or DEAD or TOGGLE, but for a mode key like hankaku/zenkaku or + // ctrl, there is no code point associated so this should be NOT_A_CODE_POINT to avoid + // unintentional use of its value when it's not relevant. + private int mCodePoint; + static Event obtainEvent() { // TODO: create an event pool instead return new Event(); } + + public void setDeadEvent(final int codePoint) { + mType = EVENT_DEAD; + mCodePoint = codePoint; + } + + public void setCommittableEvent(final int codePoint) { + mType = EVENT_COMMITTABLE; + mCodePoint = codePoint; + } + + public void setNotHandledEvent() { + mType = EVENT_NOT_HANDLED; + mCodePoint = NOT_A_CODE_POINT; // Just in case + } } diff --git a/java/src/com/android/inputmethod/event/HardwareKeyboardEventDecoder.java b/java/src/com/android/inputmethod/event/HardwareKeyboardEventDecoder.java index 34ce1c3f4..9861816ee 100644 --- a/java/src/com/android/inputmethod/event/HardwareKeyboardEventDecoder.java +++ b/java/src/com/android/inputmethod/event/HardwareKeyboardEventDecoder.java @@ -16,10 +16,15 @@ package com.android.inputmethod.event; +import android.view.KeyCharacterMap; import android.view.KeyEvent; /** * A hardware event decoder for a hardware qwerty-ish keyboard. + * + * The events are always hardware keypresses, but they can be key down or key up events, they + * can be dead keys, they can be meta keys like shift or ctrl... This does not deal with + * 10-key like keyboards; a different decoder is used for this. */ public class HardwareKeyboardEventDecoder implements HardwareEventDecoder { final int mDeviceId; @@ -31,6 +36,23 @@ public class HardwareKeyboardEventDecoder implements HardwareEventDecoder { @Override public Event decodeHardwareKey(final KeyEvent keyEvent) { - return Event.obtainEvent(); + final Event event = Event.obtainEvent(); + // KeyEvent#getUnicodeChar() does not exactly returns a unicode char, but rather a value + // that includes both the unicode char in the lower 21 bits and flags in the upper bits, + // hence the name "codePointAndFlags". {@see KeyEvent#getUnicodeChar()} for more info. + final int codePointAndFlags = keyEvent.getUnicodeChar(); + if (keyEvent.isPrintingKey()) { + if (0 != (codePointAndFlags & KeyCharacterMap.COMBINING_ACCENT)) { + // A dead key. + event.setDeadEvent(codePointAndFlags & KeyCharacterMap.COMBINING_ACCENT_MASK); + } else { + // A committable character. This should be committed right away, taking into + // account the current state. + event.setCommittableEvent(codePointAndFlags); + } + } else { + event.setNotHandledEvent(); + } + return event; } } diff --git a/java/src/com/android/inputmethod/event/SoftwareEventDecoder.java b/java/src/com/android/inputmethod/event/SoftwareEventDecoder.java index 0b80d5c0e..d81ee0b37 100644 --- a/java/src/com/android/inputmethod/event/SoftwareEventDecoder.java +++ b/java/src/com/android/inputmethod/event/SoftwareEventDecoder.java @@ -17,7 +17,12 @@ package com.android.inputmethod.event; /** - * An event decoder for software events. + * An event decoder for events out of a software keyboard. + * + * This defines the interface for an event decoder that supports events out of a software keyboard. + * This differs significantly from hardware keyboard event decoders in several respects. First, + * a software keyboard does not have a scancode/layout system; the keypresses that insert + * characters output unicode characters directly. */ public interface SoftwareEventDecoder extends EventDecoder { public Event decodeSoftwareEvent();