i m using the keyboard event on Robot Objects....
but each time i have to specify the keys individually....like
Robot r=new Robot();
r.KeyPress(KeyEvent.VK_A);
r.KeyPress(KeyEvent.VK_B);
r.KeyPress(KeyEvent.VK_C);
r.KeyPress(KeyEvent.VK_D);
is there any technique to get/recognize eachand every keys....not by specifying them individually....?
i m recieving the keycode from server side in keyCode variable....
so can i use this variable directly inplace of "KeyEvent.VK_D" like r.keyPress(keyCode);
The Key identifier is just an Int value. When the server value matches the java value, than you an directly put the value in. If not you have to create a Map where the server value references to the java key value.
I had the same issue during converstion between C++ Qt key events and Java Key events. The value also does not match. I had to create a mapping for this.
Take a look at the class KeyEvent. Every Key is listed their with a int value. You have to check if the value you get from the server matches with this int value. When the server value is not matching you have to create a Map. The map key is the server value and the map value is the corresponding KeyEvent valuke for the pressed key.
When the values matching, you dont have to create a map. You directly can use the server value for the Robot command.
Um... yes? The constants in KeyEvent.VK_D are merely there for your convenience. There is absolutely nothing wrong with using numerical values from somewhere else, as long as the same values are used for the same keys.
If I understand your question correctly, you're trying to get this action to take place whenever any key is pushed. Although I doubt I'm reading your question correctly, the solution to this would be to use KeyEvent.KEY_PRESSED and if you want an action or to set a variable when a key is released it would be KeyEvent.KEY_RELEASED. Although, I am a bit confused. Do you really want the same action to occur when every key is pressed or do you want a unique event for a set of keys?
Related
I've been trying to learn keybinds by rewriting problems from my book that I've previously solved using KeyListener. The problem that I'm struggling to solve using keybinds requires me to record a message that's been typed and to display it on the panel.
The way it was solved using KeyListener is simply by recording characters with unicodes using the keyTyped() method and reading modifier/non-Unicode keys with keyPressed. If KeyEvent.VK_ENTER matches the keycode from the keyevent, then it displays the string on the panel.
~~~~~~~~
I thought that it can be solved in a similar way with KeyBinds. It says in the KeyEvent docs that KeyEvent.KEY_TYPED is fired every time a character is entered. I assumed that it meant every character with a corresponding Unicode being typed like how it works in KeyListener.
Later on, I realized that I have no idea how to retrieve the character since the Oracle tutorial on KeyBinds says that the KeyEvent is consumed when actionPerformed() is invoked.
This is the code that I THOUGHT would enable me to record typed keys to a StringBuilder using KeyBindings:
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.KEY_TYPED, 0), "recordTypedKey");
getActionMap().put("recordTypedKey", addCharToString);
Is there a way to obtain the characters that would invoke KeyListener's keyTyped() method besides adding a key to every one of them and using a separate Action event to record them?
Is there a way to obtain the characters that would invoke KeyListener's keyTyped() method besides adding a key to every one of them and using a separate Action event to record them?
I do not believe there is a global KeyStroke you can pass to the InputMap that will work similar to a KeyListener, as KeyBindings work on an individual key basis. You can however create a single Action and bind keys to it by looping over the char values you wish to process - in the ActionListener implementation you can obtain the value of the key via getActionCommand. For example to deal with a-z:
AbstractAction action = new AbstractAction(){
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
};
//loop over the ascii char values
for ( char a = 'A'; a <= 'Z'; a++ ){
panel.getInputMap().put(KeyStroke.getKeyStroke(Character.toString(a)), "recordTypedKey");
}
panel.getActionMap().put("recordTypedKey", action);
You can add modifiers if needed...for example to deal with the shift key (eg upper case),
panel.getInputMap().put(KeyStroke.getKeyStroke("shift " + Character.toString(a)), "recordTypedKey");
I want to create an array of all keys on the keyboard.
How many KeyCode are they ?
KeyEvent.getKeyCode() return ASCII or Virtual Key?
From the docs:
The key being pressed or released is indicated by the getKeyCode and getExtendedKeyCode methods, which return a virtual key code.
You can count the number of keycodes yourself from the doc of java.lang.Enum.KeyCode:
If you're doing this in order to detect the keyboard layout, forget the Keycodes. Instead do:
InputContext context = InputContext.getInstance();
System.out.println(context.getLocale().toString());
I have to read a file and store the values and then later do a lookup.
For e.g., the file will look as follows:
Gryffindor = 5
Gryffindor.Name.Harry = 10
Gryffindor.Name.Harry.Cloak.Black = 15
and so on...
I need to store these (I was thinking of a map). Later, I need to process every character and lookup this map to assign them points. Suppose I encounter Harry, I know that he's from Gryffindor and he's wearing a blue cloak. I will have to lookup this map (or whatever object I use) as
Gryffindor.Name.Harry.Cloak.Blue
which should return me nothing. I then need to fall back to just the name and lookup
Gryffindor.Name.Harry
that should return me a 10.
Similarly, if I lookup for Ron, (suppose he's wearing black),
Gryffindor.Name.Ron.Cloak.Black
should return nothing, fall back to
Gryffindor.Name.Ron
again nothing, fall back to
Gryffindor
which should return 5.
What will be an elegant way to store and read this data? I was thinking of using a map for storing the key value pairs and then a switch case to read them back. How would you do it?
Java has a built-in Properties class that implements Map and can read and write the data format you describe (see that class's load() and store() methods).
There's nothing in there to implement your "fall back to a higher-level key" feature, so you'll need to write a method that looks in the Properties instance for data under the desired key, and keeps trying successively shorter versions of the same key if it finds nothing.
I am using hazelcast -2.5 in a cluster. I have a map (key: String, value: ArrayList of user defined objects). I am able to put/remove fine in most places but in one specific part of my code, the put operation fails silently (the key string used for the put operation is unique and the ArrayList is not empty either). No exceptions are thrown. In case there was a lock involved, I even tried tryPut and that call gave me a true return value. Right after the put operation, I tried printing out the keySet for the map but cannot see the key I just inserted - the size of the map has not changed either (yet the tryPut gave me a true return value and I'm reasonably sure the string I am using for the key is unique - and I am hoping the binary form for the key is unique as well). If the binary form for my key is not unique, I am assuming that the tryPut should return a false return value or at least replace the previously added key/value with the new key/value pair (unless I misinterpreted the docs).
boolean putVal = testMap.tryPut(this.testObj.UUID, testEntity, timeout, TimeUnit.MILLISECONDS); //timeout is 2000L or 2 seconds in this case
Any thoughts on troubleshooting this or figuring out if the binary form for my key is causing the issue will be appreciated.
Thanks
Try to do a get. And see if there is any value associated with that key. If not, the put should be successful.
I'm trying to type some text into a text field using the Robot class in Java.
The problem I have is, that I can not discover the integer value of the backtick key (to the left of 1 on the keyboard).
I have no idea which one of the VK_ constants in the KeyEvent class it is.
At first I assumed it was 96, but pressing it using the robot gives me 0 (maybe 96 is the numpad zero).
What is the integer keycode for backtick?
VK_BACK_QUOTE is the code you want. It has integer value 192.
If microsoft layout editor is right, VK_OEM_3 . Does it make sense?