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

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.

Related

Java Robot keypresses not picked up by global hotkey settings of other program

I've written a program that can detect the sound output of my PC (only works with StereoMix on Windows in Java from what I could find). Once there is a silence of 2-3 seconds, I trigger a keypress. Once there is a silence of 10+ seconds, I trigger a different keypress.
For the first keypress I have the program selected and it works just fine.
For the second keypress, however, I have global hotkeys defined in another program. These don't get picked up when the keypresses are done by Java.
My google searches sadly seem to only find things related to not detecting a keypress event in your Java program.
The code to perform the key presses. Adding a delay in between pressing and releasing makes no difference.
Robot robot = new Robot();
robot.keyPress(KeyEvent.CTRL_DOWN_MASK);
robot.keyPress(KeyEvent.SHIFT_DOWN_MASK);
robot.keyPress(KeyEvent.VK_F12);
robot.keyRelease(KeyEvent.CTRL_DOWN_MASK);
robot.keyRelease(KeyEvent.SHIFT_DOWN_MASK);
robot.keyRelease(KeyEvent.VK_F12);

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.

Java Robot class keyPress not typing into google chrome

I'm trying to beat the game 'fastTyper 2' (basically just a typing game where you type words that appear as fast as possible) by using the java robot class to type in the letters for me. I've successfully taken in the letters to be typed. However, it seems that the robot's keyPress method isn't working due to some kind of protection the game has. Does anyone know of a way to get around this?
Thanks
I don't think that the game protection is stopping you because there is no way for a game(or any other program) to decide whether the key is pressed manually or programatically.
Maybe you need to check the logic at what point of time you need your Robot class to press the key and which key to press(and make sure that you pass correct parameter value to keyPress method)

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.

Inject a keystroke in 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

Categories