Results 1 to 20 of 35
Thread: Mapping Key Events
- 06-24-2012, 02:44 AM #1
Mapping Key Events
Ok I'm trying to find a way to convert strings into their keycode equivalent. Ie. a is KeyEvent.VK_A. I need it to incorporate all keyboard values that can be used in a username and password field. I Googled it and the only answers I found were to hard code each one which seems extremely tedious and inefficient. Then I found one solution saying to map it all. I believe I did this right and I'm just trying to get the information incorrectly but I can't seem to figure out how to make it work.
I get a NullPointerException when I run it. I placed a comment on the line it refers too.
I guess what I'm asking is am I on the right track and can someone point me in the direction needed to fix my NullPointerException?
Shouldn't that line print whatever the (V) keyobject is in the Map into at the first slot?Java Code:import java.awt.*; import java.awt.event.*; import java.lang.reflect.*; import java.util.Map; public class KeyMap{ public static Map<String, Integer> into; public static void main (String[] args) throws AWTException{ //Get all available keys and map them try { Field[] fields = KeyEvent.class.getDeclaredFields(); for(Field f : fields) { if(f.getName().startsWith("VK_")) { //we only want these fields int code = ((Integer)f.get(null)).intValue(); into.put(f.getName().substring(3), code); } } } catch(Exception ex) {} for (int h = 0; h<5; h++){ System.out.println(into.get(h)); //This throws a NullPointerException I believe this should print the first 4 array indexes of the map right? } } }- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 06-24-2012, 03:10 AM #2
Re: Mapping Key Events
When you get an error please post the full text of the error message.
Some comments: You should call the printStackTrace() method in the catch block. Do NOT ignore errors. You are missing an error!!!
Why do you think there are 5 elements in into?Last edited by Norm; 06-24-2012 at 03:13 AM.
If you don't understand my response, don't ignore it, ask a question.
- 06-24-2012, 03:11 AM #3
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
Re: Mapping Key Events
Hi Dark,
I'm not familiar with the Map class but running your code shows that 'into' has not been initialised causing your null pointer exception.
Additionally, only seven fields are found in the 'fields' array after calling the getDeclaredFields() method, only one of these starts with "VK_" (Use a println statement or debug to see the content). As there is only one entry put in 'into' the program will still crash with an OutOfBoundsException as 'h' is not limited by the size of 'into' and will try to retrieve an entry outside of the array.
Regards.
- 06-24-2012, 03:21 AM #4
Re: Mapping Key Events
The full error is:
From my understanding of what the Map is for, to get all the VK_ codes for the keyboard so it should be at least 64 for the letters alone. This is the entire list of them, there are a lot of VK_ codes. KeyEvent (Java Platform SE 6)Java Code:Exception in thread "main" java.lang.NullPointerException at KeyMap.main(KeyMap.java:27)
Why is it only returning 1 field that starts with VK_?- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 06-24-2012, 03:26 AM #5
Re: Mapping Key Events
Add the call to printStackTrace() for more information.
If you don't understand my response, don't ignore it, ask a question.
- 06-24-2012, 03:29 AM #6
Re: Mapping Key Events
@Ronin
You should read before giving advice.I'm not familiar with the Map class
Read the API doc for the get() method.As there is only one entry put in 'into' the program will still crash with an OutOfBoundsException as 'h' is not limited by the size of 'into'
This is another example of how "autoboxing" fools beginning programmers.If you don't understand my response, don't ignore it, ask a question.
- 06-24-2012, 03:29 AM #7
Re: Mapping Key Events
This is the stack trace
Line 19 isJava Code:java.lang.NullPointerException at KeyMap.main(KeyMap.java:19) Exception in thread "main" java.lang.NullPointerException at KeyMap.main(KeyMap.java:27)
Java Code:into.put(f.getName().substring(3), code);
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 06-24-2012, 03:32 AM #8
Re: Mapping Key Events
Where is into given a value? Ronin pointed that out in post#3
If you don't understand my response, don't ignore it, ask a question.
- 06-24-2012, 03:48 AM #9
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
- 06-25-2012, 01:23 AM #10
Re: Mapping Key Events
Ok so this snippet of code works. However this is what it displays. Why are some of these fields null? Each time it should only put the VK_ commands into the map. Here are my results, sorry for the really long post. I couldn't figure out how to make it collapsible.Java Code:public class KeyMap{ private static final Map<Integer, String> into = new HashMap<Integer, String>(); public static int i =0; static{ try{ Field[] fields = KeyEvent.class.getDeclaredFields(); for(Field f: fields) { if(f.getName().startsWith("VK_")) { int code = ((Integer)f.get(null)).intValue(); into.put(code, f.getName().substring(3)); System.out.println(into.get(i)); i++; System.out.println(i); } } } catch(Exception ex){ex.printStackTrace();} } }
Java Code:null 1 null 2 null 3 null 4 null 5 null 6 null 7 null 8 BACK_SPACE 9 TAB 10 ENTER 11 null 12 CLEAR 13 null 14 null 15 null 16 SHIFT 17 CONTROL 18 ALT 19 PAUSE 20 CAPS_LOCK 21 null 22 null 23 null 24 null 25 null 26 null 27 ESCAPE 28 null 29 null 30 null 31 null 32 SPACE 33 PAGE_UP 34 PAGE_DOWN 35 END 36 HOME 37 LEFT 38 UP 39 RIGHT 40 DOWN 41 null 42 null 43 null 44 COMMA 45 MINUS 46 PERIOD 47 SLASH 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 null 59 SEMICOLON 60 null 61 EQUALS 62 null 63 null 64 null 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 OPEN_BRACKET 92 BACK_SLASH 93 CLOSE_BRACKET 94 null 95 null 96 NUMPAD0 97 NUMPAD1 98 NUMPAD2 99 NUMPAD3 100 NUMPAD4 101 NUMPAD5 102 NUMPAD6 103 NUMPAD7 104 NUMPAD8 105 NUMPAD9 106 MULTIPLY 107 ADD 108 SEPARATOR 109 SUBTRACT 110 DECIMAL 111 DIVIDE 112 F1 113 F2 114 F3 115 F4 116 F5 117 F6 118 F7 119 F8 120 F9 121 F10 122 F11 123 F12 124 null 125 null 126 null 127 DELETE 128 DEAD_GRAVE 129 DEAD_ACUTE 130 DEAD_CIRCUMFLEX 131 DEAD_TILDE 132 DEAD_MACRON 133 DEAD_BREVE 134 DEAD_ABOVEDOT 135 DEAD_DIAERESIS 136 DEAD_ABOVERING 137 DEAD_DOUBLEACUTE 138 DEAD_CARON 139 DEAD_CEDILLA 140 DEAD_OGONEK 141 DEAD_IOTA 142 DEAD_VOICED_SOUND 143 DEAD_SEMIVOICED_SOUND 144 NUM_LOCK 145 SCROLL_LOCK 146 null 147 null 148 null 149 null 150 AMPERSAND 151 ASTERISK 152 QUOTEDBL 153 LESS 154 PRINTSCREEN 155 INSERT 156 HELP 157 META 158 null 159 null 160 GREATER 161 BRACELEFT 162 BRACERIGHT 163 null 164 null 165 null 166 null 167 null 168 null 169 null 170 null 171 null 172 null 173 null 174 null 175 null 176 null 177 null 178 null 179 null 180 null 181 null 182 null 183 null 184 null 185 null 186 null 187 null 188 null 189 null
Last edited by Dark; 06-25-2012 at 02:36 AM.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 06-25-2012, 02:25 AM #11
Re: Mapping Key Events
Your printout would be better if you printed the value of the key(i) and its value on the same line vs 2 lines.
There might not be a value for the key yet.Why are some of these fields null?Last edited by Norm; 06-25-2012 at 02:28 AM.
If you don't understand my response, don't ignore it, ask a question.
- 06-25-2012, 02:42 AM #12
Re: Mapping Key Events
Good idea, I fixed it and looking through it and I still don't understand why some of the values are null. Am I misunderstanding what is happening here? I want it to index only the VK_ commands.
I need a-z, A-Z, 1-0. `~!@#$%^&*()_+-=[]{}\|;':",.<>/?
I'm pretty sure I have to utilize shift to get uppercase and the secondary function of the keys.Last edited by Dark; 06-25-2012 at 02:48 AM.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 06-25-2012, 02:44 AM #13
Re: Mapping Key Events
Look at the definition of the get() method. When does it return a null value?I still don't understand why some of the values are null.If you don't understand my response, don't ignore it, ask a question.
- 06-25-2012, 03:01 AM #14
Re: Mapping Key Events
Hmm, so the VK_ commands don't have a key mapping for everything its cycling through. Now I need to figure out which ones aren't getting mapped and how to exclude them. Any ideas on the easiest route to take?
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 06-25-2012, 03:06 AM #15
Re: Mapping Key Events
I thought this would exclude themhow to exclude themYou only add the ones with names starting with VK_Java Code:if(f.getName().startsWith("VK_")) {
If you get all the keys from the Map (there is a method to do that) there will not be any holes when you get() from the Map.If you don't understand my response, don't ignore it, ask a question.
- 06-25-2012, 03:12 AM #16
Re: Mapping Key Events
I made it display all the info. f.getName(), the index and the key. There are VK_ commands that aren't filling in the string value so its defaulting to null.
Java Code:1 VK_ENTER null 2 VK_BACK_SPACE null 3 VK_TAB null 4 VK_CANCEL null 5 VK_CLEAR null 6 VK_SHIFT null 7 VK_CONTROL null 8 VK_ALT BACK_SPACE 9 VK_PAUSE TAB 10 VK_CAPS_LOCK ENTER 11 VK_ESCAPE null 12 VK_SPACE CLEAR 13 VK_PAGE_UP null 14 VK_PAGE_DOWN null 15 VK_END null 16 VK_HOME SHIFT 17 VK_LEFT CONTROL 18 VK_UP ALT 19 VK_RIGHT PAUSE 20 VK_DOWN CAPS_LOCK 21 VK_COMMA null 22 VK_MINUS null 23 VK_PERIOD null 24 VK_SLASH null 25 VK_0 null 26 VK_1 null 27 VK_2 ESCAPE 28 VK_3 null 29 VK_4 null 30 VK_5 null 31 VK_6 null 32 VK_7 SPACE 33 VK_8 PAGE_UP 34 VK_9 PAGE_DOWN 35 VK_SEMICOLON END 36 VK_EQUALS HOME 37 VK_A LEFT 38 VK_B UP 39 VK_C RIGHT 40 VK_D DOWN 41 VK_E null 42 VK_F null 43 VK_G null 44 VK_H COMMA 45 VK_I MINUS 46 VK_J PERIOD 47 VK_K SLASH 48 VK_L 0 49 VK_M 1 50 VK_N 2 51 VK_O 3 52 VK_P 4 53 VK_Q 5 54 VK_R 6 55 VK_S 7 56 VK_T 8 57 VK_U 9 58 VK_V null 59 VK_W SEMICOLON 60 VK_X null 61 VK_Y EQUALS 62 VK_Z null 63 VK_OPEN_BRACKET null 64 VK_BACK_SLASH null 65 VK_CLOSE_BRACKET A 66 VK_NUMPAD0 B 67 VK_NUMPAD1 C 68 VK_NUMPAD2 D 69 VK_NUMPAD3 E 70 VK_NUMPAD4 F 71 VK_NUMPAD5 G 72 VK_NUMPAD6 H 73 VK_NUMPAD7 I 74 VK_NUMPAD8 J 75 VK_NUMPAD9 K 76 VK_MULTIPLY L 77 VK_ADD M 78 VK_SEPARATER N 79 VK_SEPARATOR O 80 VK_SUBTRACT P 81 VK_DECIMAL Q 82 VK_DIVIDE R 83 VK_DELETE S 84 VK_NUM_LOCK T 85 VK_SCROLL_LOCK U 86 VK_F1 V 87 VK_F2 W 88 VK_F3 X 89 VK_F4 Y 90 VK_F5 Z 91 VK_F6 OPEN_BRACKET 92 VK_F7 BACK_SLASH 93 VK_F8 CLOSE_BRACKET 94 VK_F9 null 95 VK_F10 null 96 VK_F11 NUMPAD0 97 VK_F12 NUMPAD1 98 VK_F13 NUMPAD2 99 VK_F14 NUMPAD3 100 VK_F15 NUMPAD4 101 VK_F16 NUMPAD5 102 VK_F17 NUMPAD6 103 VK_F18 NUMPAD7 104 VK_F19 NUMPAD8 105 VK_F20 NUMPAD9 106 VK_F21 MULTIPLY 107 VK_F22 ADD 108 VK_F23 SEPARATOR 109 VK_F24 SUBTRACT 110 VK_PRINTSCREEN DECIMAL 111 VK_INSERT DIVIDE 112 VK_HELP F1 113 VK_META F2 114 VK_BACK_QUOTE F3 115 VK_QUOTE F4 116 VK_KP_UP F5 117 VK_KP_DOWN F6 118 VK_KP_LEFT F7 119 VK_KP_RIGHT F8 120 VK_DEAD_GRAVE F9 121 VK_DEAD_ACUTE F10 122 VK_DEAD_CIRCUMFLEX F11 123 VK_DEAD_TILDE F12 124 VK_DEAD_MACRON null 125 VK_DEAD_BREVE null 126 VK_DEAD_ABOVEDOT null 127 VK_DEAD_DIAERESIS DELETE 128 VK_DEAD_ABOVERING DEAD_GRAVE 129 VK_DEAD_DOUBLEACUTE DEAD_ACUTE 130 VK_DEAD_CARON DEAD_CIRCUMFLEX 131 VK_DEAD_CEDILLA DEAD_TILDE 132 VK_DEAD_OGONEK DEAD_MACRON 133 VK_DEAD_IOTA DEAD_BREVE 134 VK_DEAD_VOICED_SOUND DEAD_ABOVEDOT 135 VK_DEAD_SEMIVOICED_SOUND DEAD_DIAERESIS 136 VK_AMPERSAND DEAD_ABOVERING 137 VK_ASTERISK DEAD_DOUBLEACUTE 138 VK_QUOTEDBL DEAD_CARON 139 VK_LESS DEAD_CEDILLA 140 VK_GREATER DEAD_OGONEK 141 VK_BRACELEFT DEAD_IOTA 142 VK_BRACERIGHT DEAD_VOICED_SOUND 143 VK_AT DEAD_SEMIVOICED_SOUND 144 VK_COLON NUM_LOCK 145 VK_CIRCUMFLEX SCROLL_LOCK 146 VK_DOLLAR null 147 VK_EURO_SIGN null 148 VK_EXCLAMATION_MARK null 149 VK_INVERTED_EXCLAMATION_MARK null 150 VK_LEFT_PARENTHESIS AMPERSAND 151 VK_NUMBER_SIGN ASTERISK 152 VK_PLUS QUOTEDBL 153 VK_RIGHT_PARENTHESIS LESS 154 VK_UNDERSCORE PRINTSCREEN 155 VK_WINDOWS INSERT 156 VK_CONTEXT_MENU HELP 157 VK_FINAL META 158 VK_CONVERT null 159 VK_NONCONVERT null 160 VK_ACCEPT GREATER 161 VK_MODECHANGE BRACELEFT 162 VK_KANA BRACERIGHT 163 VK_KANJI null 164 VK_ALPHANUMERIC null 165 VK_KATAKANA null 166 VK_HIRAGANA null 167 VK_FULL_WIDTH null 168 VK_HALF_WIDTH null 169 VK_ROMAN_CHARACTERS null 170 VK_ALL_CANDIDATES null 171 VK_PREVIOUS_CANDIDATE null 172 VK_CODE_INPUT null 173 VK_JAPANESE_KATAKANA null 174 VK_JAPANESE_HIRAGANA null 175 VK_JAPANESE_ROMAN null 176 VK_KANA_LOCK null 177 VK_INPUT_METHOD_ON_OFF null 178 VK_CUT null 179 VK_COPY null 180 VK_PASTE null 181 VK_UNDO null 182 VK_AGAIN null 183 VK_FIND null 184 VK_PROPS null 185 VK_STOP null 186 VK_COMPOSE null 187 VK_ALT_GRAPH null 188 VK_BEGIN null 189 VK_UNDEFINED null
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 06-25-2012, 03:26 AM #17
Re: Mapping Key Events
Here's what I get for values from 0 to 256, skipping the null values:
Running: java KeyMap
0 error(s)Java Code:k=0 v=UNDEFINED k=3 v=CANCEL k=8 v=BACK_SPACE k=9 v=TAB k=10 v=ENTER k=12 v=CLEAR k=16 v=SHIFT k=17 v=CONTROL k=18 v=ALT k=19 v=PAUSE k=20 v=CAPS_LOCK k=21 v=KANA k=24 v=FINAL k=25 v=KANJI k=27 v=ESCAPE k=28 v=CONVERT k=29 v=NONCONVERT k=30 v=ACCEPT k=31 v=MODECHANGE k=32 v=SPACE k=33 v=PAGE_UP k=34 v=PAGE_DOWN k=35 v=END k=36 v=HOME k=37 v=LEFT k=38 v=UP k=39 v=RIGHT k=40 v=DOWN k=44 v=COMMA k=45 v=MINUS k=46 v=PERIOD k=47 v=SLASH k=48 v=0 k=49 v=1 k=50 v=2 k=51 v=3 k=52 v=4 k=53 v=5 k=54 v=6 k=55 v=7 k=56 v=8 k=57 v=9 k=59 v=SEMICOLON k=61 v=EQUALS k=65 v=A k=66 v=B k=67 v=C k=68 v=D k=69 v=E k=70 v=F k=71 v=G k=72 v=H k=73 v=I k=74 v=J k=75 v=K k=76 v=L k=77 v=M k=78 v=N k=79 v=O k=80 v=P k=81 v=Q k=82 v=R k=83 v=S k=84 v=T k=85 v=U k=86 v=V k=87 v=W k=88 v=X k=89 v=Y k=90 v=Z k=91 v=OPEN_BRACKET k=92 v=BACK_SLASH k=93 v=CLOSE_BRACKET k=96 v=NUMPAD0 k=97 v=NUMPAD1 k=98 v=NUMPAD2 k=99 v=NUMPAD3 k=100 v=NUMPAD4 k=101 v=NUMPAD5 k=102 v=NUMPAD6 k=103 v=NUMPAD7 k=104 v=NUMPAD8 k=105 v=NUMPAD9 k=106 v=MULTIPLY k=107 v=ADD k=108 v=SEPARATOR k=109 v=SUBTRACT k=110 v=DECIMAL k=111 v=DIVIDE k=112 v=F1 k=113 v=F2 k=114 v=F3 k=115 v=F4 k=116 v=F5 k=117 v=F6 k=118 v=F7 k=119 v=F8 k=120 v=F9 k=121 v=F10 k=122 v=F11 k=123 v=F12 k=127 v=DELETE k=128 v=DEAD_GRAVE k=129 v=DEAD_ACUTE k=130 v=DEAD_CIRCUMFLEX k=131 v=DEAD_TILDE k=132 v=DEAD_MACRON k=133 v=DEAD_BREVE k=134 v=DEAD_ABOVEDOT k=135 v=DEAD_DIAERESIS k=136 v=DEAD_ABOVERING k=137 v=DEAD_DOUBLEACUTE k=138 v=DEAD_CARON k=139 v=DEAD_CEDILLA k=140 v=DEAD_OGONEK k=141 v=DEAD_IOTA k=142 v=DEAD_VOICED_SOUND k=143 v=DEAD_SEMIVOICED_SOUND k=144 v=NUM_LOCK k=145 v=SCROLL_LOCK k=150 v=AMPERSAND k=151 v=ASTERISK k=152 v=QUOTEDBL k=153 v=LESS k=154 v=PRINTSCREEN k=155 v=INSERT k=156 v=HELP k=157 v=META k=160 v=GREATER k=161 v=BRACELEFT k=162 v=BRACERIGHT k=192 v=BACK_QUOTE k=222 v=QUOTE k=224 v=KP_UP k=225 v=KP_DOWN k=226 v=KP_LEFT k=227 v=KP_RIGHT k=240 v=ALPHANUMERIC k=241 v=KATAKANA k=242 v=HIRAGANA k=243 v=FULL_WIDTH k=244 v=HALF_WIDTH k=245 v=ROMAN_CHARACTERS
What do the numbers in your post represent? For example: VK_ENTER is 10 for mine and 1 for yoursIf you don't understand my response, don't ignore it, ask a question.
- 06-25-2012, 04:29 PM #18
Re: Mapping Key Events
From my understanding it should be scraping all the VK_ commands out of the .class API using getDeclaredFields(). Which means that both of our lists should be the same. The number represents the position it was entered into the Map. If you run this code does it still show our lists differently?
Java Code:import java.awt.event.*; import java.lang.reflect.*; import java.util.*; public class KeyMap{ private static final Map<Integer, String> into = new HashMap<Integer, String>(); public static int i =0; static{ try{ Field[] fields = KeyEvent.class.getDeclaredFields(); for(Field f: fields) { if(f.getName().startsWith("VK_")) { int code = ((Integer)f.get(null)).intValue(); //if (!(f.getName().equals(null))){ into.put(code, f.getName().substring(3)); //} i++; System.out.print(i + " " + f.getName() + " "); System.out.println(into.get(i)); } } } catch(Exception ex){ex.printStackTrace();} } public static void main(String[] args){ } }- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 06-25-2012, 04:49 PM #19
Re: Mapping Key Events
There are no positions in a Map. The value of i represents the order that items were added to the Map but has NOTHING to do with the location of the item in the Map.The number represents the position it was entered into the Map.
Have you read the API doc for the Map class's get() method yet? I still recommend that you read the API doc for it.
Think about what a Map holds and how you access its contents.If you don't understand my response, don't ignore it, ask a question.
- 06-25-2012, 06:28 PM #20
Re: Mapping Key Events
Class HashMap<K,V> where K is Key and V is Value.
V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
So using the get method will return the Value (String) at the Key it was entered which would be code, which is the integer value it was mapped too correct? Or is my understanding of how this works skewed?- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
Similar Threads
-
typed events vs untyped events.
By Drun in forum SWT / JFaceReplies: 0Last Post: 11-23-2009, 12:22 PM -
Servlet mapping
By cozsmin in forum Java ServletReplies: 1Last Post: 04-04-2009, 09:15 AM -
xml mapping error with JPA in RAD, RSA
By ishakteyran in forum Other IDEsReplies: 0Last Post: 02-05-2009, 08:00 PM -
xml mapping error with JPA
By ishakteyran in forum XMLReplies: 0Last Post: 02-05-2009, 07:59 PM -
Help with image mapping
By coco in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 04:06 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks