Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.gui.keybindings;
24 
25 import java.awt.event.KeyEvent;
26 import java.lang.reflect.Field;
27 import java.util.HashMap;
28 import java.util.Map;
29 import org.jetbrains.annotations.NotNull;
30 
35 public class KeyCodeMap {
36 
40  @NotNull
41  private final Map<String, Integer> keyCodes = new HashMap<>();
42 
46  @NotNull
47  private final Map<Integer, String> keyNames = new HashMap<>();
48 
52  public KeyCodeMap() {
53  for (Field field : KeyEvent.class.getDeclaredFields()) {
54  if (field.getName().startsWith("VK_")) {
55  final int keyCode;
56  //noinspection ErrorNotRethrown
57  try {
58  keyCode = field.getInt(null);
59  } catch (final ExceptionInInitializerError|IllegalAccessException|IllegalArgumentException|NullPointerException|SecurityException ignored) {
60  continue;
61  }
62  final String keyName = field.getName().substring(3);
63 
64  keyCodes.put(keyName, keyCode);
65  keyNames.put(keyCode, keyName);
66  }
67  }
68  }
69 
76  public int getKeyCode(@NotNull final String keyName) throws NoSuchKeyCodeException {
77  if (keyCodes.containsKey(keyName)) {
78  return keyCodes.get(keyName);
79  }
80 
81  try {
82  return Integer.parseInt(keyName);
83  } catch (final NumberFormatException ex) {
84  final NoSuchKeyCodeException noSuchKeyCodeException = new NoSuchKeyCodeException();
85  noSuchKeyCodeException.initCause(ex);
86  throw noSuchKeyCodeException;
87  }
88  }
89 
95  @NotNull
96  public String getKeyName(final int keyCode) {
97  final String keyName = keyNames.get(keyCode);
98  if (keyName != null) {
99  return keyName;
100  }
101 
102  return Integer.toString(keyCode);
103  }
104 
105 }
com.realtime.crossfire.jxclient.gui.keybindings.KeyCodeMap.getKeyCode
int getKeyCode(@NotNull final String keyName)
Definition: KeyCodeMap.java:76
com.realtime.crossfire.jxclient.gui.keybindings.KeyCodeMap.getKeyName
String getKeyName(final int keyCode)
Definition: KeyCodeMap.java:96
com.realtime.crossfire.jxclient.gui.keybindings.NoSuchKeyCodeException
Definition: NoSuchKeyCodeException.java:29
com.realtime.crossfire.jxclient.gui.keybindings.KeyCodeMap.KeyCodeMap
KeyCodeMap()
Definition: KeyCodeMap.java:52
com.realtime.crossfire.jxclient.gui.keybindings.KeyCodeMap.keyCodes
final Map< String, Integer > keyCodes
Definition: KeyCodeMap.java:41
com.realtime.crossfire.jxclient.gui.keybindings.KeyCodeMap
Definition: KeyCodeMap.java:35
com.realtime.crossfire.jxclient.gui.keybindings.KeyCodeMap.keyNames
final Map< Integer, String > keyNames
Definition: KeyCodeMap.java:47