My goal is assign a global hotkey (JIntellitype, JXGrabKey) that would pass an arbitrary selected text to a java app.
The initial plan is to utilize the java.awt.Robot to emulate Ctrl-C keypress and then get the value from clipboard.
Probably there's a more elegant solution?
EXAMPLE: Open Notepad, type in some text, select that text. Now, that text needs to be copied into a Java app.
I guess you want to implement a global input monitor, Java is not so straightforward to do the job. You may have to write an API hook and pack it in a DLL, then invoke it via JNI.
The Robot only sends events inside your JVM. I don't know of anything to send events out to the operating system. Though there is plenty of examples out there of making JNI calls to the windows API, it would then be platform specific.
I've gone with with Robot and that works just fine.
Related
I'm looking to write a relatively simple key macro for my own edification and also for my own use.
I intend for it to be able to run in the background when I run other programs, and at the press of a button will enter certain text into the active window. To be more specific, I want to go in a video game and use it to automatically send messages instead of having to type them.
I Googled around a bit, but apparently I'm not using the right keywords because I'm not really sure where to start. The closest I came was finding the Keystroke class, but that appears to be used for receiving keystrokes, not sending them.
I would appreciate and info regarding, or directing me to a resource for, these issues (how to send keystrokes to windows and anything about targeting which window etc).
To send key strokes you can use java.awt.Robot
To Choose which window to activate you can look around for ws script (windows script) or old VB6 code and use it to make VB script (simple text file of extn .vbs)
Or you can junk all that and use http://www.autohotkey.com/ which has window activation, sending keys, doing things on press of certain keys (like Windows Key + B) or macros.
To get a window to activate I had made an exe long back, but no longer use it, can get it from http://sourceforge.net/projects/win-utils/files/Window-Position/rel%2001/ (but only get this if the others do not work as need to seperately get COMCTL32.ocx and install that
If you do not want to use autohotkey you can use Jini to call platform specific functions, with a wrapper to call correst OSes functions. Never done it my self, when i had to use it i would make a process to call a exe that made the window come the front.
I have a windows application which has a complex GUI that I would like to hide from users. In order to do this, I would like to create a wrapper with an extremely simple interface that overlays this application and automates a number of actions when a user clicks a single button on the wrapper. (I hope "wrapper" is the proper term.) Is it possible to use Java to block input to the underlying application so that users cannot inadvertently mess up the automation? How would I go about this? Also, how can I automate key presses and clicks to the application without hijacking the mouse? Is this possible in Java?
I have looked at java.awt.Robot, but it appears to hijack the mouse.
I have also looked at AutoIT, but it too hijacks the mouse and does not integrate with Java.
Neither of these options seem powerful enough for what I need, but I do not know how else to proceed.
I recommend that automation via the GUI only as the last resort if you really have no other alternative.
If there is an API that your application exposes, I would try to use that. For example, if the GUI is implemented in one DLL and the logic in another, then you can use JNA to load your application logic DLL and invoke the application functions directly from java. Even better would be if your application exposes a COM/OLE interface - there are plenty of Java<>COM briges, that will alow you to call this interface directly, e.g. Jacob.
If you really have no choice but to automate via the GUI, then here's how to go about doing that:
Use JNA to access the windows shell API. You can then use ShellExecute to launch your wrapped application. Specifically, passing SW_HIDE as the window mode should help ensure that the application does not appear.
Use JNA to access the windows API FindWindow to find your application window. You can also make it invisible using the ShowWindow API, just in case step 1 did not work (not all applications are written to use the nCmdShow parameter.)
You can now post messages to the application window using PostMessage. You can send keystrokes and mouse events using windows messages. E.g. See WM_KEYUP, WM_LBUTTONDOWN.
Because the wrapped application window is made invisible, you don't need to "block" that application, The user simply cannot access it's GUI. But you can still programmatically send input to it.
I'm looking to record keystrokes and mouse positions while I'm in a browser window on various websites, but I'm not sure how to do this outside the SWING (or some other GUI) window.
How can I record keystrokes when a different application's window is the one active?
The support you need is not exposed by Java. It is a fairly simple matter to do this if you have access to the RECORD extension in X, or Win32 on Windows.
Here is a Python project that implements support for both of these platforms: pykeylogger.
(Windows only) In theory you could use JNA or JNI to bind to GetAsyncKeyState and you'd be able to monitor key presses.
I don't think you can do this from pure Java. If it is possible at all, it would require the assistance of a (highly) platform specific native code library.
I'm writing an application that monitors the person’s mouse and keyboard. If they have not used the keyboard or mouse for 1 minute, it will pop up a message that says “You have not used the mouse or keyboard for 1 minute” along with an OK button.
How do I do this in Java?
You need a bit of C/C++ code and call SetWindowsHookEx This function allows you to hook into Windows events and receive a copy.
This question contains code to get you started: JNA Keyboard Hook in Windows
If you want to do this for only your application, then its very simple. You can simply add listerns i.e Toolkit.getDefaultToolkit.addAwtEventListener(..).
But for the system as a whole, I am afraid, it cannot be done in java, you may use JNI though.
If you only want to monitor activity in Java application windows, it's trivial – all you have to do is register to the appropriate event.
But to monitor all mouse and keyboard activity in the OS you will have to hook to the API which is platform dependent and will require using the JNI
I am trying to find a way how to write a "software sensor" in Java, running on Mac OS X (10.5), which records each application focus change.
My first idea was to write a "pull sensor" that uses an Applescript, which just returns the name of the app. that is in focus. Obviously this approach is not very good.
Therefore, I was wondering if there is the possibility to get a callback or some kind of notification directly from the operating system, whenever another application gets into the focus.
Thanks in advance!
Cheers Julian
java recieves no events from other applications, so there would be nothing to intercept and record. im sure OSX's SDK has something you could use though. it wouldn't be java based (unless you used JNI or JNA).