A few questions

I want to write a sort of macro-like program, I'll admit, for computer games. I was wondering if anyone could help me with the following:

1. How to detect keyboard strokes when the application is NOT active. I've used KeyListeners, but I don't think they work when another window is active and its running underneath it.

2. How can I send Keyboard/Mouse signals. I've heard of using a "Robot." Is this hard, and is there a good guide for it?

3. Can someone explain or link to a very detailed guide on creating EXECUTABLE jar's out of an application? I have tried following many guides and still don't get it.

Any help appreciatedm, thanks.

[676 byte] By [etgohomeoka] at [2007-11-27 10:52:33]
# 1

Is this for the Window's platform? If so, you may have an easier time of doing this by using either C++ or C# + user32.dll. If anyone knows of a better way with java, I'd be happy to be wrong here, so please let me know.

petes1234a at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 2

> I want to write a sort of macro-like program, I'll

> admit, for computer games. I was wondering if anyone

> could help me with the following:

>

Sure.

> 1. How to detect keyboard strokes when the

> application is NOT active. I've used KeyListeners,

> but I don't think they work when another window is

> active and its running underneath it.

>

Can't be done in pure java. You will likely have to implement JNI. Here is a good reference:

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html

> 2. How can I send Keyboard/Mouse signals. I've heard

> of using a "Robot." Is this hard, and is there a good

> guide for it?

>

You can - indeed - use a Robot. Here is the API:

http://java.sun.com/j2se/1.3/docs/api/java/awt/Robot.html

> 3. Can someone explain or link to a very detailed

> guide on creating EXECUTABLE jar's out of an

> application? I have tried following many guides and

> still don't get it.

>

Here is an example of how to create an executable jar:

public class Test {

public static void main(String[] argv) {

System.out.println("Hello, this is an executable jar.");

}

}

Place the following in a file and call it manifest.mf

Manifest-Version: 1.5

Main-Class: Test

Now execute the following:

javac Test.java

jar -cvfm Test.jar manifest.mf Test.class

To execute the executable jar, type the following:

java -jar Test.jar

Voila.

Navy_Codera at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 3

It's going to be for Windows users.

Ok... I could probably use buttons instead of hotkeys. Robot looks good.

>javac Test.java

>jar -cvfm Test.jar manifest.mf Test.class

When you say "execute," do you mean put that in a .bat file and run it? And...

>To execute the executable jar, type the following:

>java -jar Test.jar

How could I make it so that it can be run by double clicking on it?

etgohomeoka at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 4

> Is this for the Window's platform? If so, you may

> have an easier time of doing this by using either C++

> or C# + user32.dll. If anyone knows of a better way

> with java, I'd be happy to be wrong here, so please

> let me know.

I recommend not using C# in this particular case for the following reasons:

1) C# is - as you undoubtedly know - part of the .NET framework. If you wish to include a reference to a dll, it needs to also be part of the CLR. user32 is not part of the CLR, so adding this DLL is more difficult than you might think.

2) There are no good (personal opinion) open source IDEs (trust me, you need one for .NET) for C#. (SharpDevelop comes close, but it is extremely buggy. Don't waste your time with the Microsoft Express Editions. (You can download the .NET SDK and use the command line compiler - but good luck with that. Assemblies are a *****-and-a-half.) Your only other option is the $500 Microsoft (read: Satan) version.

3) Using C and writing a simple JNI module will fit very nicely into your Java application (though you will lose that platform independence that all java developer strive for ... ;-) (<-- joke... the part about all java developers... nevermind...)

4) If you are going to use C/C++, then go ahead and just build the JNI and proceed with Java. Java is well suited for the rest of what you have specified.

Navy_Codera at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 5

Save this file to c:\Example.java

import javax.swing.*;

public class Example extends JFrame {

public Example() {

setDefaultCloseOperation(EXIT_ON_CLOSE);

pack();

setSize(500, 500);

setTitle("Example");

setLocationRelativeTo(null);

}

}

Open up a command prompt, "Start-->Run-->CMD [enter]" and type the following:

cd c:\

javac Example.java

Save the following to a file called c:\manifest.mf:

Manifest-Version: 1.5

Main-Class: Example

Go back to your command prompt and type the following:

jar -cvfm Example.jar manifest.mf Example.class

Now go back to your desktop.

Right-click, new-->Shortcut

Path: c:\Example.jar

Give it a name.

Done.

Double click on that icon.

Voila.

Navy_Codera at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 6

Ok thanks. I'll try that.

etgohomeoka at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 7

> > Is this for the Window's platform? If so, you may

> > have an easier time of doing this by using either

> C++

> > or C# + user32.dll. If anyone knows of a better

> way

> > with java, I'd be happy to be wrong here, so

> please

> > let me know.

>

> I recommend not using C# in this particular

> case for the following reasons:

> 1) C# is - as you undoubtedly know - part of the .NET

> framework. If you wish to include a reference to a

> dll, it needs to also be part of the CLR. user32 is

> not part of the CLR, so adding this DLL is more

> difficult than you might think.

>

> 2) There are no good (personal opinion) open

> source IDEs (trust me, you need one for .NET) for C#.

> (SharpDevelop comes close, but it is

> extremely buggy. Don't waste your time with

> the Microsoft Express Editions. (You can

> download the .NET SDK and use the command line

> compiler - but good luck with that. Assemblies are

> a *****-and-a-half.) Your only other option is the

> $500 Microsoft (read: Satan) version.

>

> 3) Using C and writing a simple JNI module will fit

> very nicely into your Java application (though you

> will lose that platform independence that all java

> developer strive for ... ;-) (<-- joke... the part

> about all java developers... nevermind...)

>

> 4) If you are going to use C/C++, then go ahead and

> just build the JNI and proceed with Java. Java is

> well suited for the rest of what you have specified.

.. and don't get me started on the deficiencies of the .NET framework. The first time they even approach usability is in 3.0. And even then, it's still buggy (read: Microsoft-esque). Take this for example ... something that I have taken for granted until today at work:

public enum VehicleMake {

CHEVROLET, FORD, PONTIAC, DODGE, TOYOTA, HONDA;

public String toString() {

switch(this) {

case CHEVROLET:

return "C";

case FORD:

return "F";

case PONTIAC:

return "P";

case DODGE:

return "D";

case TOYOTA:

return "T";

case HONDA:

return "H";

}

}

}

That is illegal in C#. (1.1, 2.0, 3.0, 3.5)

And Generics? I don't think that .NET incorporated those into C# until .NET 3.0. Which is still really flaky.

Not to mention the fact that they threw out checked exceptions. The default operation of any .NET application where an exception goes uncaught (regardless of the type of exception): crash. (Straight from the horses mouth. If you need a citation on that, give me a while and I'll dig it up.)

So they have properties, big frickin' whoop-dee-doo. They are so overwhelmingly misused that it isn't funny. That, and I prefer the traditional accessors and modifiers (regardless of what jverd may thing.... ;-)

Navy_Codera at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 8

I tried following the executable jar instructions, but ended up with this when I tried javac:

'javac' is not recognized as an internal or external command, operable program or batch file.

I tried using the class file of a program I've already written, and when I tried the jar command on the command prompt, I got the same error message for it.

Do I need to install anything?

etgohomeoka at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 9

> I tried following the executable jar instructions,

> but ended up with this when I tried javac:

>

> 'javac' is not recognized as an internal or external

> command, operable program or batch file.

>

> I tried using the class file of a program I've

> already written, and when I tried the jar command on

> the command prompt, I got the same error message for

> it.

>

> Do I need to install anything?

No. You need to set your path variable.

Find the bin directory in your java folder (probably something like:

c:\program files\java\jdk1.6.0_01\bin\)

Copy that path.

Click start->settings->control panel

Double click on the System icon.

Go to the "Advanced" tab.

Click on "Environment Variables" (button at bottom of window)

Click "Path" in the listbox at the top of the page.

Click "edit"

Press the [End] key to go to the end of the line.

Append a semicolon (;)

paste in the path of your jdk bin directory.

Accept all those changes.

Close your CMD prompt.

Open it again.

Try the steps I told you all over again. This time it should work.

Navy_Codera at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 10

> I recommend not using C# in this particular

> case for the following reasons:

> 1) C# is - as you undoubtedly know - part of the .NET

> framework. If you wish to include a reference to a

> dll, it needs to also be part of the CLR. user32 is

> not part of the CLR, so adding this DLL is more

> difficult than you might think.

It's not that hard. I've created apps that control others through user32.dll using DLLImport. It's as simple as adding:

[DllImport("User32.Dll", EntryPoint = "SetForegroundWindow")]

public static extern void SetForegroundWindow(IntPtr hwnd);

The tricky part of course is you have to be scrupulous about memory usage and handling resources, but with care, it can be done without all that much difficulty.

> Don't waste your time with

> the Microsoft Express Editions.

Personal opinion. I kind of like this IDE.

> Your only other option is the

> $500 Microsoft (read: Satan) version.

Agree. Microsoft is the devil incarnate.

> 3) Using C and writing a simple JNI module will fit

> very nicely into your Java application

I've never worked with JNI but keep hearing about how difficult it is to deal with. I guess it depends upon who you talk to. Some may find the JNI hard, others the c# dllimport, you say potato and I say potahto.

Navy_Coder, please understand that I am not saying you are wrong, just that you are not absolutely right (but then, who is?).

petes1234a at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 11

> It's not that hard. I've created apps that control

> others through user32.dll using DLLImport. It's as

> simple as adding:

> [code]

> [DllImport("User32.Dll", EntryPoint =

> "SetForegroundWindow")]

> public static extern void

> SetForegroundWindow(IntPtr hwnd);

> /code]

> The tricky part of course is you have to be

> scrupulous about memory usage and handling resources,

> but with care, it can be done without all that much

> difficulty.

>

I still think it's more of a pest than writing JNI - perhaps because I like C and have more experience with it, perhaps because I sincerely dislike C# (or perhaps both)

> > Don't waste your time with

> > the Microsoft Express Editions.

>

> Personal opinion. I kind of like this IDE.

Exactly as I said. Personal opinion.

>

> > Your only other option is the

> > $500 Microsoft (read: Satan) version.

>

> Agree. Microsoft is the devil incarnate.

>

... Who doesn't? ;-)

> > 3) Using C and writing a simple JNI module

> will fit

> > very nicely into your Java application

>

> I've never worked with JNI but keep hearing about how

> difficult it is to deal with. I guess it depends

> upon who you talk to. Some may find the JNI hard,

> others the c# dllimport, you say potato and I say

> potahto.

>

You can get a template for JNI. Fill in the blank. Compile. Done. Again, I'm sure it comes down to having done it time and time again.

> Navy_Coder, please understand that I am not saying

> you are wrong, just that you are not absolutely right

> (but then, who is?).

Why would I think you were telling me I was wrong? You didn't contradict anything that I said... (And I will never ever claim to be absolutely right about anything ...)

Navy_Codera at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 12

> No. You need to set your path variable.

> Find the bin directory in your java folder (probably

> something like:

> c:\program files\java\jdk1.6.0_01\bin\)

> Copy that path.

> Click start->settings->control panel

>

> Double click on the System icon.

> Go to the "Advanced" tab.

> Click on "Environment Variables" (button at bottom of

> window)

> Click "Path" in the listbox at the top of the page.

> Click "edit"

> Press the [End] key to go to the end of the line.

> Append a semicolon (;)

> paste in the path of your jdk bin directory.

>

> Accept all those changes.

> Close your CMD prompt.

> Open it again.

> Try the steps I told you all over again. This time

> it should work.

Ok I tried doing that. It didn't work, so I went and checked out my bin folder and noticed that there is no javac.exe or jar.exe. There are a bunch of other .exe's. Is my Java corrupt or am I missing a patch or something?

etgohomeoka at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 13

You need to install the JDK, not the JRE.

jverda at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...
# 14

Ok I looked around and found it in the jdk folder. I went through and followed all of those directions. It created the class, added it and the manifest that I put in to a jar, and I clicked it, but I got an error message saying something like:

"Error loading Main-Class attribute from manifest"

I checked and the Main-Class attribute looks fine.

etgohomeoka at 2007-7-29 11:38:13 > top of Java-index,Java Essentials,New To Java...