?
Solved

Simulating non-alphanumeric keystrokes using java.awt.Robot

Posted on 2003-03-24
14
Medium Priority
?
639 Views
Last Modified: 2007-12-19
Hello,

I'm trying to write a java class capable of sending characters to a
console window. So far I've been able to get it to send any
alphanumeric key, or alphanumeric key with SHIFT, ALT and/or CTRL. The
problem is that I cannot currently manage to send any non-alphanumeric
key. The specific instance that I have been working on is the '<' key,
which I had assumed to be represented by 'VK_LESS'. However this
merely throws an IllegalArgumentException, complaining that the
keycode is invalid. Any help would be appreciated - full text of the
exception and the source of the test program are included below.

import java.awt.*;
import java.awt.event.*;

public class KeyPresser
{
 public static void main(String[] args) throws AWTException
 {
  Robot r = new Robot();
   r.keyPress(KeyEvent.VK_LESS);

 }
}

java.lang.IllegalArgumentException: Invalid key code
        at sun.awt.windows.WRobotPeer.keyPress(Native Method)
        at java.awt.Robot.keyPress(Robot.java:222)
        at KeyPresser.main(KeyPresser.java:9)
0
Comment
Question by:Poolya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
14 Comments
 
LVL 3

Expert Comment

by:wide_awake
ID: 8197740
what version is your JVM? VK_LESS has only been supported since 1.2

You could try:

r.keyPress(KeyEvent.VK_SHIFT);
r.keyPress(KeyEvent.VK_COMMA);
r.keyRelease(KeyEvent.VK_COMMA);
r.keyRelease(KeyEvent.VK_SHIFT);

-Mark.
0
 

Accepted Solution

by:
Gursel earned 150 total points
ID: 8197892

Here is the solution.. Well the problem, in US keyboard there is no < key.. So to generate <, first press shift then comma..


import java.awt.*;
import java.awt.event.*;

public class KeyPresser
{
public static void main(String[] args) throws AWTException
{
 Robot r = new Robot();
  r.keyPress(KeyEvent.VK_SHIFT);
  r.keyPress(KeyEvent.VK_COMMA);
    r.keyRealease(KeyEvent.VK_SHIFT);
}
}


Here is another  solution..



import java.awt.*;
import java.awt.event.*;

public class KeyPresser
{
public static void main(String[] args) throws AWTException
{
 Robot r = new Robot();
  r.keyPress(KeyEvent.VK_ALT);
  r.keyPress(KeyEvent.VK_NUMPAD6);
  r.keyPress(KeyEvent.VK_NUMPAD0);
    r.keyRealease(KeyEvent.VK_ALT);
}
}


For more information you can check this address
http://developer.java.sun.com/developer/bugParade/bugs/4615539.html
0
 

Expert Comment

by:Gursel
ID: 8198118
VK_LESS is not supported on Windows.. Because the keyboard does not have VK_LESS key.. Therefore the version of jdk is irrelevant..
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 3

Expert Comment

by:wide_awake
ID: 8198350
JDK version is relevant, since Poolya didn't specify the OS.

Also, you should use keyRelease for each keyPress, otherwise you might have issues with missing KEY_TYPED events.

0
 

Author Comment

by:Poolya
ID: 8198522
Thanks for the responses so far guys, though I fear the problem is not solved quite yet. Firstly - as wide_awake enquired (and as I should have said initially) the JDK version is 1.4.0, and the environment is Cygwin under Win XP (using a Bash 2.05b shell) .

Thank you also to Gursel for the two examples - the latter (ALT-60 method) will produce a < symbol in Windows text editors or DOS command shells - but seems to produce nothing on a Cygwin console.

The former (SHIFT-COMMA method) example has largely the same trouble as the first - an IllegalArgumentException: Invalid Keycode, and the same applies to a unshifted VK_COMMA.

As before and always - all assistance is very much appreciated.

java.lang.IllegalArgumentException: Invalid key code
        at sun.awt.windows.WRobotPeer.keyPress(Native Method)
        at java.awt.Robot.keyPress(Robot.java:222)
        at KeyPresser.main(KeyPresser.java:12)

0
 

Expert Comment

by:Gursel
ID: 8201495
How  can you produce "<" in Cygwin console?...by typing which key?.. I think that is the way of producing < by java.awt.Robot...

0
 

Author Comment

by:Poolya
ID: 8209679
>How  can you produce "<" in Cygwin console?...by typing
>which key?.. I think that is the way of producing < by
>java.awt.Robot...

Sadly the key for "<" is Shift-Comma (which Java doesn't seem to accept) and the ALT-codes have seemingly no effect.


0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8211679
Is this a compile-time or run-time error?

Have you tried compiling/running it using the CMD window in XP instead of bash?

0
 

Author Comment

by:Poolya
ID: 8213597
The error is RunTime and occurs only when the non-alphanumeric keystroke is supposed to occur (either the < or the , etc).

The program will produce a < character using the ALT-60 method described above in the XP CMD window, but encounters the same error as Cygwin with the SHIFT-COMMA or plain '<' method.
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8213773
Don't have jvm installed here at work, I'll have to look into it when I get home (probably a few hours).

To entertain you in the meantime, imagine if the hyphen ('-') in this website's name was one character earlier.  :)
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8214655
okay, I'm confused now.

I tried running the sample code on my comp, and it gives the same error you get.

So I made a keyListener to see what the values were for the "real" events when you type "<", and even using those values doesn't work.

Here's the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class k implements KeyListener
{
   public static void main(String[] args)
   {
      JFrame f = new JFrame("Hello");
      JTextField a = new JTextField();
      f.getContentPane().add(a);
      a.addKeyListener(new k());
      f.setSize(100,20);
      f.setVisible(true);
   }

   public void keyPressed(KeyEvent e)
   {
      System.out.println("Pressed: " + e.getKeyCode());
   }
   public void keyReleased(KeyEvent e)
   {
      System.out.println("Released: " + e.getKeyCode());
   }
   public void keyTyped(KeyEvent e)
   {
      System.out.println("Typed: " + e.getKeyCode());
   }
}

It says the "Comma" key is keyCode 44 (same as the documentation for VK_COMMA), but it still doesn't work when you try it with keyPress(44).  very strange...
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8214693
Oddly, it works just fine on my linux box running the same version of the j2sdk.
0
 

Author Comment

by:Poolya
ID: 8214859
Ok - going to go for a workaround and implement it using JNI rather than the Robot class directly - seems safer and is more likely to come up with reproducable results on multiple platforms as is the ultimate plan.

I'm going to close the question now. I'll give the points to Gursel as he did provide the answer to the question as stated (ALT-60 method), but I'd also like to personally thank wide_awake for his efforts too. It was all appreciated, even if there does not seem to be a straightforward answer.
0
 

Expert Comment

by:Gursel
ID: 8216206
thanks for the point.. :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month12 days, 8 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question