• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

keypress + Robot + Method

 
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to use reflection to get the proper "key code" to use:

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changed for you!
,
cc11rocks aka John Price
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic