00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.gui.keybindings;
00023
00024 import java.awt.event.KeyEvent;
00025 import java.lang.reflect.Field;
00026 import java.util.HashMap;
00027 import java.util.Map;
00028 import org.jetbrains.annotations.NotNull;
00029
00034 public class KeyCodeMap {
00035
00039 @NotNull
00040 private final Map<String, Integer> keyCodes = new HashMap<String, Integer>();
00041
00045 @NotNull
00046 private final Map<Integer, String> keyNames = new HashMap<Integer, String>();
00047
00051 public KeyCodeMap() {
00052 for (final Field field : KeyEvent.class.getDeclaredFields()) {
00053 if (field.getName().startsWith("VK_")) {
00054 final int keyCode;
00055 try {
00056 keyCode = field.getInt(null);
00057 } catch (final SecurityException ignored) {
00058 continue;
00059 } catch (final IllegalArgumentException ignored) {
00060 continue;
00061 } catch (final IllegalAccessException ignored) {
00062 continue;
00063 } catch (final NullPointerException ignored) {
00064 continue;
00065 } catch (final ExceptionInInitializerError ignored) {
00066 continue;
00067 }
00068 final String keyName = field.getName().substring(3);
00069
00070 keyCodes.put(keyName, keyCode);
00071 keyNames.put(keyCode, keyName);
00072 }
00073 }
00074 }
00075
00082 public int getKeyCode(@NotNull final String keyName) throws NoSuchKeyCodeException {
00083 if (keyCodes.containsKey(keyName)) {
00084 return keyCodes.get(keyName);
00085 }
00086
00087 try {
00088 return Integer.parseInt(keyName);
00089 } catch (final NumberFormatException ex) {
00090 final NoSuchKeyCodeException noSuchKeyCodeException = new NoSuchKeyCodeException();
00091 noSuchKeyCodeException.initCause(ex);
00092 throw noSuchKeyCodeException;
00093 }
00094 }
00095
00101 @NotNull
00102 public String getKeyName(final int keyCode) {
00103 final String keyName = keyNames.get(keyCode);
00104 if (keyName != null) {
00105 return keyName;
00106 }
00107
00108 return Integer.toString(keyCode);
00109 }
00110
00111 }