Emulating a keyboard in java

L

lilanidhaval

To AnyOneThatCanHelp,

I want to know how is it possible to emulate a keyboard in java. The
purpose of this is so that the emulated keyboard can interact with
other programs as if it were a normal keyboard and also the emaulated
keyboard should be able to be automated.
 
T

Thomas Weidenfeller

I want to know how is it possible to emulate a keyboard in java. The
purpose of this is so that the emulated keyboard can interact with
other programs as if it were a normal keyboard and also the emaulated
keyboard should be able to be automated.

For any practical purpose this is not feasible in Java.

/Thomas
 
J

jan V

For any practical purpose this is not feasible in Java.

What about Robot? Is that restricted to generating events within the JVM
universe?
 
T

Thomas Weidenfeller

jan said:
What about Robot? Is that restricted to generating events within the JVM
universe?

You can't 'locate' another application window with Robot. Because of
this you have severe difficulties to move the input focus to that other
application - you can't find the window, so where do you want to move
the mouse to and click to move the focus to that other application?

/Thomas
 
J

jan V

What about Robot? Is that restricted to generating events within the JVM
You can't 'locate' another application window with Robot. Because of
this you have severe difficulties to move the input focus to that other
application - you can't find the window, so where do you want to move
the mouse to and click to move the focus to that other application?

hmmm yes, that sucks, doesn't it? It means nobody can write a pure Java GUI
testing tool which can test programs written in any language. Shame...
 
R

Roedy Green

I want to know how is it possible to emulate a keyboard in java. The
purpose of this is so that the emulated keyboard can interact with
other programs as if it were a normal keyboard and also the emaulated
keyboard should be able to be automated.

What a real keyboard does is create Keystroke events. You need
something that does that. The hairy chested way would be to study up
how keystroke events work and synthesize them, see
http://mindprod.com/jgloss/event11.html or the clever way would be to
study up on the java.awt.Robot class that does it for you and also
lets you move the mouse. see http://mindprod.com/jgloss/robot.html
 
R

Roedy Green

If the other programs are not written in java you need a platform
specific solution, doing something like recorder macros.

That would require JNI to hook into that platform specific keystroke
generator. It seems you will run afoul of security. It is not
considered polite for programs running in different address spaces to
interfere with one another uninvited.

In windows, I believe what you want to do is possible IF you spawn the
controlled program. Otherwise, how do you get a handle on it?

IF you need such a Windows Robot class, I could write it for you for a
fee.
 
J

jan V

No. java.awt.Robot works very well!
that's amazing. The people who are such old maids designing the
Applet sandbox left that gaping security hole.

Roedy, you should know better. How long ago was it that Java was being
worked on by a team of say a dozen people (the same people that came up with
the SecurityManager mechanism and that applet sandbox) ? Ten years, at
least... since then, Sun must have thrown between 50 and 100 software
engineers at the whole JDK. The person who designed the Robot class almost
definitely didn't have squat to do with the whole security framework and
APIs.

This is what I've deplored more and more as the years go by: an increasing
divergence of quality standards within the JDK. Some facets are designed
really well, others suck big time. Some aspects follow long-respected Java
coding patterns, while others totally flout them. Some aspects have great
documentation, others have pathetic documentation.

I've said it before and I'll say it again: Sun should hang their heads in
shame for not imposing a far more uniform quality throughout the APIs it
designs and implements.
 
T

Thomas Weidenfeller

jan said:
I've said it before and I'll say it again: Sun should hang their heads in
shame for not imposing a far more uniform quality throughout the APIs it
designs and implements.

Sun is fare more interested in adding new features, with new and
improved bugs, than finally getting around and fixing the existing
stinkers in Java

/Thomas
 
C

Claus Petersen

Hi NG,

I'm also trying to 'throw' some keys down in the keyboard buffer.

Here's my problem:

I use the programe WinLirc, that runs as a server, winLirc is an opensource
program that recieves IR codes from a remote reciever connected to the
com(rs232) port.

So what i would like to do is get the keys from their server(they have a
document describing how)
Put them down in the keyboard buffer so that all programs can be controlled
by a remote.

The smart way would ofcause be to write a olugin to the programe, but its
not possible.


Is there anybody here in the NG that has any suggestions???

Can it be done in Java, or should i do it in C++???


Claus
 
B

Bill Tschumy

that's amazing. The people who are such old maids designing the
Applet sandbox left that gaping security hole.

Are you sure the use of Robot is not restricted by the SecurityManager in
effect? My guess is that applets cannot use Robot but I could be wrong.
 
R

Roedy Green

I'm also trying to 'throw' some keys down in the keyboard buffer.

Here's my problem:

I use the programe WinLirc, that runs as a server, winLirc is an opensource
program that recieves IR codes from a remote reciever connected to the
com(rs232) port.

So what i would like to do is get the keys from their server(they have a
document describing how)
Put them down in the keyboard buffer so that all programs can be controlled
by a remote.

The smart way would ofcause be to write a olugin to the programe, but its
not possible.


Is there anybody here in the NG that has any suggestions???

Can it be done in Java, or should i do it in C++???

Are these correct assumptions?

1. you are running Windows

2. you want to remotely control arbitrary Windows application, typing
blind into them remotely (or programmatically generating keystrokes
remotely for them).

the tools you have are:

The Robot class. See http://mindprod.com/robot.html Your problem here
is how do you blindly find the program you want to key into.

In C++ the ability to spawn a program and send it keystrokes, access
it via JNI.

There are tools better designed for remote control such as PCAnywhere
see http://mindprod.com/jgloss/remotecontrol.html
 
C

Claus Petersen

Roedy Green said:
Are these correct assumptions?

1. you are running Windows

2. you want to remotely control arbitrary Windows application, typing
blind into them remotely (or programmatically generating keystrokes
remotely for them).

the tools you have are:

The Robot class. See http://mindprod.com/robot.html Your problem here
is how do you blindly find the program you want to key into.

In C++ the ability to spawn a program and send it keystrokes, access
it via JNI.

There are tools better designed for remote control such as PCAnywhere
see http://mindprod.com/jgloss/remotecontrol.html

Hi there,

Well it's not because i want to remotly control different windows prorgrams,
but im working on a small sparetime project where i would like to control
fx. a Java Applet with a joystick or a remote control for my tv, so the key
should be send to the window in focus.
 
R

Roedy Green

Well it's not because i want to remotly control different windows prorgrams,
but im working on a small sparetime project where i would like to control
fx. a Java Applet with a joystick or a remote control for my tv, so the key
should be send to the window in focus.

So the code receiving the keystrokes is your code and it is in Java.
That hints that Robot or something even more direct -- calling
publicly exposed methods is what you want.

With Robot you are blind. You have to keep track of your tabs and
enters to know which field must have focus. When you call
Listeners/EventHandlers, you have the naming system to help you target
your keystrokes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top