Why does Java AWT Robot not work on a game window? - java

I am currently experimenting with java awt robot and now I wanted to try to press something inside my game window.
For steam.exe, I put the following properties:
"compatibility to Windows 7",
"always run as administrator".
Then, I launched the game "Counter-Strike: Global Offensive" with this .bat file:
#echo off
start "" "D:\Program Files (x86)\Steam\Steam.exe" -login username password -applaunch 730 -low -nohltv -nosound -novid -window -w 400 -h 300 +exec autoexec.cfg -x 0 -y 0
The game launched and now I wanted to click something in the game with the following code:
public static void keyStroke(int x, int y) throws AWTException
{
Robot robo = new Robot();
robo.mouseMove(x, y);
delay(1000);//milliseconds
robo.mousePress(InputEvent.BUTTON1_DOWN_MASK);
delay(100);
robo.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
Here is a picture of the game:
The mouse goes to the correct location but when it should be clicking something, the game does not respond or recognize it.
However, if I do not put steam.exe in windows 7 compatibility, it works...
Why does robot not work anymore if the game is in windows 7 compatibility mode ?
Unfortunaetely I need to find a fix for this because I can only run steam in win7 comp.
Are there ways to fix it?

Okay I needed to run eclipse as administrator. My bad

Related

Java Robot class won't interact with an application?

I've been reading up on this and haven't found an answer that has made sense to me.
I'm trying to write a program in Java to interact with an application to see if I can write a program to play a video game for me. The game is on my computer.
Here is an excerpt of code:
public static void main(String[] args) throws java.io.IOException {
Runtime run = Runtime.getRuntime();
run.exec("open /Applications/OpenEmu.app");
try {
Robot robot = new Robot();
System.out.println("Waiting 5 Seconds");
//robot.delay(5000);
System.out.println("Pressed X");
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_X);
//Starts an easy mode game
It opens the application fine, and in something like notepad, it will type XXXX, but it won't do so for the game?
I've assigned the 'x' key on my keyboard as a command button for the game. My guess is that the 'x' press is internal. All help is appreciated!
If you are trying to simulate input, try add robot.keyRelease as well. Javadoc for robot says for keyPress "Presses a given key. The key should be released using the keyRelease method."
System.out.println("Pressed X");
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
...
Also remember this:
https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html "Note that some platforms require special privileges or extensions to access low-level input control. "

How to make my e4 swt app icon bounce in the Mac dock

i am trying to make my mac tray icon as suggested in How to make my app icon bounce in the Mac dock
this works fine with pure java applications and swings
but this doesn't works with e4 swt applications , how to make it bounce in this type applications
ref:
pfa of the sample code in the following link
https://bugs.eclipse.org/bugs/show_bug.cgi?id=321949
Application.requestUserAttention works for me in an e4 application (Eclipse 4.3.2 on Mac 10.9.3 with Java 1.8 update 5).
Note: It only does something if the application is not the focused app. With the false parameter there is only one bounce, specify true to make it bounce until the app has focus.
Update:
You can also do this using the SWT Mac specific classes, like this:
private static final long sel_requestUserAttention_ = OS.sel_registerName("requestUserAttention:");
private static final int NSCriticalRequest = 0;
private static final int NSInformationalRequest = 10;
...
NSApplication app = NSApplication.sharedApplication();
OS.objc_msgSend(app.id, sel_requestUserAttention_, NSInformationalRequest);
Use NSInformationalRequest for a single bounce, NSCriticalRequest to bounce until the app receive focus.
Since this is Mac only SWT code you will have to put it in a plugin or fragment with a platform filter in the MANIFEST.MF such as:
Eclipse-PlatformFilter: (& (osgi.ws=cocoa) (osgi.os=macosx) (osgi.arch=x86_64) )
Update:
The above code is for 64 bit SWT on Mac OSX, for 32 bit SWT use
private static final int sel_requestUserAttention_ = OS.sel_registerName("requestUserAttention:");

Set a dynamic Apple menu title for Java program in NetBeans 7.4

I inherited a Java app built (I believe) in Eclipse, which I'm modifying using NetBeans 7.4. I want to set the main menu title which shows up on a Mac next to the Apple menu. Right now that name is MainForm, but I want it to change dynamically to the contents of a specific text file (name.txt). I've looked up tons of info on project.properties, ANT scripts, and the like, but I can't find a definitive (and hopefully cross-platform) way to set this main menu title. I have a function in my code that returns this name, so I can use that if there's a place to do it. Thanks in advance!
I have found that in order to set the App Name in Mac OS X Application Menu, and avoid having it show up as the name of your Java project, you have to set it VERY early in the application cycle, using System.setProperty("apple.awt.application.name", "Your App Name");
Here's how I have mine set in my "main" java method that launches the application:
public static void main(String[] args) {
// the application menu for Mac OS X must be set very early in the cycle
String opSysName = System.getProperty("os.name").toLowerCase();
if (opSysName.contains("mac")) {
// to set the name of the app in the Mac App menu:
System.setProperty("apple.awt.application.name", "Your App Name");
//to show the menu bar at the top of the screen:
System.setProperty("apple.laf.useScreenMenuBar", "true");
// to show a more mac-like file dialog box
System.setProperty("apple.awt.fileDialogForDirectories", "true");
//underlying laf:
javax.swing.UIManager.getInstalledLookAndFeels();
// other set-up code goes here
}
else { // not on Mac OS X
// set-up code for non-Mac systems
}
java.awt.EventQueue.invokeLater(() -> {
// run the program
});
}

JOGL javaw.exe remains running after application is closed

I have a simple java application that uses JOGL. When I run it from eclipse, and then close the application window, javaw.exe remains running. Here is the the relevant code:
public class App {
private Display mDisplay;
private Shell mShell;
private GL4 mGl;
private int mProgramId;
private int mVsId;
private int mFsId;
// ...
public void start() {
if (!initialize()) {
return;
}
while (!mShell.isDisposed()) {
if (!mDisplay.readAndDispatch()) {
mDisplay.sleep();
}
}
destroy();
}
private void initialize() {
mDisplay = new Display();
mShell = new Shell(mDisplay);
// some SWT and opengl initialization code, which is irrelevant for this issue
// (at least I think so)
// getting GLProfile, GLContext, GL4 etc.
final String vsText = ResourceManager.getShaderText(vsPath);
final String fsText = ResourceManager.getShaderText(fsPath);
mVsId = mGl.glCreateShader(GL4.GL_VERTEX_SHADER);
mFsId = mGl.glCreateShader(GL4.GL_FRAGMENT_SHADER);
mGl.glShaderSource(mVsId, 1, new String[] { vsText }, null, 0);
mGl.glCompileShader(mVsId);
mGl.glShaderSource(mFsId, 1, new String[] { fsText }, null, 0);
mGl.glCompileShader(mFsId);
mProgramId = mGl.glCreateProgram();
mGl.glAttachShader(mProgramId, mFsId);
mGl.glAttachShader(mProgramId, mVsId);
// bind a constant attribute location for positions of vertices
mGl.glBindAttribLocation(mProgramId, 0, "in_Position");
// bind another constant attribute location, this time for color
mGl.glBindAttribLocation(mProgramId, 1, "in_Color");
mGl.glLinkProgram(mProgramId);
// here error code is 0x0 (no error)
int error = mGl.glGetError();
mShell.open();
return true;
}
private void destroy() {
// here error code is 0x502 (GL_INVALID_OPERATION)
int error = mGl.glGetError();
mGl.glDetachShader(mProgramId, mFsId);
mGl.glDetachShader(mProgramId, mVsId);
mGl.glDeleteShader(mFsId);
mGl.glDeleteShader(mVsId);
mGl.glDeleteProgram(mProgramId);
mDisplay.dispose();
}
}
I commented out all rendering code and most other opengl/JOGL related calls (besides getting GLProfile, GLContext, GL4 and everything listed in this sample) and this problem persists.
Generally, the application works fine, shaders compile and link without problem (I used validation which I didn't display in this sample) and it displays what it needs to. The only problem is that javaw.exe remains running after I close the application window (by pressing the x in the corner of the window).
This issue is removed only if I comment out mGl.glCompileShader(mVsId); and subsequent lines. If I leave this line, javaw.exe will remain running, so I guess the problem is related to shader initialization/destruction code.
Also, glGetError() returns 0 (no error) at the end of initialize() and 0x502 (GL_INVALID_OPERATION) at the beginning of destroy(). There is only the main loop in between and no opengl calls that I know of, since, for testing, I commented out all rendering code.
Any ideas?
Edit 2012-10-03:
I still don't know what the problem is, but since I updated my graphic card drivers, 'javaw.exe' terminates as it should after application is closed. I have AMD Radeon HD 6870. My current driver version is 8.982 from 2012-07-27, and I can't remember what the last version was, but I believe it was from january 2011 or so.
However, glGetError() still returns 0x502 at the beginning of destroy, so I guess there is still something wrong.
Assuming you use JOGL from jogamp.org, pls use either our SWT GLCanvas
or our NEWTCanvasSWT.
The latter is preferred due to custom GLCapabilities, pls check API doc.
This given plus you are doing everything SWT related on the SWT thread (read
linked unit tests), IMHO it should work - at least our unit tests.
Since you mentioned after an update (GPU/driver) your troubles ceased to exist,
it might have been a driver problem.
Now to your GL error. Trace GL errors can be simply done by setting the system property 'jogl.debug.DebugGL', i.e. on the commandline "-Djogl.debug.DebugGL".
This will install the debug pipeline for your GL object automatically and checks for GL error, which will throw an GLException if appear.
You can also trace via the property 'jogl.debug.TraceGL'.
I don't know if this is relevant or not, but may help someone I guess so I'm gonna share it here. Keep in mind I'm just a hobbyist getting started in Java SWT.
I made a simple application in Eclipse IDE using the Java SWT library. I made a ''Quit'' button in my main Window shell that when pushed calls this :
quitBtn.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
shell.getDisplay().dispose();
System.exit(0);
}
});
After exporting the .jar file to Desktop, I converted it to a .exe file with LaunchJ4 wrapper and check the "Allow only a single instance of the application" in the Single instance tab using the singleR3XPlayer mutex.
When I would close the .exe file with the "Quit" button, everything was fine and the javaw.exe would terminates. But when I closed the shell with the X button on the top-right window, javaw.exe would remains running. I figured that out when I tried to delete the .exe file (Windows "Used file, still open in Java(TM) Platform SE binary, close the file and retry" type of error pop-up) and as I couldn't open another instance of the file after closing it with the X button (because of the Single instance mutex). Also, multiple instances of javaw.exe would remains running if I would execute a few of the .jar file (even after closing them, but only with the X and not the "Quit" button).
I figured out closing the window with the X button would only dispose of the shell and not exit the program. But pressing the "Quit" button would because it called System.exit(0). So I did this :
// SWT Event Loop
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
// To make sure javaw.exe terminates when Window is closed
if (shell.isDisposed()) {
System.exit(0);
}
Hence, after my SWT Event loop is done (shell.isDisposed() == true), I made sure System.exit(0) would be called. This way, javaw.exe is terminated either way.
I don't know how SWT works and I don't know if that's the proper way to do it but it ensures javaw.exe is terminated once the application closes.
Hope that helps.

launch an external installer app in Mac from Java

I´m creating a small app in Java that needs to be used in Mac and Windows from a CD.
The basic idea of this app is just to have a main menu (different for Mac and Windows) where you can select several options (install an app, view the content of the CD, view the help manual...etc) with a the logo of a company...etc.
The app to be installed is going to be different in Windows and Mac.
What I want to do is launch the external installer and once is installed, i want to launch the app.
The main problem that i have is that once I've launched the installer in a different process, the waitfor() return a valid exitvalue and continues.
I want to wait until this app is totally installed before i try to run it.
for Windows
Runtime.getRuntime().exec(" \"c:/.../ExternalAppforWin.exe\"");
for Mac
File instFolder = new File(System.getProperty("user.dir") + "ExternalAppforMac.pkg")
Process p = Runtime.getRuntime().exec(new String[] { "open", instFolder.toString() });
int exitVal = p.waitFor();
if (exitVal==0)
...
Could you help me?
Thanks.
It seems that you need to check for the presence of the install window on the system rather than the executable. As far as I know, there is no system independent way to do this in Java, however with the use of powerful libraries like sun's JNA(which is supported on both windows and mac and can be found here) you can do this through the appropriate OS API calls.
here is an example of what you may want to do on windows, mac calls should be similiar:
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
.
.
.
//execute process
Process p = Runtime.getRuntime().exec(" \"c:/.../ExternalAppforWin.exe\"");
//wait for return value
int res = p.waitFor();
//if we have a valid return code begin waiting for window to be closed
if(res == 0)
{
//define a window handle variable
WinDef.HWND windowHandle = null;
do
{
//sleep a little while before polling the value
try{Thread.sleep(100);}catch(InterruptedException e){}
//try to fetch the window by title
windowHandle = User32.INSTANCE.FindWindow(null, "<Window Title>");
//if the handle is not null, the window is still open so sleep and then try try again
}while(windowHandle != null && windowHandle.getPointer() != Pointer.NULL);
//continue on with your code
}

Categories