📄 defaultinputmethodhandler.java
字号:
/* * @(#)DefaultInputMethodHandler.java 1.46 01/08/12 * * Copyright (c) 2000-2001 Sun Microsystems, Inc., 901 San Antonio Road, * Palo Alto, CA 94303, U.S.A. All Rights Reserved. * * Sun Microsystems, Inc. has intellectual property rights relating * to the technology embodied in this software. In particular, and * without limitation, these intellectual property rights may include * one or more U.S. patents, foreign patents, or pending * applications. Sun, Sun Microsystems, the Sun logo, Java, KJava, * and all Sun-based and Java-based marks are trademarks or * registered trademarks of Sun Microsystems, Inc. in the United * States and other countries. * * This software is distributed under licenses restricting its use, * copying, distribution, and decompilation. No part of this * software may be reproduced in any form by any means without prior * written authorization of Sun and its licensors, if any. * * FEDERAL ACQUISITIONS: Commercial Software -- Government Users * Subject to Standard License Terms and Conditions */package com.sun.midp.lcdui;import java.util.TimerTask;import java.util.Timer;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.TextBox;import javax.microedition.lcdui.TextField;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Font;/** * A Default implementation of the InputMethodHandler interface. */public class DefaultInputMethodHandler extends InputMethodHandler { /** * InputMethodClient client that is implemented by TextBox. */ protected InputMethodClient imc; /** * Text buffer. */ protected char buf[] = new char[2]; /** * Number of characters in the buffer. */ protected int bufLen; /** * Committed length of editted buffer. */ protected int committedLen; /** * Max length to fit the client's max length. */ protected int maxLen; /** * Depository for an event. */ protected int savedEvent; /* * IDs for supported input modes */ /** The 'none' input mode. */ protected static final int IM_NONE = 0; /** The 'roman caps' input mode. */ protected static final int IM_ROMAN_CAPS = 1; /** The 'lowercase roman' input mode. */ protected static final int IM_ROMAN_SMALL = 2; /** The 'numeric' input mode. */ protected static final int IM_NUMERIC = 3; /** The 'symbol' input mode. */ protected static final int IM_SYMBOL = 4; /** * Default input mode. */ protected int defaultMode; /** * The number of allowed input modes. */ protected int allowedModesNum; /** * The set of allowed input modes. */ protected int allowedModes[] = new int[5]; /** * Currrent input mode. */ protected int inputMode; /** * Next input mode. */ protected int nextInputMode; /** * InputMode values for supported input modes. */ protected String supportedInputModes[] = { "LATIN", "LATIN_DIGITS", }; /** * Conversion table between this input modes and InputMode's input modes. */ protected Object inputModeConvTable[][] = { {"LATIN", new Integer(IM_ROMAN_CAPS)}, {"LATIN", new Integer(IM_ROMAN_SMALL)}, {"LATIN_DIGITS", new Integer(IM_NUMERIC)}, {"LATIN", new Integer(IM_SYMBOL)}, }; /** * Current constraint. */ protected int constraint; // Key definition for key maps /** A special no-op key. */ protected static final int KEY_NONE = -1; /** The '0' key. */ protected static final int KEY_NUM0 = 0; /** The '1' key. */ protected static final int KEY_NUM1 = 1; /** The '2' key. */ protected static final int KEY_NUM2 = 2; /** The '3' key. */ protected static final int KEY_NUM3 = 3; /** The '4' key. */ protected static final int KEY_NUM4 = 4; /** The '5' key. */ protected static final int KEY_NUM5 = 5; /** The '6' key. */ protected static final int KEY_NUM6 = 6; /** The '7' key. */ protected static final int KEY_NUM7 = 7; /** The '8' key. */ protected static final int KEY_NUM8 = 8; /** The '9' key. */ protected static final int KEY_NUM9 = 9; /** The '*' key. */ protected static final int KEY_STAR = 10; /** The '#' key. */ protected static final int KEY_POUND = 11; /** The 'CLR' key. */ protected static final int KEY_CLEAR = 100; /** * Current input table that can be one of the following. */ protected char keyMap[][]; /** * The uppercase roman key map. */ protected char upperRomanKeyMap[][] = { {'0'}, {'1'}, {'A', 'B', 'C', '2'}, {'D', 'E', 'F', '3'}, {'G', 'H', 'I', '4'}, {'J', 'K', 'L', '5'}, {'M', 'N', 'O', '6'}, {'P', 'Q', 'R', 'S', '7'}, {'T', 'U', 'V', '8'}, {'W', 'X', 'Y', 'Z', '9'}, {'\0'}, {' '} }; /** * The lowercase roman key map. */ protected char lowerRomanKeyMap[][] = { {'0'}, {'1'}, {'a', 'b', 'c', '2'}, {'d', 'e', 'f', '3'}, {'g', 'h', 'i', '4'}, {'j', 'k', 'l', '5'}, {'m', 'n', 'o', '6'}, {'p', 'q', 'r', 's', '7'}, {'t', 'u', 'v', '8'}, {'w', 'x', 'y', 'z', '9'}, {'\0'}, {' '} }; /** * The numeric key map. * Includes 0-9,*, and space. */ protected char numericKeyMap[][] = { {'0'}, {'1'}, {'2'}, {'3'}, {'4'}, {'5'}, {'6'}, {'7'}, {'8'}, {'9'}, {'*'}, {' '} }; /** * The phone numeric key map. * Includes 0-9,*,#,+. */ protected char phoneNumericKeyMap[][] = { {'0'}, {'1'}, {'2'}, {'3'}, {'4'}, {'5'}, {'6'}, {'7'}, {'8'}, {'9'}, {'*'}, {'#', '+'} }; /** * Symbol table. */ protected SymbolTable st = new SymbolTable(); /** * The number of symbol_table is designed to be 25 for 5x5 matrix, * starting the selection at 12. But if you have more, the total * must be under 36 for 6x6 matrix. */ protected char symbolTableChars[] = { '_', '$', '(', ')', '\\', '~', '"', '\'', '/', '&', '*', '@', '.', '?', '!', '#', '-', ',', ':', ';', '%', '=', '+', '<', '>'}; /** * Default cursor position for the Symbol table. */ protected int defaultSymbolCursorPos = 12; /** * Boolean indicating whether the current client is returned from * the Symbol table or not. */ protected boolean hasSymbolTable = false; /** * Set the focus of this handler. * Overrides InputMethodHandler.setFocus * * @param focus If True, this handler should take focus */ public void setFocus(boolean focus) { /* * Note that setFocus() is always called when Display is * changed. */ if (imc == null) { return; } if (focus) { if (hasSymbolTable) { int mode = nextInputMode(); if (bufLen > 0) { handleCharInput(); } setInputMode(mode); hasSymbolTable = false; } else { setInputMode(defaultMode); } } else { if (!hasSymbolTable) { setInputMode(IM_NONE); } } } /** * Establish the input method client for this handler. * Overrides InputMethodHandler.setInputMethodClient * * @param imc The input method client */ public void setInputMethodClient(InputMethodClient imc) { /* * return if the same imc. this is called when the Symbol * table returns. */ if (this.imc == imc) { return; } // initialize variables lastKey = -1; lastPos = 0; incomposing = false; // set the imc specific stuff this.imc = imc; constraint = imc.getConstraints() & TextField.CONSTRAINT_MASK; setConstraint(); // initialize inputMode and keyMap in case TextBox is // invoked from TextField. inputMode = defaultMode; switch (inputMode) { case IM_ROMAN_CAPS: keyMap = upperRomanKeyMap; break; case IM_ROMAN_SMALL: keyMap = lowerRomanKeyMap; break; case IM_NUMERIC: keyMap = (constraint == TextField.PHONENUMBER) ? phoneNumericKeyMap : numericKeyMap; break; } } /** * Clear the input method client. * Overrides InputMethodHandler.clearInputMethodClient. * * @param imcToBeCleared The input method client to be cleared */ public void clearInputMethodClient(InputMethodClient imcToBeCleared) { if (!hasSymbolTable && imc == imcToBeCleared) { imc = null; } } /** * Translate the given key code to its index in the key map. * * @param keyCode The key code of the key pressed * @return int The index of the given key code in the key map */ protected int toKeyMapIndex(int keyCode) { switch (keyCode) { case Canvas.KEY_NUM0: return KEY_NUM0; case Canvas.KEY_NUM1: return KEY_NUM1; case Canvas.KEY_NUM2: return KEY_NUM2; case Canvas.KEY_NUM3: return KEY_NUM3; case Canvas.KEY_NUM4: return KEY_NUM4; case Canvas.KEY_NUM5: return KEY_NUM5; case Canvas.KEY_NUM6: return KEY_NUM6; case Canvas.KEY_NUM7: return KEY_NUM7; case Canvas.KEY_NUM8: return KEY_NUM8; case Canvas.KEY_NUM9: return KEY_NUM9; case Canvas.KEY_STAR: return KEY_STAR; case Canvas.KEY_POUND: return KEY_POUND; case -8: return KEY_CLEAR; default: return KEY_NONE; } } /** * Handle a key pressed event. * Overrides InputMethodHandler.keyPressed. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -