This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes String to Robot? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "String to Robot?" Watch "String to Robot?" New topic
Author

String to Robot?

Bret Waldow
Ranch Hand

Joined: Aug 04, 2000
Posts: 58
I am writing a handwriting input tool for my linux tablet computer.

I've figured out a gui approach that doesn't steal the focus from other windows, so the user can write on the gui, and the text generated will go to the other (not just java - any window that has the system focus) window.

Now I'm working out how to take the recognized text and generate the keycodes that Robot seems to want:

public void keyPress(int keycode)
public void keyRelease(int keycode)

I am starting with, say - String word = "typographic";

How can I turn those characters into the "KeyEvent.VK_T" inputs Robot seems to want?

Thanks in advance,
Bret
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Bret Waldow:
How can I turn those characters into the "KeyEvent.VK_T" inputs Robot seems to want?


VK_A through VK_Z have the same values as chars 'A' through 'Z' (and are even documented as such) so those are easy. Other chars will be tougher, I would think. I guess you will also have to simulate the shift key and such.


bitguru blog
Bret Waldow
Ranch Hand

Joined: Aug 04, 2000
Posts: 58
Thanks. I have looked at the javadocs.

I'm curious there is no constructor - what about other languages, unicode, etc.?

Is Robot intended to be so limited in what it can test (rhetorical question)? Not only do those constants limit it to the Roman alphabet, it also requires canned, pre-selected keys, rather than accepting general text input. Odd.

I'll ask Sun.

If anyone knows of another way besides Robot to output keystrokes to a system, I'd certainly like to hear of that as well. As I'm only concerned about X11 windowing systems, I can do things with XTest, XTRAP, and JNI, but Robot would be such a clean implementation if I can get it to do what I want.

I only need Western European characters, but I'm intending to GPL my app, so I'd like a technology that can satisfy the broader need.

cheers,
Bret
Bret Waldow
Ranch Hand

Joined: Aug 04, 2000
Posts: 58
Nope. There's a constructor. An episode of User Failure (me).

I see these two arguments (among others):
int keyCode, char keyChar

I guess I'll have to read up on some apparent keyboard mapping scheme Java either adds or at least accesses on the target platform. If anyone has a pointer to an explanation they found that they think is particularly clear, please tell me?

Thanks,
Bret
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Bret Waldow:
what about other languages, unicode, etc.?

Is Robot intended to be so limited in what it can test (rhetorical question)? Not only do those constants limit it to the Roman alphabet, it also requires canned, pre-selected keys, rather than accepting general text input. Odd.


I've never used Robot, but it is intended to mimic the user input devices available to the user. That is, a mouse with one or more buttons and a wheel, and a keyboard with 100 or so buttons. The idea is that each key is modelled, not each char. That's why there are different keycode constants for VK_4 and VK_NUMPAD4. To model a capital T, you would want to simulate VK_SHIFT down, VK_T down, VK_T up, VK_SHIFT up.

(Now I can't tell you why there is a separate keycode constant for VK_DOLLAR, which on my keyboard is the same key as VK_4. Perhaps there are keyboards somewhere in the world where $ gets its own key? It seems unlikely.)

For unicode chars and such I guess you would have to do something similar to the capital-T thing, except I'm not sure of the details of unicode input. Perhaps it would be simpler if you modelled a paste from the clipboard instead? You can put arbitrary unicode strings on the clipboard.
Bret Waldow
Ranch Hand

Joined: Aug 04, 2000
Posts: 58
The idea is that each key is modelled, not each char.


Yes, that fits my experience. I rigged up a simple test tool in a gui to try it:


This does not work - that's what led to my posting. This does:


I haven't found a reason for the discrepancy, but I think it's moot now.

Unfortunately, the keyPress/keyRelease pair introduces a problem - the system may not pay attention to Java between the first and the second, so several keypresses may register (autorepeat). I haven't thought of a way to prevent that possibility, although I've been reading of a Real-Time version of Java that's being requested/worked on.

Synchronize would only have effect on the JVM, not the underlying system's priorities. I could 'nice' the JVM process to gain near total priority, but that's gambling, even if it does work on this machine, most of the time, etc.

I'm thinking of trying to use a Java X11 interface library, or a JNI setup to put a key directly into the system's input queue now.

If you can think of a way to make Robot's input definitive, that would be easiest (depending on what it would take to make Robot's input definitive, of course).

Cheers,
Bret
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You may want to take a look at how this is implemented in JFCUnit.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String to Robot?
 
Similar Threads
retaining window focus
Canvas, keyPressed() and TextBox
Focus issues among JTextField and Dialogs
keypress + Robot + Method
ENTER KEY with JComboBox