Back to index

im-sdk  12.3.91
IIIMKeyEvent.java
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 
00044 package sun.awt.im.iiimp;
00045 
00046 import java.util.Hashtable;
00047 import java.util.StringTokenizer;
00048 import java.awt.event.InputEvent;
00049 import java.awt.event.KeyEvent;
00050 import com.sun.iiim.*;
00051 
00056 public class IIIMKeyEvent extends IIIMEvent {
00057 
00058     private int keyCode = KeyEvent.VK_UNDEFINED;
00059     private char keyChar = KeyEvent.CHAR_UNDEFINED;
00060     private int modifier = 0;
00061     private long when = 0;
00062     private String keyString = null;
00063 
00064     public IIIMKeyEvent(String keyString) {
00065        super(KEY);
00066        this.keyString = keyString;
00067        // parse key representaion string
00068        // modifir+...+keyrepresentation 
00069        StringTokenizer st = new StringTokenizer(keyString, "+");
00070        String lastToken = null;
00071        while(st.hasMoreTokens()) {
00072            String token = st.nextToken();
00073            modifier |= getModifier(token);
00074            lastToken = token;
00075        }
00076        setCharCode(lastToken);
00077     }
00078 
00079     private boolean triggerPress = false;
00080 
00081     public boolean match(KeyEvent ke) {
00082        int id = ke.getID();
00083        switch(id) {
00084          case KeyEvent.KEY_PRESSED:
00085            if (keyCode == ke.getKeyCode() && ke.getModifiers() == modifier) {
00086               triggerPress = true;
00087               return true;
00088            }
00089            break;
00090          case KeyEvent.KEY_TYPED:
00091            if (triggerPress) {
00092               return true;
00093            }
00094            break;
00095          case KeyEvent.KEY_RELEASED:
00096            if (keyCode == ke.getKeyCode() && triggerPress) {
00097               triggerPress = false;
00098               return true;
00099            }
00100        }
00101        return false;
00102     }
00103 
00104     public int getKeyCode() {
00105        return keyCode;
00106     }
00107 
00108     public char getKeyChar() {
00109        return keyChar;
00110     }
00111 
00112     public long getWhen() {
00113        return when;
00114     }
00115 
00116     public String toString() {
00117        return keyString;
00118     }
00119 
00120     private int getModifier(String mod) {
00121        Integer i = (Integer)modifierTable.get(mod.toLowerCase());
00122        if (i == null) {
00123            return 0;
00124        }
00125        return i.intValue();
00126     }
00127 
00128     private void setCharCode(String key) {
00129        if (key == null || key.length() == 0) {
00130            return;
00131        }
00132        if (key.length() > 1) {
00133            // this is KeyCode string representation
00134            Integer I = (Integer)keyTable.get(key.toUpperCase());
00135            if (I != null) {
00136               keyCode = I.intValue();
00137            }
00138            // need to check UTF-16
00139        } else {
00140            // length == 1
00141            keyChar = key.charAt(0);
00142        }
00143     }
00144 
00145     static Hashtable modifierTable = null;
00146     static Hashtable keyTable = null;
00147     static {
00148        modifierTable = new Hashtable();
00149        modifierTable.put("shift", new Integer(InputEvent.SHIFT_MASK));
00150        modifierTable.put("control", new Integer(InputEvent.CTRL_MASK));
00151        modifierTable.put("meta", new Integer(InputEvent.META_MASK));
00152        modifierTable.put("alt", new Integer(InputEvent.ALT_MASK));
00153        modifierTable.put("alt_graph", new Integer(InputEvent.ALT_GRAPH_MASK));
00154        modifierTable.put("button1", new Integer(InputEvent.BUTTON1_MASK));
00155        modifierTable.put("button2", new Integer(InputEvent.BUTTON2_MASK));
00156        modifierTable.put("button3", new Integer(InputEvent.BUTTON3_MASK));
00157 
00158        keyTable = new Hashtable();
00159        keyTable.put("ENTER", new Integer(KeyEvent.VK_ENTER));
00160        keyTable.put("BACK_SPACE", new Integer(KeyEvent.VK_BACK_SPACE));
00161        keyTable.put("TAB", new Integer(KeyEvent.VK_TAB));
00162        keyTable.put("CANCEL", new Integer(KeyEvent.VK_CANCEL));
00163        keyTable.put("CLEAR", new Integer(KeyEvent.VK_CLEAR));
00164        keyTable.put("SHIFT", new Integer(KeyEvent.VK_SHIFT));
00165        keyTable.put("CONTROL", new Integer(KeyEvent.VK_CONTROL));
00166        keyTable.put("ALT", new Integer(KeyEvent.VK_ALT));
00167        keyTable.put("PAUSE", new Integer(KeyEvent.VK_PAUSE));
00168        keyTable.put("CAPS_LOCK", new Integer(KeyEvent.VK_CAPS_LOCK));
00169        keyTable.put("ESCAPE", new Integer(KeyEvent.VK_ESCAPE));
00170        keyTable.put("SPACE", new Integer(KeyEvent.VK_SPACE));
00171        keyTable.put("PAGE_UP", new Integer(KeyEvent.VK_PAGE_UP));
00172        keyTable.put("PAGE_DOWN", new Integer(KeyEvent.VK_PAGE_DOWN));
00173        keyTable.put("END", new Integer(KeyEvent.VK_END));
00174        keyTable.put("HOME", new Integer(KeyEvent.VK_HOME));
00175        keyTable.put("LEFT", new Integer(KeyEvent.VK_LEFT));
00176        keyTable.put("UP", new Integer(KeyEvent.VK_UP));
00177        keyTable.put("RIGHT", new Integer(KeyEvent.VK_RIGHT));
00178        keyTable.put("DOWN", new Integer(KeyEvent.VK_DOWN));
00179        keyTable.put("COMMA", new Integer(KeyEvent.VK_COMMA));
00180        keyTable.put("MINUS", new Integer(KeyEvent.VK_MINUS));
00181        keyTable.put("PERIOD", new Integer(KeyEvent.VK_PERIOD));
00182        keyTable.put("SLASH", new Integer(KeyEvent.VK_SLASH));
00183 
00184        keyTable.put("SEMICOLON", new Integer(KeyEvent.VK_SEMICOLON));
00185        keyTable.put("EQUALS", new Integer(KeyEvent.VK_EQUALS));
00186 
00187        keyTable.put("OPEN_BRACKET", new Integer(KeyEvent.VK_OPEN_BRACKET));
00188        keyTable.put("BACK_SLASH", new Integer(KeyEvent.VK_BACK_SLASH));
00189        keyTable.put("CLOSE_BRACKET", new Integer(KeyEvent.VK_CLOSE_BRACKET));
00190 
00191        keyTable.put("NUMPAD0", new Integer(KeyEvent.VK_NUMPAD0));
00192        keyTable.put("NUMPAD1", new Integer(KeyEvent.VK_NUMPAD1));
00193        keyTable.put("NUMPAD2", new Integer(KeyEvent.VK_NUMPAD2));
00194        keyTable.put("NUMPAD3", new Integer(KeyEvent.VK_NUMPAD3));
00195        keyTable.put("NUMPAD4", new Integer(KeyEvent.VK_NUMPAD4));
00196        keyTable.put("NUMPAD5", new Integer(KeyEvent.VK_NUMPAD5));
00197        keyTable.put("NUMPAD6", new Integer(KeyEvent.VK_NUMPAD6));
00198        keyTable.put("NUMPAD7", new Integer(KeyEvent.VK_NUMPAD7));
00199        keyTable.put("NUMPAD8", new Integer(KeyEvent.VK_NUMPAD8));
00200        keyTable.put("NUMPAD9", new Integer(KeyEvent.VK_NUMPAD9));
00201        keyTable.put("MULTIPLY", new Integer(KeyEvent.VK_MULTIPLY));
00202        keyTable.put("ADD", new Integer(KeyEvent.VK_ADD));
00203        keyTable.put("SEPARATER", new Integer(KeyEvent.VK_SEPARATER));
00204        keyTable.put("SUBTRACT", new Integer(KeyEvent.VK_SUBTRACT));
00205        keyTable.put("DECIMAL", new Integer(KeyEvent.VK_DECIMAL));
00206        keyTable.put("DIVIDE", new Integer(KeyEvent.VK_DIVIDE));
00207        keyTable.put("DELETE", new Integer(KeyEvent.VK_DELETE));
00208        keyTable.put("NUM_LOCK", new Integer(KeyEvent.VK_NUM_LOCK));
00209        keyTable.put("SCROLL_LOCK", new Integer(KeyEvent.VK_SCROLL_LOCK));
00210 
00211        keyTable.put("F1", new Integer(KeyEvent.VK_F1));
00212        keyTable.put("F2", new Integer(KeyEvent.VK_F2));
00213        keyTable.put("F3", new Integer(KeyEvent.VK_F3));
00214        keyTable.put("F4", new Integer(KeyEvent.VK_F4));
00215        keyTable.put("F5", new Integer(KeyEvent.VK_F5));
00216        keyTable.put("F6", new Integer(KeyEvent.VK_F6));
00217        keyTable.put("F7", new Integer(KeyEvent.VK_F7));
00218        keyTable.put("F8", new Integer(KeyEvent.VK_F8));
00219        keyTable.put("F9", new Integer(KeyEvent.VK_F9));
00220        keyTable.put("F10", new Integer(KeyEvent.VK_F10));
00221        keyTable.put("F11", new Integer(KeyEvent.VK_F11));
00222        keyTable.put("F12", new Integer(KeyEvent.VK_F12));
00223        keyTable.put("F13", new Integer(KeyEvent.VK_F13));
00224        keyTable.put("F14", new Integer(KeyEvent.VK_F14));
00225        keyTable.put("F15", new Integer(KeyEvent.VK_F15));
00226        keyTable.put("F16", new Integer(KeyEvent.VK_F16));
00227        keyTable.put("F17", new Integer(KeyEvent.VK_F17));
00228        keyTable.put("F18", new Integer(KeyEvent.VK_F18));
00229        keyTable.put("F19", new Integer(KeyEvent.VK_F19));
00230        keyTable.put("F20", new Integer(KeyEvent.VK_F20));
00231        keyTable.put("F21", new Integer(KeyEvent.VK_F21));
00232        keyTable.put("F22", new Integer(KeyEvent.VK_F22));
00233        keyTable.put("F23", new Integer(KeyEvent.VK_F23));
00234        keyTable.put("F24", new Integer(KeyEvent.VK_F24));
00235 
00236        keyTable.put("PRINTSCREEN", new Integer(KeyEvent.VK_PRINTSCREEN));
00237        keyTable.put("INSERT", new Integer(KeyEvent.VK_INSERT));
00238        keyTable.put("HELP", new Integer(KeyEvent.VK_HELP));
00239        keyTable.put("META", new Integer(KeyEvent.VK_META));
00240 
00241        keyTable.put("BACK_QUOTE", new Integer(KeyEvent.VK_BACK_QUOTE));
00242        keyTable.put("QUOTE", new Integer(KeyEvent.VK_QUOTE));
00243 
00244        keyTable.put("KP_UP", new Integer(KeyEvent.VK_KP_UP));
00245        keyTable.put("KP_DOWN", new Integer(KeyEvent.VK_KP_DOWN));
00246        keyTable.put("KP_LEFT", new Integer(KeyEvent.VK_KP_LEFT));
00247        keyTable.put("KP_RIGHT", new Integer(KeyEvent.VK_KP_RIGHT));
00248 
00249        keyTable.put("DEAD_GRAVE", new Integer(KeyEvent.VK_DEAD_GRAVE));
00250        keyTable.put("DEAD_ACUTE", new Integer(KeyEvent.VK_DEAD_ACUTE));
00251        keyTable.put("DEAD_CIRCUMFLEX", new Integer(KeyEvent.VK_DEAD_CIRCUMFLEX));
00252        keyTable.put("DEAD_TILDE", new Integer(KeyEvent.VK_DEAD_TILDE));
00253        keyTable.put("DEAD_MACRON", new Integer(KeyEvent.VK_DEAD_MACRON));
00254        keyTable.put("DEAD_BREVE", new Integer(KeyEvent.VK_DEAD_BREVE));
00255        keyTable.put("DEAD_ABOVEDOT", new Integer(KeyEvent.VK_DEAD_ABOVEDOT));
00256        keyTable.put("DEAD_DIAERESIS", new Integer(KeyEvent.VK_DEAD_DIAERESIS));
00257        keyTable.put("DEAD_ABOVERING", new Integer(KeyEvent.VK_DEAD_ABOVERING));
00258        keyTable.put("DEAD_DOUBLEACUTE", new Integer(KeyEvent.VK_DEAD_DOUBLEACUTE));
00259        keyTable.put("DEAD_CARON", new Integer(KeyEvent.VK_DEAD_CARON));
00260        keyTable.put("DEAD_CEDILLA", new Integer(KeyEvent.VK_DEAD_CEDILLA));
00261        keyTable.put("DEAD_OGONEK", new Integer(KeyEvent.VK_DEAD_OGONEK));
00262        keyTable.put("DEAD_IOTA", new Integer(KeyEvent.VK_DEAD_IOTA));
00263        keyTable.put("DEAD_VOICED_SOUND", new Integer(KeyEvent.VK_DEAD_VOICED_SOUND));
00264        keyTable.put("DEAD_SEMIVOICED_SOUND", new Integer(KeyEvent.VK_DEAD_SEMIVOICED_SOUND));
00265 
00266        keyTable.put("AMPERSAND", new Integer(KeyEvent.VK_AMPERSAND));
00267        keyTable.put("ASTERISK", new Integer(KeyEvent.VK_ASTERISK));
00268        keyTable.put("QUOTEDBL", new Integer(KeyEvent.VK_QUOTEDBL));
00269        keyTable.put("LESS", new Integer(KeyEvent.VK_LESS));
00270        
00271        keyTable.put("GREATER", new Integer(KeyEvent.VK_GREATER));
00272        keyTable.put("BRACELEFT", new Integer(KeyEvent.VK_BRACELEFT));
00273        keyTable.put("BRACERIGHT", new Integer(KeyEvent.VK_BRACERIGHT));
00274 
00275        keyTable.put("AT", new Integer(KeyEvent.VK_AT));
00276        keyTable.put("COLON", new Integer(KeyEvent.VK_COLON));
00277        keyTable.put("CIRCUMFLEX", new Integer(KeyEvent.VK_CIRCUMFLEX));
00278        keyTable.put("DOLLAR", new Integer(KeyEvent.VK_DOLLAR));
00279        keyTable.put("EURO_SIGN", new Integer(KeyEvent.VK_EURO_SIGN));
00280        keyTable.put("EXCLAMATION_MARK", new Integer(KeyEvent.VK_EXCLAMATION_MARK));
00281        keyTable.put("INVERTED_EXCLAMATION_MARK", new Integer(KeyEvent.VK_INVERTED_EXCLAMATION_MARK));
00282        keyTable.put("LEFT_PARENTHESIS", new Integer(KeyEvent.VK_LEFT_PARENTHESIS));
00283        keyTable.put("NUMBER_SIGN", new Integer(KeyEvent.VK_NUMBER_SIGN));
00284        keyTable.put("PLUS", new Integer(KeyEvent.VK_PLUS));
00285        keyTable.put("RIGHT_PARENTHESIS", new Integer(KeyEvent.VK_RIGHT_PARENTHESIS));
00286        keyTable.put("UNDERSCORE", new Integer(KeyEvent.VK_UNDERSCORE));
00287 
00288        keyTable.put("FINAL", new Integer(KeyEvent.VK_FINAL));
00289        keyTable.put("CONVERT", new Integer(KeyEvent.VK_CONVERT));
00290        keyTable.put("NONCONVERT", new Integer(KeyEvent.VK_NONCONVERT));
00291        keyTable.put("ACCEPT", new Integer(KeyEvent.VK_ACCEPT));
00292        keyTable.put("MODECHANGE", new Integer(KeyEvent.VK_MODECHANGE));
00293        keyTable.put("KANA", new Integer(KeyEvent.VK_KANA));
00294        keyTable.put("KANJI", new Integer(KeyEvent.VK_KANJI));
00295 
00296        keyTable.put("ALPHANUMERIC", new Integer(KeyEvent.VK_ALPHANUMERIC));
00297        keyTable.put("KATAKANA", new Integer(KeyEvent.VK_KATAKANA));
00298        keyTable.put("HIRAGANA", new Integer(KeyEvent.VK_HIRAGANA));
00299        keyTable.put("FULL_WIDTH", new Integer(KeyEvent.VK_FULL_WIDTH));
00300        keyTable.put("HALF_WIDTH", new Integer(KeyEvent.VK_HALF_WIDTH));
00301        keyTable.put("ROMAN_CHARACTERS", new Integer(KeyEvent.VK_ROMAN_CHARACTERS));
00302        keyTable.put("ALL_CANDIDATES", new Integer(KeyEvent.VK_ALL_CANDIDATES));
00303        keyTable.put("PREVIOUS_CONDIDATE", new Integer(KeyEvent.VK_PREVIOUS_CANDIDATE));
00304        keyTable.put("CODE_INPUT", new Integer(KeyEvent.VK_CODE_INPUT));
00305        keyTable.put("JAPANESE_KATAKANA", new Integer(KeyEvent.VK_JAPANESE_KATAKANA));
00306        keyTable.put("JAPANESE_HIRAGANA", new Integer(KeyEvent.VK_JAPANESE_HIRAGANA));
00307        keyTable.put("JAPANESE_ROMAN", new Integer(KeyEvent.VK_JAPANESE_ROMAN));
00308 
00309        keyTable.put("CUT", new Integer(KeyEvent.VK_CUT));
00310        keyTable.put("COPY", new Integer(KeyEvent.VK_COPY));
00311        keyTable.put("PASTE", new Integer(KeyEvent.VK_PASTE));
00312        keyTable.put("UNDO", new Integer(KeyEvent.VK_UNDO));
00313        keyTable.put("AGAIN", new Integer(KeyEvent.VK_AGAIN));
00314        keyTable.put("FIND", new Integer(KeyEvent.VK_FIND));
00315        keyTable.put("PROPS", new Integer(KeyEvent.VK_PROPS));
00316        keyTable.put("STOP", new Integer(KeyEvent.VK_STOP));
00317        
00318        keyTable.put("COMPOSE", new Integer(KeyEvent.VK_COMPOSE));
00319        keyTable.put("ALT_GRAPH", new Integer(KeyEvent.VK_ALT_GRAPH));
00320     }
00321 }