Crossfire JXClient, Trunk  R20561
KeyCodeMap.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.keybindings;
23 
24 import java.awt.event.KeyEvent;
25 import java.lang.reflect.Field;
26 import java.util.HashMap;
27 import java.util.Map;
28 import org.jetbrains.annotations.NotNull;
29 
34 public class KeyCodeMap {
35 
39  @NotNull
40  private final Map<String, Integer> keyCodes = new HashMap<>();
41 
45  @NotNull
46  private final Map<Integer, String> keyNames = new HashMap<>();
47 
51  public KeyCodeMap() {
52  for (final Field field : KeyEvent.class.getDeclaredFields()) {
53  if (field.getName().startsWith("VK_")) {
54  final int keyCode;
55  //noinspection ErrorNotRethrown
56  try {
57  keyCode = field.getInt(null);
58  } catch (final SecurityException ignored) {
59  continue;
60  } catch (final IllegalArgumentException ignored) {
61  continue;
62  } catch (final IllegalAccessException ignored) {
63  continue;
64  } catch (final NullPointerException ignored) {
65  continue;
66  } catch (final ExceptionInInitializerError ignored) {
67  continue;
68  }
69  final String keyName = field.getName().substring(3);
70 
71  keyCodes.put(keyName, keyCode);
72  keyNames.put(keyCode, keyName);
73  }
74  }
75  }
76 
83  public int getKeyCode(@NotNull final String keyName) throws NoSuchKeyCodeException {
84  if (keyCodes.containsKey(keyName)) {
85  return keyCodes.get(keyName);
86  }
87 
88  try {
89  return Integer.parseInt(keyName);
90  } catch (final NumberFormatException ex) {
91  final NoSuchKeyCodeException noSuchKeyCodeException = new NoSuchKeyCodeException();
92  noSuchKeyCodeException.initCause(ex);
93  throw noSuchKeyCodeException;
94  }
95  }
96 
102  @NotNull
103  public String getKeyName(final int keyCode) {
104  final String keyName = keyNames.get(keyCode);
105  if (keyName != null) {
106  return keyName;
107  }
108 
109  return Integer.toString(keyCode);
110  }
111 
112 }
final Map< Integer, String > keyNames
Maps key code to key name.
Definition: KeyCodeMap.java:46
String getKeyName(final int keyCode)
Returns the key name for a key code.
Maps between key codes integer constants and string representations.
Definition: KeyCodeMap.java:34
int getKeyCode(@NotNull final String keyName)
Returns the key code for a key name.
Definition: KeyCodeMap.java:83
final Map< String, Integer > keyCodes
Maps key name to key code.
Definition: KeyCodeMap.java:40