Java tutorial
package com.chaosting.product.hotkey; import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.List; import java.util.Properties; import javax.annotation.PostConstruct; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import com.chaosting.base.util.license.LicenseUtil; import com.chaosting.base.util.os.OSUtil; import com.melloware.jintellitype.HotkeyListener; import com.melloware.jintellitype.JIntellitype; @Component public class Main { private static final Logger logger = Logger.getLogger(Main.class); private static final String LIC_FILE_NAME = "product.lic"; private Robot robot = null; @Autowired private Properties properties; @PostConstruct public void run() { logger.info("Checking the license..."); String licFile = ClassLoader.getSystemClassLoader().getResource("").getPath() + LIC_FILE_NAME; if (!LicenseUtil.isLicValid(licFile)) { logger.info("The license is expired. The software will exit."); return; } logger.info("The license is OK. Checking the required dll..."); String fileName = OSUtil.getOSArch().contains("86") ? "JIntellitype.dll" : "JIntellitype64.dll"; String dllFile = ClassLoader.getSystemClassLoader().getResource("").getPath() + "lib/" + fileName; JIntellitype.setLibraryLocation(dllFile); if (!JIntellitype.isJIntellitypeSupported()) { logger.info("The OS is not Windows or the dll is not found. The software will exit."); return; } JIntellitype jIntellitype = JIntellitype.getInstance(); logger.info("The dll is ready. Parsing the config now..."); final List<Hotkey> hotkeyList = getHotKeyList(); logger.info("Finish parasing config. Registing the hotkey now..."); for (Hotkey hotkey : hotkeyList) { jIntellitype.registerHotKey(hotkey.getIdentifier(), (hotkey.isModShift() == true ? JIntellitype.MOD_SHIFT : 0) + (hotkey.isModControl() == true ? JIntellitype.MOD_CONTROL : 0) + (hotkey.isModWin() == true ? JIntellitype.MOD_WIN : 0) + (hotkey.isModAlt() == true ? JIntellitype.MOD_ALT : 0), (int) hotkey.getKey()); } try { robot = new Robot(); } catch (AWTException e) { e.printStackTrace(); } jIntellitype.addHotKeyListener(new HotkeyListener() { public void onHotKey(int identifier) { for (Hotkey hotkey : hotkeyList) { if (hotkey.getIdentifier() == identifier) { if (hotkey.getGoal().equalsIgnoreCase("exe")) { if (OSUtil.execute(hotkey.getParam())) { logger.info("Executed the " + hotkey.getParam() + " success."); } else { logger.info("Executed the " + hotkey.getParam() + " faild."); } } else if (hotkey.getGoal().equalsIgnoreCase("out")) { int delay = 100; try { delay = Integer.parseInt(properties.getProperty("delay")); } catch (Exception e) { e.printStackTrace(); } robot.delay(delay); for (char c : hotkey.getParam().toCharArray()) { int keycode = convertAsciiInt2KeycodeInt(c); if (keycode == -1) { logger.info("Unsupport character " + c); } else { if ((int) c >= 65 && (int) c <= 90) { robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(keycode); robot.keyRelease(KeyEvent.VK_SHIFT); robot.keyRelease(keycode); } else { robot.keyPress(keycode); robot.keyRelease(keycode); } } } } break; } } } }); logger.info("All the things get ready."); } /** * case ascii in (33,34,35,36,37,38,40...)shiftkeycode,keyperss(shift)keyrelease(shift) * * String temp = "!@#$%^&()_+{}|:\"<>?"; * String temp = "*-=[]\\;',./"; * * @param ascii * @return */ private int convertAsciiInt2KeycodeInt(int ascii) { if (ascii >= 65 && ascii <= 90) { return ascii; } if (ascii >= 48 && ascii <= 57) { return ascii + 48; } if (ascii >= 97 && ascii <= 122) { return ascii - 32; } switch (ascii) { case 13: return KeyEvent.VK_ENTER; case 32: return KeyEvent.VK_SPACE; //case 33: // return KeyEvent.VK_EXCLAMATION_MARK; //case 34: // return KeyEvent.VK_QUOTEDBL; //case 35: // return KeyEvent.VK_NUMBER_SIGN; //case 36: // return KeyEvent.VK_DOLLAR; // 37 % // 38 & case 39: return KeyEvent.VK_QUOTE; //case 40: // return KeyEvent.VK_LEFT_PARENTHESIS; //case 41: // return KeyEvent.VK_RIGHT_PARENTHESIS; case 42: return KeyEvent.VK_MULTIPLY; //case 43: // return KeyEvent.VK_PLUS; case 44: return KeyEvent.VK_COMMA; case 45: return KeyEvent.VK_MINUS; case 46: return KeyEvent.VK_PERIOD; case 47: return KeyEvent.VK_SLASH; //case 58: // return KeyEvent.VK_COLON; case 59: return KeyEvent.VK_SEMICOLON; //case 60: // return KeyEvent.VK_LESS; case 61: return KeyEvent.VK_EQUALS; //case 62: // return KeyEvent.VK_GREATER; // 63 ? //case 64: // return KeyEvent.VK_AT; case 91: return KeyEvent.VK_OPEN_BRACKET; case 92: return KeyEvent.VK_BACK_SLASH; case 93: return KeyEvent.VK_CLOSE_BRACKET; //case 94: // return KeyEvent.VK_CIRCUMFLEX; //case 95: // return KeyEvent.VK_UNDERSCORE; // 96 ` //case 123: // return KeyEvent.VK_BRACELEFT; // 124 | //case 125: // return KeyEvent.VK_BRACERIGHT; // 126 ~ default: return -1; } } /** * ?? * * #identifier=modShiftmodControlmodWinmodAlt|key|exe|path * #identifier=modShiftmodControlmodWinmodAlt|key|out|string * 1=FFFT|a|exe|D\:\\FlashFXP\\flashfxp.exe 2=TFFT|1|out|f**||k u, idiot\! * __\u64CD|\uFF01 * * @return */ private List<Hotkey> getHotKeyList() { List<Hotkey> hotkeyList = new ArrayList<Hotkey>(); for (Object o : properties.keySet()) { try { String key = (String) o; if ("delay".equals(key)) { continue; } String[] values = properties.getProperty(key).split("\\|", 4); Hotkey hotkey = new Hotkey(); hotkey.setIdentifier(Integer.parseInt(key)); hotkey.setModShift("T".equalsIgnoreCase(String.valueOf(values[0].charAt(0)))); hotkey.setModControl("T".equalsIgnoreCase(String.valueOf(values[0].charAt(1)))); hotkey.setModWin("T".equalsIgnoreCase(String.valueOf(values[0].charAt(2)))); hotkey.setModAlt("T".equalsIgnoreCase(String.valueOf(values[0].charAt(3)))); hotkey.setKey(values[1].toUpperCase().charAt(0)); hotkey.setGoal(values[2]); hotkey.setParam(values[3]); hotkeyList.add(hotkey); } catch (Exception e) { logger.error("?" + o + "..."); } } return hotkeyList; } public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); context.start(); context.close(); } }