Tutorial :key events



Question:

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); please tell me....


Solution:1

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.


Solution:2

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.


Solution:3

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?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »