KeyEvent.java
资源名称:src.zip [点击查看]
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:19k
源码类别:
J2ME
开发平台:
Java
- package com.framework;
- import javax.microedition.lcdui.Canvas;
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author swaroop_kumar
- */
- public class KeyEvent {
- public static int KEY_SOFT_LEFT;
- public static int KEY_SOFT_RIGHT;
- public static int KEY_SOFT_CENTER;
- public static int BACK_KEY;
- /**
- * constants for platforms names
- */
- public static final String PLATFORM_MOTOROLA = "motorola";
- public static final String PLATFORM_NOKIA = "nokia";
- public static final String PLATFORM_SONY_ERICSSON = "SE";
- public static final String PLATFORM_SIEMENS = "siemens";
- public static final String PLATFORM_SAMSUNG = "samsung";
- public static final String PLATFORM_LG = "LG";
- public static final String PLATFORM_NOT_DEFINED = "NA";
- /** Devices having Qwerty keyboard with different gamekey behaviour*/
- private static final String[] _SQwertyDevices = {"SonyEricssonP1i"};
- public static final String PLATFORM_BLACKBERRY = "blackberry";
- /**
- * current platform name
- */
- private static String PLATFORM_NAME;
- private static final int SOFT_KEY_LEFT_SE = -6;
- private static final int SOFT_KEY_RIGHT_SE = -7;
- private static final int DELETE_KEY_SE = -8;
- private static final int INTERNET_KEY_SE = -10;
- private static final int SOFT_KEY_LEFT_SAMSUNG = -6;
- private static final int SOFT_KEY_RIGHT_SAMSUNG = -7;
- private static final int DELETE_KEY_SAMSUNG = -8;
- private static final int SOFT_KEY_LEFT_SIEMENS = -1;
- private static final int SOFT_KEY_RIGHT_SIEMENS = -4;
- private static final int SOFT_KEY_LEFT_NOKIA = -6;
- private static final int SOFT_KEY_RIGHT_NOKIA = -7;
- private static final int DELETE_KEY_NOKIA = -8;
- private static final int SOFT_KEY_MIDLE_NOKIA = -5;
- private static final int SOFT_KEY_LEFT_MOTOROLA = -21;
- private static final int SOFT_KEY_RIGHT_MOTOROLA = -22;
- private static final int SOFT_KEY_LEFT_MOTOROLA2 = -20;
- private static final int SOFT_KEY_LEFT_MOTOROLA1 = 21;
- private static final int SOFT_KEY_RIGHT_MOTOROLA1 = 22;
- private static final int SOFT_KEY_MIDLE_MOTOROLA = -23;
- private static final String SOFT_WORD = "SOFT";
- // Key management...
- protected static final int DELETE_KEY_BLACKBERRY = 8;
- static int _keysPressed = 0;
- static int _keysCurrent = 0;
- static int _keyCode = 0;
- /**
- * Handles the key pressed event of the mobile.
- * @param pKeyCode Value of the key being pressed down.
- */
- public static void init(Canvas canvas)
- {
- PLATFORM_NAME = getPlatform(canvas);
- KEY_SOFT_LEFT = getLeftSoftkeyCode(canvas);
- KEY_SOFT_RIGHT= getRightSoftkeyCode(canvas);
- KEY_SOFT_CENTER = getMiddleSoftkeyCode(canvas);
- BACK_KEY = getDeleteKeyCode(canvas);
- try {
- String prop = FrameWorkMidlet.getInstance().getAppProperty("KeyPad-Type");
- if(prop != null && prop.length() > 0)
- {
- if(Util.equalsIgnoreCase(prop, "QWERTY"))
- {
- EditControl.setKeyPadType(EditControl.QWERTY_KEY_PAD);
- }
- }
- } catch (Exception e) {
- }
- }
- /**
- * define real key's C or DELETE code for current platform
- *
- * @return code
- */
- protected static int getDeleteKeyCode(Canvas canvas) {
- try {
- if (PLATFORM_NAME.equals(PLATFORM_MOTOROLA)) {
- } else if (PLATFORM_NAME.equals(PLATFORM_NOKIA)) {
- if (canvas.getKeyName(DELETE_KEY_SE).toUpperCase().indexOf("CLEAR") >= 0) {
- return DELETE_KEY_NOKIA;
- } else {
- return DELETE_KEY_NOKIA;
- }
- } else if (PLATFORM_NAME.equals(PLATFORM_SAMSUNG)) {
- if (canvas.getKeyName(DELETE_KEY_SAMSUNG).toUpperCase().indexOf("CLEAR") >= 0) {
- return DELETE_KEY_SAMSUNG;
- }
- } else if (PLATFORM_NAME.equals(PLATFORM_SIEMENS)) {
- } else if (PLATFORM_NAME.equals(PLATFORM_SONY_ERICSSON)) {
- if (canvas.getKeyName(DELETE_KEY_SE).toUpperCase().indexOf("CLEAR") >= 0) {
- return DELETE_KEY_SE;
- } else if (canvas.getKeyName(DELETE_KEY_SE).toUpperCase().indexOf("C") >= 0) {
- return DELETE_KEY_SE;
- } else {
- return DELETE_KEY_SE;
- }
- }
- } catch (Throwable e) {
- return DELETE_KEY_SE;
- }
- return -8;
- }
- /**
- * define real middle soft key code for current platform
- *
- * @return code
- */
- protected static int getMiddleSoftkeyCode(Canvas canvas) {
- try {
- if (PLATFORM_NAME.equals(PLATFORM_MOTOROLA)) {
- if (canvas.getKeyName(SOFT_KEY_MIDLE_MOTOROLA).toUpperCase().indexOf("SOFT") >= 0) {
- return SOFT_KEY_MIDLE_MOTOROLA;
- }
- } else if (PLATFORM_NAME.equals(PLATFORM_NOKIA)) {
- if (canvas.getKeyName(SOFT_KEY_MIDLE_NOKIA).toUpperCase().indexOf("SOFT") >= 0) {
- return SOFT_KEY_MIDLE_NOKIA;
- }
- } else if (PLATFORM_NAME.equals(PLATFORM_SAMSUNG)) {
- } else if (PLATFORM_NAME.equals(PLATFORM_SIEMENS)) {
- } else if (PLATFORM_NAME.equals(PLATFORM_SONY_ERICSSON)) {
- return INTERNET_KEY_SE;
- }
- } catch (Throwable e) {
- }
- return Canvas.FIRE;
- }
- /**
- * define real right soft key code for current platform
- *
- * @return code
- */
- protected static int getRightSoftkeyCode(Canvas canvas) {
- int keyCode = -7;
- try {
- if (PLATFORM_NAME.equals(PLATFORM_MOTOROLA)) {
- String rightSoftMoto1 = "";
- try {
- rightSoftMoto1 = canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA1).toUpperCase();
- } catch (IllegalArgumentException ilae) {
- // ilae.printStackTrace();
- }
- String rightSoftMoto = "";
- try {
- rightSoftMoto = canvas.getKeyName(SOFT_KEY_RIGHT_MOTOROLA).toUpperCase();
- } catch (IllegalArgumentException ilae) {
- // ilae.printStackTrace();
- }
- String rightSoftMoto2 = "";
- try {
- rightSoftMoto2 = canvas.getKeyName(SOFT_KEY_RIGHT_MOTOROLA1).toUpperCase();
- } catch (IllegalArgumentException ilae) {
- // ilae.printStackTrace();
- }
- if (rightSoftMoto.indexOf(SOFT_WORD) >= 0 && rightSoftMoto.indexOf("2") >= 0) {
- return SOFT_KEY_RIGHT_MOTOROLA;
- } else if (rightSoftMoto1.indexOf(SOFT_WORD) >= 0 && rightSoftMoto1.indexOf("2") >= 0) {
- return SOFT_KEY_RIGHT_MOTOROLA;
- } else if (rightSoftMoto2.indexOf(SOFT_WORD) >= 0 && rightSoftMoto2.indexOf("2") >= 0) {
- return SOFT_KEY_RIGHT_MOTOROLA1;
- } else if (rightSoftMoto.indexOf(SOFT_WORD) >= 0 && rightSoftMoto.indexOf("RIGHT") >= 0) {
- return SOFT_KEY_LEFT_MOTOROLA;
- } else if (rightSoftMoto1.indexOf(SOFT_WORD) >= 0 && rightSoftMoto1.indexOf("RIGHT") >= 0) {
- return SOFT_KEY_RIGHT_MOTOROLA1;
- } else if (rightSoftMoto2.indexOf(SOFT_WORD) >= 0 && rightSoftMoto2.indexOf("RIGHT") >= 0) {
- return SOFT_KEY_RIGHT_MOTOROLA;
- }
- } else if (PLATFORM_NAME.equals(PLATFORM_NOKIA)) {
- return SOFT_KEY_RIGHT_NOKIA;
- } else if (PLATFORM_NAME.equals(PLATFORM_SAMSUNG)) {
- // String rightSoftSamsung = adaptorCanvas.getKeyName(SOFT_KEY_RIGHT_SAMSUNG).toUpperCase();
- // if (rightSoftSamsung.indexOf(SOFT_WORD) >= 0) {
- // if (rightSoftSamsung.indexOf("2") >= 0) {
- return SOFT_KEY_RIGHT_SAMSUNG;
- // } else if (rightSoftSamsung.indexOf("RIGHT") >= 0) {
- // return SOFT_KEY_RIGHT_SAMSUNG;
- // }
- // }
- } else if (PLATFORM_NAME.equals(PLATFORM_SIEMENS)) {
- String rightSoftSiemens = canvas.getKeyName(SOFT_KEY_RIGHT_SIEMENS).toUpperCase();
- if (rightSoftSiemens.indexOf(SOFT_WORD) >= 0) {
- if (rightSoftSiemens.indexOf("4") >= 0) {
- return SOFT_KEY_RIGHT_SIEMENS;
- } else if (rightSoftSiemens.indexOf("RIGHT") >= 0) {
- return SOFT_KEY_RIGHT_SIEMENS;
- }
- }
- } else if (PLATFORM_NAME.equals(PLATFORM_SONY_ERICSSON)) {
- return SOFT_KEY_RIGHT_SE;
- } else if (PLATFORM_NAME.equals(PLATFORM_NOT_DEFINED)) {
- for (int i = -125; i <= 125; i++) {
- if (i == 0) {
- i++;
- }
- String keyName = canvas.getKeyName(i).toUpperCase();
- if (keyName.indexOf(SOFT_WORD) >= 0) {
- if (keyName.indexOf("2") >= 0) {
- keyCode = i;
- break;
- } else if (keyName.indexOf("4") >= 0) {
- keyCode = i;
- break;
- } else if (keyName.indexOf("RIGHT") >= 0) {
- keyCode = i;
- break;
- }
- }
- }
- }
- } catch (Throwable iaEx) {
- //if emulator
- return SOFT_KEY_RIGHT_NOKIA;
- //endif
- }
- return keyCode;
- }
- /**
- * define real left soft key code by platform
- *
- * @return code
- */
- protected static int getLeftSoftkeyCode(Canvas canvas) {
- int keyCode = -6;
- try {
- if (PLATFORM_NAME.equals(PLATFORM_MOTOROLA)) {
- String softkeyLeftMoto = "";
- try {
- softkeyLeftMoto = canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA).toUpperCase();
- } catch (IllegalArgumentException ilae) {
- // ilae.printStackTrace();
- }
- String softkeyLeftMoto1 = "";
- try {
- softkeyLeftMoto1 = canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA1).toUpperCase();
- } catch (IllegalArgumentException ilae) {
- // ilae.printStackTrace();
- }
- String softkeyLeftMoto2 = "";
- try {
- softkeyLeftMoto2 = canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA2).toUpperCase();
- } catch (IllegalArgumentException ilae) {
- // ilae.printStackTrace();
- }
- if (softkeyLeftMoto.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto.indexOf("1") >= 0) {
- return SOFT_KEY_LEFT_MOTOROLA;
- } else if (softkeyLeftMoto1.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto1.indexOf("1") >= 0) {
- return SOFT_KEY_LEFT_MOTOROLA1;
- } else if (softkeyLeftMoto2.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto2.indexOf("1") >= 0) {
- return SOFT_KEY_LEFT_MOTOROLA2;
- } else if (softkeyLeftMoto.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto.indexOf("LEFT") >= 0) {
- return SOFT_KEY_LEFT_MOTOROLA;
- } else if (softkeyLeftMoto1.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto1.indexOf("LEFT") >= 0) {
- return SOFT_KEY_LEFT_MOTOROLA1;
- } else if (softkeyLeftMoto2.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto2.indexOf("LEFT") >= 0) {
- return SOFT_KEY_LEFT_MOTOROLA2;
- }
- } else if (PLATFORM_NAME.equals(PLATFORM_NOKIA)) {
- return SOFT_KEY_LEFT_NOKIA;
- } else if (PLATFORM_NAME.equals(PLATFORM_SAMSUNG)) {
- // String leftkeySamsungName = adaptorCanvas.getKeyName(SOFT_KEY_LEFT_SAMSUNG).toUpperCase();
- // if (leftkeySamsungName.indexOf(SOFT_WORD) >= 0) {
- // if (leftkeySamsungName.indexOf("1") >= 0) {
- return SOFT_KEY_LEFT_SAMSUNG;
- // } else if (leftkeySamsungName.indexOf("LEFT") >= 0) {
- // return SOFT_KEY_LEFT_SAMSUNG;
- // }
- // }
- } else if (PLATFORM_NAME.equals(PLATFORM_SIEMENS)) {
- String leftKeySiemensName = canvas.getKeyName(SOFT_KEY_LEFT_SIEMENS).toUpperCase();
- if (leftKeySiemensName.indexOf(SOFT_WORD) >= 0) {
- if (leftKeySiemensName.indexOf("1") >= 0) {
- return SOFT_KEY_LEFT_SIEMENS;
- } else if (leftKeySiemensName.indexOf("LEFT") >= 0) {
- return SOFT_KEY_LEFT_SIEMENS;
- }
- }
- } else if (PLATFORM_NAME.equals(PLATFORM_SONY_ERICSSON)) {
- return SOFT_KEY_LEFT_SE;
- } else if (PLATFORM_NAME.equals(PLATFORM_NOT_DEFINED)) {
- //
- for (int i = -125; i <= 125; i++) {
- if (i == 0) {
- i++;
- }
- // System.out.println(getKeyName(i).toUpperCase());
- final String s = canvas.getKeyName(i).toUpperCase();
- if (s.indexOf(SOFT_WORD) >= 0) {
- if (s.indexOf("1") >= 0) {
- keyCode = i;
- break;
- }
- if (s.indexOf("LEFT") >= 0) {
- keyCode = i;
- break;
- }
- }
- }
- }
- if (keyCode == 0) {
- //if emulator
- return SOFT_KEY_LEFT_NOKIA;
- //endif
- }
- } catch (Throwable iaEx) {
- //if emulator
- return SOFT_KEY_LEFT_NOKIA;
- //endif
- }
- return keyCode;
- }
- /**
- * Returns mobile phone platform
- *
- * @return name mobile phone platform
- */
- private static String getPlatform(Canvas canvas) {
- // detecting NOKIA or SonyEricsson
- try {
- final String currentPlatform = System.getProperty("microedition.platform");
- if (currentPlatform.indexOf("Nokia") != -1) {
- return PLATFORM_NOKIA;
- } else if (currentPlatform.indexOf("SonyEricsson") != -1) {
- return PLATFORM_SONY_ERICSSON;
- }
- } catch (Throwable ex) {
- }
- // detecting SAMSUNG
- try {
- Class.forName("com.samsung.util.Vibration");
- return PLATFORM_SAMSUNG;
- } catch (Throwable ex) {
- }
- // detecting MOTOROLA
- try {
- Class.forName("com.motorola.multimedia.Vibrator");
- return PLATFORM_MOTOROLA;
- } catch (Throwable ex) {
- try {
- Class.forName("com.motorola.graphics.j3d.Effect3D");
- return PLATFORM_MOTOROLA;
- } catch (Throwable ex2) {
- try {
- Class.forName("com.motorola.multimedia.Lighting");
- return PLATFORM_MOTOROLA;
- } catch (Throwable ex3) {
- try {
- Class.forName("com.motorola.multimedia.FunLight");
- return PLATFORM_MOTOROLA;
- } catch (Throwable ex4) {
- }
- }
- }
- }
- try {
- if (canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA).toUpperCase().indexOf(SOFT_WORD) > -1) {
- return PLATFORM_MOTOROLA;
- }
- } catch (Throwable e) {
- try {
- if (canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA1).toUpperCase().indexOf(SOFT_WORD) > -1) {
- return PLATFORM_MOTOROLA;
- }
- } catch (Throwable e1) {
- try {
- if (canvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA2).toUpperCase().indexOf(SOFT_WORD) > -1) {
- return PLATFORM_MOTOROLA;
- }
- } catch (Throwable e2) {
- }
- }
- }
- // detecting SIEMENS
- try {
- Class.forName("com.siemens.mp.io.File");
- return PLATFORM_SIEMENS;
- } catch (Throwable ex) {
- }
- // detecting LG
- try {
- Class.forName("mmpp.media.MediaPlayer");
- return PLATFORM_LG;
- } catch (Throwable ex) {
- try {
- Class.forName("mmpp.phone.Phone");
- return PLATFORM_LG;
- } catch (Throwable ex1) {
- try {
- Class.forName("mmpp.lang.MathFP");
- return PLATFORM_LG;
- } catch (Throwable ex2) {
- try {
- Class.forName("mmpp.media.BackLight");
- return PLATFORM_LG;
- } catch (Throwable ex3) {
- }
- }
- }
- }
- return PLATFORM_NOT_DEFINED;
- }
- protected static boolean isNumericKeyPad(Canvas canvas)
- {
- try {
- for (int i = 65; i < (65 + 26); i++)
- {
- canvas.getKeyName(i);
- }
- if(canvas.getGameAction((int)'A') == Canvas.LEFT && canvas.getGameAction((int)'D') == Canvas.RIGHT && canvas.getGameAction((int)'W') == Canvas.UP && canvas.getGameAction((int)'Z') == Canvas.DOWN)
- {
- return false;
- }
- String platform = System.getProperty("microedition.platform");
- for(int devCnt=0; devCnt<_SQwertyDevices.length; devCnt++){
- if(platform.indexOf(_SQwertyDevices[devCnt]) != -1){
- return false;
- }
- }
- return true;
- } catch (Exception e) {
- return true;
- }
- }
- }