my dog learned polymorphism
The moose likes Java in General and the fly likes keypress + Robot + Method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "keypress + Robot + Method" Watch "keypress + Robot + Method" New topic
Author

keypress + Robot + Method

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

I am using awt.Robot. I am trying to create a method for keyPresses, but can't figure it out. I've spent 2 hours trying various ways to get this to work. Here is the skeleton:
call:


I've tried various ways of making this work. Tried to convert the char to keyevent and other ways. Is this even possible?
Thanks,
John Price aka cc11rocks


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 1791
    
    2
You need to use reflection to get the proper "key code" to use:

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

I would have never been able to do that by myself. Thank you! Here is what I came up with:


Thanks,
John Price aka cc11rocks
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 1791
    
    2
Here is what I came up with


Please learn by example. I didn't use any "magic numbers" in my example code, so why did you?

I used KeyEvent.VK_SHIFT. Why did you use "16". People don't know what that means.

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

Changed for you!
,
cc11rocks aka John Price
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Note that VK_A == 'A', VK_B == 'B', VK_1 == '1', etc. This is documented in the KeyEvent Javadoc:
VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A)
VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39)
Therefore, a simple Character.toUpperCase() should be enough. You don't need to use reflection for this:


Also, if you use reflection, note that a) you can get the KeyEvent Class object by using KeyEvent.class, and b) the fields are static. Therefore, you don't need to create an instance:


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 1791
    
    2
you can get the KeyEvent Class object by using KeyEvent.class,


Thanks, I knew there was a way, I just escaped me at the moment.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: keypress + Robot + Method
 
Similar Threads
How do I take a variable and print it to a cursor using SWT?
How to convert set to char[]?
Robot class and loops
� displayed as ?
assembling a complex structure