Inject a keystroke in java - java

I'm looking for a way to inject a keystroke into the OS keyboard input buffer,
like when you click a button the program inserts one (or more) keyboard strokes. I wanted to do this in java because I want to run this in (win,linux and osx). I guess that I'll have to make use of the JNI, do anyone have some ideas?
Thanks all stackoverflowers ;)

My guess is that the java.awt.Robot class will do this for you:
new Robot().keyPress(...);
http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#keyPress(int)
java.awt.Robot "is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed."

Check java Robot . I believe this is what you are looking for.
Also check this out. Example

Related

Pressing key at any point to make an action

I'm programming on java, so I have to make a game-like program. The game should be able to "pause" at any point. Can you press a key at any point so a message is displayed saying "game is paused"? Then when you press that key again it will say "game resumed"?
Thanks
It looks like you need to be able to read input directly from the keyboard into your game? Is your game built into a GUI or in the console?
This can be done by using the KeyListener interface Java provides.
https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html
I'll assume your using swing so this tutorial should be pretty useful.
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
Sorry I can't go into more detail, more information about what you are trying to do in the comments would help me help you.
Edit: You mentioned you're using the command line. To get user input you can use java.util.Scanner
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Make a boolean, isPaused. If space is pressed, switch isPaused from true to false or vice versa. If isPaused is true, run the game, if not, run a pause screen instead.

how to set a specific position though command line?

String cmd = "start calc.exe";
Process process = Runtime.getRuntime().exec(codeString);
I can call calculator out, but I wish to specify a accurate position like (200,300).
how can I rewrite my cmd String?
I know that java.awt.window can set a window or frame to the specific position.
Is there any method I can use to fill frame or window with my process?
There is no clean pure java solution because JDK does not provide API that can control non-java windows. So, if you want to can use JNI/JNA.
But I can suggest you a patch that will typically work.
Windows OS allows moving windows using keyboard. Try the following manually:
Win+R
type calc and press enter
press alt+space
press M
press enter
now use arrows to move the window. Press ESC to exit this mode.
All these actions can be implemented using java.awt.Robot.
So, you can run calculator and then immediately move its window where you want.
Well, this is not clear solution, but very simple one.
Expected Problems:
Alt+space is mapped to other, custom application
Other window that started together with calc overlaps it.
User will see that window is created somewhere and then quickly moved.
So, everything depends on how important all this for you. This solution is good as an exercise or demo but bad for real commercial application.

Keyboard/Mouse Robot not working when focused on a certain application

So I am trying to use a robot (either keypress or mouse) to simulate some tests on an application. However, this program isn't allowing me to do so and I'm pretty sure it is a security feature. The application I am using is TEMS Investigation and I am trying to simulate a record and stop recording for some automated tests I have. The program I am using to automate these tests can not access TEMS so there is no way for me to do this without a Robot. So my questions are:
1) How can I override whatever it is that is preventing me from using my robot.
2) Would this be considered illegal if I did? I don't need to break any laws...I just want to get my testing done! lol
I can't even runs something as simple as:
Robot r = new Robot();
r.keyPress(KeyEvent.VK_F2);
r.keyRelease(KeyEvent.VK_F2);
It will run, as I have downloaded some software to tell me what keys I am pressing....just wont be recognized while the TEMS application has focus.
Update: I found out using Notepad I can't use any keys (like "ALT +
F") to perform any menu options. It doesn't even pull up the "File"
menu. I am able to simulate typing in text, just not the context menu.
What was really weird is that I can't even use mouseMove() while TEMS
has the focus
It sounds like your runtime has no knowledge of the TEMS app. If you start it in your java code it should work. For example the following:
Runtime.getRuntime().exec("notepad");
robot.keyPress(KeyEvent.VK_J);
Will bring up notepad and type in the letter J. Try exec-ing TEMS and then send the key press.
And no, it's not illegal.
UPDATE:
To open the file menu in notepad, you would do the following:
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F);
The two keys must be pressed together before a robot.keyRelease.

Disabling keyboard/mouse in Java

I am working on a program, which allows the user to lock the computer, so no one else can use it. Is there anyway, I can disable the mouse, and specific keys on the keyboard? Thanks.
No but you can create have your program locking the screen and then using a MouseMovementListener so each time the mouse moves, you return it programatically to some point in the screen ( it would look like it doesn't move anymore )
over Linux you can use xinput enable id
you can get the id throw xinput without parameters.
Process p;
p = Runtime.getRuntime().exec("xinput disable 12");
I don't know of a portable way and I'm pretty sure Java does not supply anything like that in fact. However on Windows this can be done with BlockInput. But if you were going to code JNI/JNA you might as well use LockWorkStation.
I think you could do it if you are implementing the MouseListener interface. In the MouseClicked method you could check a boolean before actually performing any action. When you want to disable or enable the action change the state of that boolean.
If you want to lock the screen then you can use java Robot class. And if you want to block the keyboard and mouse events when Windows is locked then there is no need. Since locking the screen results in disabling all the inputs.

How to insert text to another program's textbox using java

How can I insert some text into a textbox of another program using java. for example, yahoo messenger chatbox.
I'm not trying to make a yahoo bot, It's just an example of what I'm looking for.
Thank you!
As Jonathon noted in the comment, you can try using java.awt.Robot. But you'd need to know the exact location of target text field, and have it visible on the screen.
You can have something like:
Robot robot = new Robot();
robot.mouseMove(xCoord, yCoord);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.keyPress(50);
robot.keyPress(51);
Apart from that, you'd need the application to provide some native API to interact with its form, and use it via JNI.
You should use JNI (or better, JNA) and send Windows Messages to the other programs textbox. I assume the other program is not a Swing App (in which case only AWT Robot works), and by sending native windows messages you can reliable find the other programs textbox even if it doesn't has the focus or is hidden in the background.

Categories