Link to home
Start Free TrialLog in
Avatar of Poolya
Poolya

asked on

Simulating non-alphanumeric keystrokes using java.awt.Robot

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)
Avatar of wide_awake
wide_awake

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.
ASKER CERTIFIED SOLUTION
Avatar of Gursel
Gursel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
VK_LESS is not supported on Windows.. Because the keyboard does not have VK_LESS key.. Therefore the version of jdk is irrelevant..
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.

Avatar of Poolya

ASKER

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)

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

Avatar of Poolya

ASKER

>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.


Is this a compile-time or run-time error?

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

Avatar of Poolya

ASKER

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.
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.  :)
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...
Oddly, it works just fine on my linux box running the same version of the j2sdk.
Avatar of Poolya

ASKER

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.
thanks for the point.. :)