PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/melloware/jintellitype/JIntellitype.java

https://github.com/yoihito/hot-translate
Java | 675 lines | 435 code | 44 blank | 196 comment | 48 complexity | 19de19790a04399e2ae98e01f257b109 MD5 | raw file
  1. /**
  2. * JIntellitype ----------------- Copyright 2005-2008 Emil A. Lefkof III,
  3. * Melloware Inc. I always give it my best shot to make a program useful and
  4. * solid, but remeber that there is absolutely no warranty for using this
  5. * program as stated in the following terms: Licensed under the Apache License,
  6. * Version 2.0 (the "License"); you may not use this file except in compliance
  7. * with the License. You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
  9. * or agreed to in writing, software distributed under the License is
  10. * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  11. * KIND, either express or implied. See the License for the specific language
  12. * governing permissions and limitations under the License.
  13. */
  14. package com.melloware.jintellitype;
  15. import java.awt.event.InputEvent;
  16. import java.awt.event.KeyEvent;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.util.Collections;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.concurrent.CopyOnWriteArrayList;
  26. import javax.swing.SwingUtilities;
  27. /**
  28. * JIntellitype A Java Implementation for using the Windows API Intellitype
  29. * commands and the RegisterHotKey and UnRegisterHotkey API calls for globally
  30. * responding to key events. Intellitype are commands that are using for Play,
  31. * Stop, Next on Media keyboards or some laptops that have those special keys.
  32. * <p>
  33. * JIntellitype class that is used to call Windows API calls using the
  34. * JIntellitype.dll.
  35. * <p>
  36. * This file comes with native code in JINTELLITYPE.DLL The DLL should go in
  37. * C:/WINDOWS/SYSTEM or in your current directory
  38. * <p>
  39. * <p>
  40. * Copyright (c) 1999-2008 Melloware, Inc. <http://www.melloware.com>
  41. * @author Emil A. Lefkof III <info@melloware.com>
  42. * @version 1.3.1
  43. */
  44. public final class JIntellitype implements JIntellitypeConstants {
  45. /**
  46. * Static variable to hold singleton.
  47. */
  48. private static JIntellitype jintellitype = null;
  49. /**
  50. * Static variable for double checked thread safety.
  51. */
  52. private static boolean isInitialized = false;
  53. /**
  54. * Static variable to hold the libary location if set
  55. */
  56. private static String libraryLocation = null;
  57. /**
  58. * Listeners collection for Hotkey events
  59. */
  60. private final List<HotkeyListener> hotkeyListeners = Collections
  61. .synchronizedList(new CopyOnWriteArrayList<HotkeyListener>());
  62. /**
  63. * Listeners collection for Hotkey events
  64. */
  65. private final List<IntellitypeListener> intellitypeListeners = Collections
  66. .synchronizedList(new CopyOnWriteArrayList<IntellitypeListener>());
  67. /**
  68. * Handler is used by JNI code to keep different JVM instances separate
  69. */
  70. private final int handler = 0;
  71. /**
  72. * Map containing key->keycode mapping
  73. * @see #registerHotKey(int, String)
  74. * @see #getKey2KeycodeMapping()
  75. */
  76. private final HashMap<String, Integer> keycodeMap;
  77. /**
  78. * Private Constructor to prevent instantiation. Initialize the library for
  79. * calling.
  80. */
  81. private JIntellitype() {
  82. try {
  83. // Load JNI library
  84. System.loadLibrary("JIntellitype");
  85. } catch (Throwable ex) {
  86. try {
  87. if (getLibraryLocation() != null) {
  88. System.load(getLibraryLocation());
  89. } else {
  90. String jarPath = "com/melloware/jintellitype/";
  91. String tmpDir = System.getProperty("java.io.tmpdir");
  92. try {
  93. String dll = "JIntellitype.dll";
  94. fromJarToFs(jarPath + dll, tmpDir + dll);
  95. System.load(tmpDir + dll);
  96. } catch (UnsatisfiedLinkError e) {
  97. String dll = "JIntellitype64.dll";
  98. fromJarToFs(jarPath + dll, tmpDir + dll);
  99. System.load(tmpDir + dll);
  100. }
  101. }
  102. } catch (Throwable ex2) {
  103. throw new JIntellitypeException(
  104. "Could not load JIntellitype.dll from local file system or from inside JAR", ex2);
  105. }
  106. }
  107. initializeLibrary();
  108. this.keycodeMap = getKey2KeycodeMapping();
  109. }
  110. /**
  111. * Pulls a file out of the JAR and puts it on the File Path.
  112. * <p>
  113. * @param jarPath the path to the JAR
  114. * @param filePath the file path to extract to
  115. * @throws IOException if any IO error occurs
  116. */
  117. private void fromJarToFs(String jarPath, String filePath) throws IOException {
  118. File file = new File(filePath);
  119. if (file.exists()) {
  120. boolean success = file.delete();
  121. if (!success) {
  122. throw new IOException("couldn't delete " + filePath);
  123. }
  124. }
  125. InputStream is = null;
  126. OutputStream os = null;
  127. try {
  128. is = ClassLoader.getSystemClassLoader().getResourceAsStream(jarPath);
  129. os = new FileOutputStream(filePath);
  130. byte[] buffer = new byte[8192];
  131. int bytesRead;
  132. while ((bytesRead = is.read(buffer)) != -1) {
  133. os.write(buffer, 0, bytesRead);
  134. }
  135. } finally {
  136. if (is != null) {
  137. is.close();
  138. }
  139. if (os != null) {
  140. os.close();
  141. }
  142. }
  143. }
  144. /**
  145. * Gets the singleton instance of the JIntellitype object.
  146. * <p>
  147. * But the possibility of creation of more instance is only before the
  148. * instance is created. Since all code defined inside getInstance method is
  149. * in the synchronized block, even the subsequent requests will also come and
  150. * wait in the synchronized block. This is a performance issue. The same can
  151. * be solved using double-checked lock. Following is the implementation of
  152. * Singleton with lazy initialization and double-checked lock.
  153. * <p>
  154. * @return an instance of JIntellitype class
  155. */
  156. public static JIntellitype getInstance() {
  157. if (!isInitialized) {
  158. synchronized (JIntellitype.class) {
  159. if (!isInitialized) {
  160. jintellitype = new JIntellitype();
  161. isInitialized = true;
  162. }
  163. }
  164. }
  165. return jintellitype;
  166. }
  167. /**
  168. * Adds a listener for hotkeys.
  169. * <p>
  170. * @param listener the HotKeyListener to be added
  171. */
  172. public void addHotKeyListener(HotkeyListener listener) {
  173. hotkeyListeners.add(listener);
  174. }
  175. /**
  176. * Adds a listener for intellitype commands.
  177. * <p>
  178. * @param listener the IntellitypeListener to be added
  179. */
  180. public void addIntellitypeListener(IntellitypeListener listener) {
  181. intellitypeListeners.add(listener);
  182. }
  183. /**
  184. * Cleans up all resources used by JIntellitype.
  185. */
  186. public void cleanUp() {
  187. try {
  188. terminate();
  189. } catch (UnsatisfiedLinkError ex) {
  190. throw new JIntellitypeException(ERROR_MESSAGE, ex);
  191. } catch (RuntimeException ex) {
  192. throw new JIntellitypeException(ex);
  193. }
  194. }
  195. /**
  196. * Registers a Hotkey with windows. This combination will be responded to by
  197. * all registered HotKeyListeners. Uses the JIntellitypeConstants for MOD,
  198. * ALT, CTRL, and WINDOWS keys.
  199. * <p>
  200. * @param identifier a unique identifier for this key combination
  201. * @param modifier MOD_SHIFT, MOD_ALT, MOD_CONTROL, MOD_WIN from
  202. * JIntellitypeConstants, or 0 if no modifier needed
  203. * @param keycode the key to respond to in Ascii integer, 65 for A
  204. */
  205. public void registerHotKey(int identifier, int modifier, int keycode) {
  206. try {
  207. int modifiers = swingToIntelliType(modifier);
  208. if (modifiers == 0) {
  209. modifiers = modifier;
  210. }
  211. regHotKey(identifier, modifier, keycode);
  212. } catch (UnsatisfiedLinkError ex) {
  213. throw new JIntellitypeException(ERROR_MESSAGE, ex);
  214. } catch (RuntimeException ex) {
  215. throw new JIntellitypeException(ex);
  216. }
  217. }
  218. /**
  219. * Registers a Hotkey with windows. This combination will be responded to by
  220. * all registered HotKeyListeners. Use the Swing InputEvent constants from
  221. * java.awt.InputEvent.
  222. * <p>
  223. * @param identifier a unique identifier for this key combination
  224. * @param modifier InputEvent.SHIFT_MASK, InputEvent.ALT_MASK,
  225. * InputEvent.CTRL_MASK, or 0 if no modifier needed
  226. * @param keycode the key to respond to in Ascii integer, 65 for A
  227. */
  228. public void registerSwingHotKey(int identifier, int modifier, int keycode) {
  229. try {
  230. regHotKey(identifier, swingToIntelliType(modifier), keycode);
  231. } catch (UnsatisfiedLinkError ex) {
  232. throw new JIntellitypeException(ERROR_MESSAGE, ex);
  233. } catch (RuntimeException ex) {
  234. throw new JIntellitypeException(ex);
  235. }
  236. }
  237. /**
  238. * Registers a Hotkey with windows. This combination will be responded to by
  239. * all registered HotKeyListeners. Use the identifiers CTRL, SHIFT, ALT
  240. * and/or WIN.
  241. * <p>
  242. * @param identifier a unique identifier for this key combination
  243. * @param modifierAndKeyCode String with modifiers separated by + and keycode
  244. * (e.g. CTRL+SHIFT+A)
  245. * @see #registerHotKey(int, int, int)
  246. * @see #registerSwingHotKey(int, int, int)
  247. */
  248. public void registerHotKey(int identifier, String modifierAndKeyCode) {
  249. String[] split = modifierAndKeyCode.split("\\+");
  250. int mask = 0;
  251. int keycode = 0;
  252. for (int i = 0; i < split.length; i++) {
  253. if ("ALT".equalsIgnoreCase(split[i])) {
  254. mask += JIntellitype.MOD_ALT;
  255. } else if ("CTRL".equalsIgnoreCase(split[i]) || "CONTROL".equalsIgnoreCase(split[i])) {
  256. mask += JIntellitype.MOD_CONTROL;
  257. } else if ("SHIFT".equalsIgnoreCase(split[i])) {
  258. mask += JIntellitype.MOD_SHIFT;
  259. } else if ("WIN".equalsIgnoreCase(split[i])) {
  260. mask += JIntellitype.MOD_WIN;
  261. } else if (keycodeMap.containsKey(split[i].toLowerCase())) {
  262. keycode = keycodeMap.get(split[i].toLowerCase());
  263. }
  264. }
  265. registerHotKey(identifier, mask, keycode);
  266. }
  267. /**
  268. * Removes a listener for hotkeys.
  269. */
  270. public void removeHotKeyListener(HotkeyListener listener) {
  271. hotkeyListeners.remove(listener);
  272. }
  273. /**
  274. * Removes a listener for intellitype commands.
  275. */
  276. public void removeIntellitypeListener(IntellitypeListener listener) {
  277. intellitypeListeners.remove(listener);
  278. }
  279. /**
  280. * Unregisters a previously registered Hotkey identified by its unique
  281. * identifier.
  282. * <p>
  283. * @param identifier the unique identifer of this Hotkey
  284. */
  285. public void unregisterHotKey(int identifier) {
  286. try {
  287. unregHotKey(identifier);
  288. } catch (UnsatisfiedLinkError ex) {
  289. throw new JIntellitypeException(ERROR_MESSAGE, ex);
  290. } catch (RuntimeException ex) {
  291. throw new JIntellitypeException(ex);
  292. }
  293. }
  294. /**
  295. * Checks to see if this application is already running.
  296. * <p>
  297. * @param appTitle the name of the application to check for
  298. * @return true if running, false if not running
  299. */
  300. public static boolean checkInstanceAlreadyRunning(String appTitle) {
  301. return getInstance().isRunning(appTitle);
  302. }
  303. /**
  304. * Checks to make sure the OS is a Windows flavor and that the JIntellitype
  305. * DLL is found in the path and the JDK is 32 bit not 64 bit. The DLL
  306. * currently only supports 32 bit JDK.
  307. * <p>
  308. * @return true if Jintellitype may be used, false if not
  309. */
  310. public static boolean isJIntellitypeSupported() {
  311. boolean result = false;
  312. String os = "none";
  313. try {
  314. os = System.getProperty("os.name").toLowerCase();
  315. } catch (SecurityException ex) {
  316. // we are not allowed to look at this property
  317. System.err.println("Caught a SecurityException reading the system property "
  318. + "'os.name'; the SystemUtils property value will default to null.");
  319. }
  320. // only works on Windows OS currently
  321. if (os.startsWith("windows")) {
  322. // try an get the instance and if it succeeds then return true
  323. try {
  324. getInstance();
  325. result = true;
  326. } catch (Exception e) {
  327. result = false;
  328. }
  329. }
  330. return result;
  331. }
  332. /**
  333. * Gets the libraryLocation.
  334. * <p>
  335. * @return Returns the libraryLocation.
  336. */
  337. public static String getLibraryLocation() {
  338. return libraryLocation;
  339. }
  340. /**
  341. * Sets the libraryLocation.
  342. * <p>
  343. * @param libraryLocation The libraryLocation to set.
  344. */
  345. public static void setLibraryLocation(String libraryLocation) {
  346. final File dll = new File(libraryLocation);
  347. if (!dll.isAbsolute()) {
  348. JIntellitype.libraryLocation = dll.getAbsolutePath();
  349. } else {
  350. // absolute path, no further calculation needed
  351. JIntellitype.libraryLocation = libraryLocation;
  352. }
  353. }
  354. /**
  355. * Sets the libraryLocation of the DLL using a File object.
  356. * <p>
  357. * @param libraryFile the java.io.File representing the DLL
  358. */
  359. public static void setLibraryLocation(File libraryFile) {
  360. if (!libraryFile.isAbsolute()) {
  361. JIntellitype.libraryLocation = libraryFile.getAbsolutePath();
  362. }
  363. }
  364. /**
  365. * Notifies all listeners that Hotkey was pressed.
  366. * <p>
  367. * @param identifier the unique identifier received
  368. */
  369. protected void onHotKey(final int identifier) {
  370. for (final HotkeyListener hotkeyListener : hotkeyListeners) {
  371. SwingUtilities.invokeLater(new Runnable() {
  372. public void run() {
  373. hotkeyListener.onHotKey(identifier);
  374. }
  375. });
  376. }
  377. }
  378. /**
  379. * Notifies all listeners that Intellitype command was received.
  380. * <p>
  381. * @param command the unique WM_APPCOMMAND received
  382. */
  383. protected void onIntellitype(final int command) {
  384. for (final IntellitypeListener intellitypeListener : intellitypeListeners) {
  385. SwingUtilities.invokeLater(new Runnable() {
  386. public void run() {
  387. intellitypeListener.onIntellitype(command);
  388. }
  389. });
  390. }
  391. }
  392. /**
  393. * Swing modifier value to Jintellipad conversion. If no conversion needed
  394. * just return the original value. This lets users pass either the original
  395. * JIntellitype constants or Swing InputEvent constants.
  396. * <p>
  397. * @param swingKeystrokeModifier the Swing KeystrokeModifier to check
  398. * @return Jintellitype the JIntellitype modifier value
  399. */
  400. protected static int swingToIntelliType(int swingKeystrokeModifier) {
  401. int mask = 0;
  402. if ((swingKeystrokeModifier & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK
  403. || (swingKeystrokeModifier & InputEvent.SHIFT_DOWN_MASK) == InputEvent.SHIFT_DOWN_MASK) {
  404. mask |= JIntellitypeConstants.MOD_SHIFT;
  405. }
  406. if ((swingKeystrokeModifier & InputEvent.ALT_MASK) == InputEvent.ALT_MASK
  407. || (swingKeystrokeModifier & InputEvent.ALT_DOWN_MASK) == InputEvent.ALT_DOWN_MASK) {
  408. mask |= JIntellitypeConstants.MOD_ALT;
  409. }
  410. if ((swingKeystrokeModifier & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK
  411. || (swingKeystrokeModifier & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK) {
  412. mask |= JIntellitypeConstants.MOD_CONTROL;
  413. }
  414. if ((swingKeystrokeModifier & InputEvent.META_MASK) == InputEvent.META_MASK
  415. || (swingKeystrokeModifier & InputEvent.META_DOWN_MASK) == InputEvent.META_DOWN_MASK) {
  416. mask |= JIntellitypeConstants.MOD_WIN;
  417. }
  418. return mask;
  419. }
  420. /**
  421. * Puts all constants from {@link java.awt.event.KeyEvent} in a keycodeMap.
  422. * The key is the lower case form of it.
  423. * @return Map containing key->keycode mapping DOCU Now enables the user to
  424. * use all keys specified here instead of just [A-Z],[0-9] as before
  425. */
  426. private HashMap<String, Integer> getKey2KeycodeMapping() {
  427. HashMap<String, Integer> map = new HashMap<String, Integer>();
  428. map.put("first", KeyEvent.KEY_FIRST);
  429. map.put("last", KeyEvent.KEY_LAST);
  430. map.put("typed", KeyEvent.KEY_TYPED);
  431. map.put("pressed", KeyEvent.KEY_PRESSED);
  432. map.put("released", KeyEvent.KEY_RELEASED);
  433. map.put("enter", 13);
  434. map.put("back_space", KeyEvent.VK_BACK_SPACE);
  435. map.put("tab", KeyEvent.VK_TAB);
  436. map.put("cancel", KeyEvent.VK_CANCEL);
  437. map.put("clear", KeyEvent.VK_CLEAR);
  438. map.put("pause", KeyEvent.VK_PAUSE);
  439. map.put("caps_lock", KeyEvent.VK_CAPS_LOCK);
  440. map.put("escape", KeyEvent.VK_ESCAPE);
  441. map.put("space", KeyEvent.VK_SPACE);
  442. map.put("page_up", KeyEvent.VK_PAGE_UP);
  443. map.put("page_down", KeyEvent.VK_PAGE_DOWN);
  444. map.put("end", KeyEvent.VK_END);
  445. map.put("home", KeyEvent.VK_HOME);
  446. map.put("left", KeyEvent.VK_LEFT);
  447. map.put("up", KeyEvent.VK_UP);
  448. map.put("right", KeyEvent.VK_RIGHT);
  449. map.put("down", KeyEvent.VK_DOWN);
  450. map.put("comma", 188);
  451. map.put("minus", 109);
  452. map.put("period", 110);
  453. map.put("slash", 191);
  454. map.put("accent `", 192);
  455. map.put("0", KeyEvent.VK_0);
  456. map.put("1", KeyEvent.VK_1);
  457. map.put("2", KeyEvent.VK_2);
  458. map.put("3", KeyEvent.VK_3);
  459. map.put("4", KeyEvent.VK_4);
  460. map.put("5", KeyEvent.VK_5);
  461. map.put("6", KeyEvent.VK_6);
  462. map.put("7", KeyEvent.VK_7);
  463. map.put("8", KeyEvent.VK_8);
  464. map.put("9", KeyEvent.VK_9);
  465. map.put("semicolon", 186);
  466. map.put("equals", 187);
  467. map.put("a", KeyEvent.VK_A);
  468. map.put("b", KeyEvent.VK_B);
  469. map.put("c", KeyEvent.VK_C);
  470. map.put("d", KeyEvent.VK_D);
  471. map.put("e", KeyEvent.VK_E);
  472. map.put("f", KeyEvent.VK_F);
  473. map.put("g", KeyEvent.VK_G);
  474. map.put("h", KeyEvent.VK_H);
  475. map.put("i", KeyEvent.VK_I);
  476. map.put("j", KeyEvent.VK_J);
  477. map.put("k", KeyEvent.VK_K);
  478. map.put("l", KeyEvent.VK_L);
  479. map.put("m", KeyEvent.VK_M);
  480. map.put("n", KeyEvent.VK_N);
  481. map.put("o", KeyEvent.VK_O);
  482. map.put("p", KeyEvent.VK_P);
  483. map.put("q", KeyEvent.VK_Q);
  484. map.put("r", KeyEvent.VK_R);
  485. map.put("s", KeyEvent.VK_S);
  486. map.put("t", KeyEvent.VK_T);
  487. map.put("u", KeyEvent.VK_U);
  488. map.put("v", KeyEvent.VK_V);
  489. map.put("w", KeyEvent.VK_W);
  490. map.put("x", KeyEvent.VK_X);
  491. map.put("y", KeyEvent.VK_Y);
  492. map.put("z", KeyEvent.VK_Z);
  493. map.put("open_bracket", 219);
  494. map.put("back_slash", 220);
  495. map.put("close_bracket", 221);
  496. map.put("numpad0", KeyEvent.VK_NUMPAD0);
  497. map.put("numpad1", KeyEvent.VK_NUMPAD1);
  498. map.put("numpad2", KeyEvent.VK_NUMPAD2);
  499. map.put("numpad3", KeyEvent.VK_NUMPAD3);
  500. map.put("numpad4", KeyEvent.VK_NUMPAD4);
  501. map.put("numpad5", KeyEvent.VK_NUMPAD5);
  502. map.put("numpad6", KeyEvent.VK_NUMPAD6);
  503. map.put("numpad7", KeyEvent.VK_NUMPAD7);
  504. map.put("numpad8", KeyEvent.VK_NUMPAD8);
  505. map.put("numpad9", KeyEvent.VK_NUMPAD9);
  506. map.put("multiply", KeyEvent.VK_MULTIPLY);
  507. map.put("add", KeyEvent.VK_ADD);
  508. map.put("separator", KeyEvent.VK_SEPARATOR);
  509. map.put("subtract", KeyEvent.VK_SUBTRACT);
  510. map.put("decimal", KeyEvent.VK_DECIMAL);
  511. map.put("divide", KeyEvent.VK_DIVIDE);
  512. map.put("delete", 46);
  513. map.put("num_lock", KeyEvent.VK_NUM_LOCK);
  514. map.put("scroll_lock", KeyEvent.VK_SCROLL_LOCK);
  515. map.put("f1", KeyEvent.VK_F1);
  516. map.put("f2", KeyEvent.VK_F2);
  517. map.put("f3", KeyEvent.VK_F3);
  518. map.put("f4", KeyEvent.VK_F4);
  519. map.put("f5", KeyEvent.VK_F5);
  520. map.put("f6", KeyEvent.VK_F6);
  521. map.put("f7", KeyEvent.VK_F7);
  522. map.put("f8", KeyEvent.VK_F8);
  523. map.put("f9", KeyEvent.VK_F9);
  524. map.put("f10", KeyEvent.VK_F10);
  525. map.put("f11", KeyEvent.VK_F11);
  526. map.put("f12", KeyEvent.VK_F12);
  527. map.put("f13", KeyEvent.VK_F13);
  528. map.put("f14", KeyEvent.VK_F14);
  529. map.put("f15", KeyEvent.VK_F15);
  530. map.put("f16", KeyEvent.VK_F16);
  531. map.put("f17", KeyEvent.VK_F17);
  532. map.put("f18", KeyEvent.VK_F18);
  533. map.put("f19", KeyEvent.VK_F19);
  534. map.put("f20", KeyEvent.VK_F20);
  535. map.put("f21", KeyEvent.VK_F21);
  536. map.put("f22", KeyEvent.VK_F22);
  537. map.put("f23", KeyEvent.VK_F23);
  538. map.put("f24", KeyEvent.VK_F24);
  539. map.put("printscreen", 44);
  540. map.put("insert", 45);
  541. map.put("help", 47);
  542. map.put("meta", KeyEvent.VK_META);
  543. map.put("back_quote", KeyEvent.VK_BACK_QUOTE);
  544. map.put("quote", KeyEvent.VK_QUOTE);
  545. map.put("kp_up", KeyEvent.VK_KP_UP);
  546. map.put("kp_down", KeyEvent.VK_KP_DOWN);
  547. map.put("kp_left", KeyEvent.VK_KP_LEFT);
  548. map.put("kp_right", KeyEvent.VK_KP_RIGHT);
  549. map.put("dead_grave", KeyEvent.VK_DEAD_GRAVE);
  550. map.put("dead_acute", KeyEvent.VK_DEAD_ACUTE);
  551. map.put("dead_circumflex", KeyEvent.VK_DEAD_CIRCUMFLEX);
  552. map.put("dead_tilde", KeyEvent.VK_DEAD_TILDE);
  553. map.put("dead_macron", KeyEvent.VK_DEAD_MACRON);
  554. map.put("dead_breve", KeyEvent.VK_DEAD_BREVE);
  555. map.put("dead_abovedot", KeyEvent.VK_DEAD_ABOVEDOT);
  556. map.put("dead_diaeresis", KeyEvent.VK_DEAD_DIAERESIS);
  557. map.put("dead_abovering", KeyEvent.VK_DEAD_ABOVERING);
  558. map.put("dead_doubleacute", KeyEvent.VK_DEAD_DOUBLEACUTE);
  559. map.put("dead_caron", KeyEvent.VK_DEAD_CARON);
  560. map.put("dead_cedilla", KeyEvent.VK_DEAD_CEDILLA);
  561. map.put("dead_ogonek", KeyEvent.VK_DEAD_OGONEK);
  562. map.put("dead_iota", KeyEvent.VK_DEAD_IOTA);
  563. map.put("dead_voiced_sound", KeyEvent.VK_DEAD_VOICED_SOUND);
  564. map.put("dead_semivoiced_sound", KeyEvent.VK_DEAD_SEMIVOICED_SOUND);
  565. map.put("ampersand", KeyEvent.VK_AMPERSAND);
  566. map.put("asterisk", KeyEvent.VK_ASTERISK);
  567. map.put("quotedbl", KeyEvent.VK_QUOTEDBL);
  568. map.put("less", KeyEvent.VK_LESS);
  569. map.put("greater", KeyEvent.VK_GREATER);
  570. map.put("braceleft", KeyEvent.VK_BRACELEFT);
  571. map.put("braceright", KeyEvent.VK_BRACERIGHT);
  572. map.put("at", KeyEvent.VK_AT);
  573. map.put("colon", KeyEvent.VK_COLON);
  574. map.put("circumflex", KeyEvent.VK_CIRCUMFLEX);
  575. map.put("dollar", KeyEvent.VK_DOLLAR);
  576. map.put("euro_sign", KeyEvent.VK_EURO_SIGN);
  577. map.put("exclamation_mark", KeyEvent.VK_EXCLAMATION_MARK);
  578. map.put("inverted_exclamation_mark", KeyEvent.VK_INVERTED_EXCLAMATION_MARK);
  579. map.put("left_parenthesis", KeyEvent.VK_LEFT_PARENTHESIS);
  580. map.put("number_sign", KeyEvent.VK_NUMBER_SIGN);
  581. map.put("plus", KeyEvent.VK_PLUS);
  582. map.put("right_parenthesis", KeyEvent.VK_RIGHT_PARENTHESIS);
  583. map.put("underscore", KeyEvent.VK_UNDERSCORE);
  584. map.put("context_menu", KeyEvent.VK_CONTEXT_MENU);
  585. map.put("final", KeyEvent.VK_FINAL);
  586. map.put("convert", KeyEvent.VK_CONVERT);
  587. map.put("nonconvert", KeyEvent.VK_NONCONVERT);
  588. map.put("accept", KeyEvent.VK_ACCEPT);
  589. map.put("modechange", KeyEvent.VK_MODECHANGE);
  590. map.put("kana", KeyEvent.VK_KANA);
  591. map.put("kanji", KeyEvent.VK_KANJI);
  592. map.put("alphanumeric", KeyEvent.VK_ALPHANUMERIC);
  593. map.put("katakana", KeyEvent.VK_KATAKANA);
  594. map.put("hiragana", KeyEvent.VK_HIRAGANA);
  595. map.put("full_width", KeyEvent.VK_FULL_WIDTH);
  596. map.put("half_width", KeyEvent.VK_HALF_WIDTH);
  597. map.put("roman_characters", KeyEvent.VK_ROMAN_CHARACTERS);
  598. map.put("all_candidates", KeyEvent.VK_ALL_CANDIDATES);
  599. map.put("previous_candidate", KeyEvent.VK_PREVIOUS_CANDIDATE);
  600. map.put("code_input", KeyEvent.VK_CODE_INPUT);
  601. map.put("japanese_katakana", KeyEvent.VK_JAPANESE_KATAKANA);
  602. map.put("japanese_hiragana", KeyEvent.VK_JAPANESE_HIRAGANA);
  603. map.put("japanese_roman", KeyEvent.VK_JAPANESE_ROMAN);
  604. map.put("kana_lock", KeyEvent.VK_KANA_LOCK);
  605. map.put("input_method_on_off", KeyEvent.VK_INPUT_METHOD_ON_OFF);
  606. map.put("cut", KeyEvent.VK_CUT);
  607. map.put("copy", KeyEvent.VK_COPY);
  608. map.put("paste", KeyEvent.VK_PASTE);
  609. map.put("undo", KeyEvent.VK_UNDO);
  610. map.put("again", KeyEvent.VK_AGAIN);
  611. map.put("find", KeyEvent.VK_FIND);
  612. map.put("props", KeyEvent.VK_PROPS);
  613. map.put("stop", KeyEvent.VK_STOP);
  614. map.put("compose", KeyEvent.VK_COMPOSE);
  615. map.put("alt_graph", KeyEvent.VK_ALT_GRAPH);
  616. map.put("begin", KeyEvent.VK_BEGIN);
  617. return map;
  618. }
  619. private synchronized native void initializeLibrary() throws UnsatisfiedLinkError;
  620. private synchronized native void regHotKey(int identifier, int modifier, int keycode) throws UnsatisfiedLinkError;
  621. private synchronized native void terminate() throws UnsatisfiedLinkError;
  622. private synchronized native void unregHotKey(int identifier) throws UnsatisfiedLinkError;
  623. /**
  624. * Checks if there's an instance with hidden window title = appName running
  625. * Can be used to detect that another instance of your app is already running
  626. * (so exit..)
  627. * <p>
  628. * @param appName = the title of the hidden window to search for
  629. */
  630. private synchronized native boolean isRunning(String appName);
  631. }