Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 670
  • Last Modified:

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)
0
Poolya
Asked:
Poolya
  • 6
  • 4
  • 4
1 Solution
 
wide_awakeCommented:
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
 
GurselCommented:

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
 
GurselCommented:
VK_LESS is not supported on Windows.. Because the keyboard does not have VK_LESS key.. Therefore the version of jdk is irrelevant..
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
wide_awakeCommented:
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
 
PoolyaAuthor Commented:
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
 
GurselCommented:
How  can you produce "<" in Cygwin console?...by typing which key?.. I think that is the way of producing < by java.awt.Robot...

0
 
PoolyaAuthor Commented:
>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
 
wide_awakeCommented:
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
 
PoolyaAuthor Commented:
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
 
wide_awakeCommented:
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
 
wide_awakeCommented:
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
 
wide_awakeCommented:
Oddly, it works just fine on my linux box running the same version of the j2sdk.
0
 
PoolyaAuthor Commented:
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
 
GurselCommented:
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!

  • 6
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now