Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35
Like Tree1Likes

Thread: Mapping Key Events

  1. #21
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Mapping Key Events

    The int value used in the get() method is autoboxed to an Integer object and that is used as the key to retrieve a value from the Map.
    The long list I posted#17 has a k= label for the key used and v= for the value returned for that key. I skipped printing any null values. I did the printout in a separate loop from the loop that loaded the Map.
    If you don't understand my response, don't ignore it, ask a question.

  2. #22
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Mapping Key Events

    Ok so my key is Integer Objects, but shouldn't our lists be the same if I excluded all the null values in printing too? It needs to be a universal map on any computer for my program to work. So your A needs to be the same as my A so when I call A from the Map it doesn't give me something like X instead.

    I need to go through a string and find the VK_command for each letter so the Virtual Keyboard from the Robot class can input it into a text box.

    Am I not understanding the way Maps work still? You have the Key and the Value, so when I use .get on the Key it will return the Value. So if I did .get((Integer)65); it would return "A" 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.

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Mapping Key Events

    Do you have new code that gets all the values in one loop and prints the contents of the Map in a second loop?

    Post your new code that shows how you are creating the list. Your old code was asking for key/value pairs before they were in the Map.
    If you don't understand my response, don't ignore it, ask a question.

  4. #24
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Mapping Key Events

    Yes, I made it pull from the list after assigning everything. I also posted the results.

    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();
    					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();}
    		for (int i = 0; i<into.size(); i++){
    			if (into.get(i) != null){
    				System.out.println(i + " " + into.get((Integer)i));
    			}
    		}
    	}
    }
    Java Code:
    0 UNDEFINED
    3 CANCEL
    8 BACK_SPACE
    9 TAB
    10 ENTER
    12 CLEAR
    16 SHIFT
    17 CONTROL
    18 ALT
    19 PAUSE
    20 CAPS_LOCK
    21 KANA
    24 FINAL
    25 KANJI
    27 ESCAPE
    28 CONVERT
    29 NONCONVERT
    30 ACCEPT
    31 MODECHANGE
    32 SPACE
    33 PAGE_UP
    34 PAGE_DOWN
    35 END
    36 HOME
    37 LEFT
    38 UP
    39 RIGHT
    40 DOWN
    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
    59 SEMICOLON
    61 EQUALS
    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
    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
    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
    150 AMPERSAND
    151 ASTERISK
    152 QUOTEDBL
    153 LESS
    154 PRINTSCREEN
    155 INSERT
    156 HELP
    157 META
    160 GREATER
    161 BRACELEFT
    162 BRACERIGHT
    • 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.

  5. #25
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Mapping Key Events

    Since there are holes in the data, the key values will exceed the number of items in the Map. The size() method will not be large enough. If you look at the definitions for the VK_ variables, some have much larger values than 1000.

    Look at the Map interface's methods. There are other methods that will get you a complete Set of the keys to all the items in a Map.
    If you don't understand my response, don't ignore it, ask a question.

  6. #26
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Mapping Key Events

    Well it didn't cause an error when I ran it. Are you talking about this method?
    Java Code:
    Set<K> 	keySet()
              Returns a Set view of the keys contained in this map.
    • 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.

  7. #27
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Mapping Key Events

    You can use that method to get a Set of the keys being used in the Map.
    If you don't understand my response, don't ignore it, ask a question.

  8. #28
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Mapping Key Events

    Then after I got the set I would figure out the size of the set for my loop? Is that what you're saying?
    • 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.

  9. #29
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Mapping Key Events

    Look at the Set class. It has methods for getting its contents. Then given a key from the Set you could get its value from the Map
    If you don't understand my response, don't ignore it, ask a question.

  10. #30
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Mapping Key Events

    I could get the total size of the Map by using toArray and then using .length() on the new Array Object. Then I wouldn't have an issue with it possibly going out of bounds?

    Then to see if the character I was looking for in my map I would use .contains() and if it returned true then I would use .get() right? I'm not sure what you're trying to have me figure out here. You told me that using .size() on my map object could cause out of bound errors and that there is a better way to do it, and now you're asking me how I would get the info from the map?
    • 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.

  11. #31
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Mapping Key Events

    Did you try getting the Set of all keys and using the keys from that Set to get all the values?

    What are you trying to do?

    You told me that using .size() on my map object could cause out of bound errors
    That's backwards. For example if the Map had keys: 1, 100, 10000 (a size of 3) and you tried to get the values for keys 0,1,2 you would only get 1 value and 2 null for 0 and 2
    If you don't understand my response, don't ignore it, ask a question.

  12. #32
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Mapping Key Events

    Oh I get it now, because it loops through every field in class ignoring the ones who don't have VK_ in front of them. Hmm, so what would you recommend to solve this issue?

    I haven't tried getting the set and using the keys from that set to get all of the values yet. I will have to try that now that I've made my map.
    • 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.

  13. #33
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Mapping Key Events

    what would you recommend to solve this issue?
    Can you describe what you are trying to do?
    If you don't understand my response, don't ignore it, ask a question.

  14. #34
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Mapping Key Events

    The issue of their keys not being sequential. Like 1, 100, 10000 and not 1,2,3. I figure that it will be much easier to manipulate if they are sequential.
    • 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.

  15. #35
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Mapping Key Events

    There must be a way to sort the keys after you get them into a Set.
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 12:22 PM
  2. Servlet mapping
    By cozsmin in forum Java Servlet
    Replies: 1
    Last Post: 04-04-2009, 09:15 AM
  3. xml mapping error with JPA in RAD, RSA
    By ishakteyran in forum Other IDEs
    Replies: 0
    Last Post: 02-05-2009, 08:00 PM
  4. xml mapping error with JPA
    By ishakteyran in forum XML
    Replies: 0
    Last Post: 02-05-2009, 07:59 PM
  5. Help with image mapping
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •