1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.event; 18 19 import android.view.KeyCharacterMap; 20 import android.view.KeyEvent; 21 22 import com.android.inputmethod.latin.Constants; 23 24 /** 25 * A hardware event decoder for a hardware qwerty-ish keyboard. 26 * 27 * The events are always hardware keypresses, but they can be key down or key up events, they 28 * can be dead keys, they can be meta keys like shift or ctrl... This does not deal with 29 * 10-key like keyboards; a different decoder is used for this. 30 */ 31 public class HardwareKeyboardEventDecoder implements HardwareEventDecoder { 32 final int mDeviceId; 33 34 public HardwareKeyboardEventDecoder(final int deviceId) { 35 mDeviceId = deviceId; 36 // TODO: get the layout for this hardware keyboard 37 } 38 39 @Override 40 public Event decodeHardwareKey(final KeyEvent keyEvent) { 41 // KeyEvent#getUnicodeChar() does not exactly returns a unicode char, but rather a value 42 // that includes both the unicode char in the lower 21 bits and flags in the upper bits, 43 // hence the name "codePointAndFlags". {@see KeyEvent#getUnicodeChar()} for more info. 44 final int codePointAndFlags = keyEvent.getUnicodeChar(); 45 // The keyCode is the abstraction used by the KeyEvent to represent different keys that 46 // do not necessarily map to a unicode character. This represents a physical key, like 47 // the key for 'A' or Space, but also Backspace or Ctrl or Caps Lock. 48 final int keyCode = keyEvent.getKeyCode(); 49 if (KeyEvent.KEYCODE_DEL == keyCode) { 50 return Event.createCommittableEvent(Constants.CODE_DELETE, null /* next */); 51 } 52 if (keyEvent.isPrintingKey() || KeyEvent.KEYCODE_SPACE == keyCode 53 || KeyEvent.KEYCODE_ENTER == keyCode) { 54 if (0 != (codePointAndFlags & KeyCharacterMap.COMBINING_ACCENT)) { 55 // A dead key. 56 return Event.createDeadEvent( 57 codePointAndFlags & KeyCharacterMap.COMBINING_ACCENT_MASK, null /* next */); 58 } 59 if (KeyEvent.KEYCODE_ENTER == keyCode) { 60 // The Enter key. If the Shift key is not being pressed, this should send a 61 // CODE_ENTER to trigger the action if any, or a carriage return otherwise. If the 62 // Shift key is being pressed, this should send a CODE_SHIFT_ENTER and let 63 // Latin IME decide what to do with it. 64 return Event.createCommittableEvent(keyEvent.isShiftPressed() 65 ? Constants.CODE_SHIFT_ENTER : Constants.CODE_ENTER, 66 null /* next */); 67 } 68 // If not Enter, then we have a committable character. This should be committed 69 // right away, taking into account the current state. 70 return Event.createCommittableEvent(codePointAndFlags, null /* next */); 71 } 72 return Event.createNotHandledEvent(); 73 } 74 } 75