Is it possible to detect a mouse click anywhere on a screen, outside of my application?
I have written an application to do this in C#, but would like to write a version of this in Java so that it can be run on several platforms.
It looks like I can get the co-ordinates of the mouse at any time with java.awt.MouseInfo.getPointerInfo() but I am not sure as to how to listen for a mouse click.
In C# I used GetAsyncKeyState to detect whether the mouse button was clicked, but obviously I cannot use this if I wish to keep this "clean" for use in multiple platforms.
You can do this only with platform specific implementation of the OS API, as you can't detect clicks outside from your program in your program itself.
While you won't get around writing platform specific code, just abstract it as an interface and use different implementations appropiately.
Related
I have a swing Java application (A) that launches another application (B) using Runtime.getRuntime().exec() when a certain button is pressed.
Is it possible to set a certain Key Hook so that when the application (B) is open and active, it changes the buttons (for example, when you press "Q", in fact, the "H" button is pressed, and so on). And a similar situation for the mouse (for example, if "Mouse Mid" or "Mouse Wheel" is pressed, in fact another button is pressed, for example "T").
Ideally, it should work for all operating systems if possible, but Windows primarily.
Please tell me if it is possible to implement this at all, I will be glad to any advice or examples.
As you want to hook with operating system's low level events. It could require JNI implementation that could be complex and will require OS libraries. There is one Maven library available at Github https://github.com/kristian/system-hook
It contains required libs as mentioned in Readme.md file. I haven't tried this. Hopefully it should work.
I have a macro tool that runs using pascal. I need to track the movements of the mouse while the program is running over very long periods of time. I wrote a basic mouse recorder in java that draws the movements and clicks in slow motion so they are easier to analyze, using jnativehook. It works fine when I move the mouse manually, but it does not pick up mouse movements from pascal. It does however pick up mouse clicks from pascal.
How can I pick up mouse movements made by a pascal macro tool in Java?
The issue is how the events are generated in Pascal and are going to depend on the platform you are working with. I would suggest using the event posting functionality in JNativeHook as it will always be able to pick up the events it generates. You can still send events that the library can pick up outside of the library, but on some platforms it requires using a specific API as some methods cannot be picked up by the listener on specific platforms.
Using Java, is it possible to detect user actions, such as key-presses, mouse-button-presses, and/or mouse's screen location, without the use of GUI elements?
If it is, how could I implement it?
Otherwise, what are the alternatives?
The only StackOverflow source i found on this matter is this question, which is unanswered.
And the only sources I found outside StackOverflow on this matter point to an "Invisible GUI" solution, wish is something i really want to avoid.
It can be implemented using JNI and/or JNA but this cannot be really called "java implementation" because you will have to write platform specific native code.
Alternative solution I tried is to use full screen transparent widow that is listening to all events of mouse and keyboard and "forwards" them to the real application using class Robot. I tried this approach. It works well with one limitation: there is a problem to support "mouse over" events of applications: the mouse is not moving over the real application. It is moving over the transparent java window.
Use the java.awt.event.KeyListener class. You will have to write three methods in which you can write which key you want to be detected, and whatever you want to have happen when the key is pressed.
I want to be able to record mouse movements, clicks and keyboard input from a user. It would be great if it was a cross platform solution.
I'd like to get back something like this (pseudo code):
mouse moved to 500, 500
mouse double clicked
mouse moved to 800, 300
mouse left clicked
keyboard typed "Hello World"
Does either C++ or Java have any classes that can do this? If I was using C++, I would probably working with the QT framework.
Edit:
I should have said this originally, but I want to record the movements and clicks outside of the applications gui, so on the desktop too.
GLUT does this, but it's tied to OpenGL which might be overkill for your project.
OpenGL is cross-platform.
I don't believe there's a cross-platform toolkit specifically for grabbing input from a window and nothing more, but most toolkits provide this capability. Two good options are:
Use SDL, as it's fairly lightweight and can handle simple input.
Implement the functionality natively per platform, as it should be trivial in X11, Windows, Mac OS X, etc.
On Windows, this is called a Journal Record Hook. You should write the hook part in C or C++, it might be technically possible to do in java, but it's not a good idea, you want your hook procedure to have as few dependencies as possible, and to be a quick as possible. System wide hooks, especially journal add a lot of overhead to keyboard and mouse input, you want to minized your impact as much as possible.
You Install Windows hooks by using SetWindowsHookEx passing WH_JOURNALRECORD to get a Journal Record Hook.
You could also (maybe) get this working by installing both WH_KEYBOARD_LL and WH_MOUSE_LL, but your two hook procedures would be called separately, and you would have to write your own code to put the events in order.
I doubt you will find a cross-platform solution.
It sounds like Qt might allow you to implement event filters that extend beyond the application to the window system. See also Qt - top level widget with keyboard and mouse event transparency?
If you want to trap events across the whole GUI system, not just one app, there's not much likelihood of a cross platform solution. However, the event hooking part could easily be separated from the recording part, so you could make most of the program cross-platform.
For Windows, you need this 17 year old (!) document. (Man, I'm getting old!)
I am developing a screen capturing utility in Java & I want to capture any background window, when I click on that particular window. But I am not getting how to add mouseClicked event to background window. Can somebody please help me?
I may be way off base but if the other window is not a Java window then it should be outside the Java sandbox. To interact with it requires a native API which is anathema to Java.
Quite obviously as it is you can't interact with other application windows. It can be any random window in your case I presume. Therefore, your mouselistener approach is not correct.
Rather, try to approach it like fetching pixel information displayed on the screen. There is an awt package java.awt.Robot or something that could be used for your purpose. If you want to implement capturing of active window then see if there are java APIs to interact with O.S. and get information of current active window and it's pixel co-ordinates. The co-ordinates could then be supplied to the rectangle attribute that is used with java.awt.Robot APIs to define screen capture area.
If that window is not part of your application you can't do much with it.
Otherwise you just have to add the mouse listener to that window too.
What's your situation?
java.awt.Robot has a method createScreenCapture(Rectangle screenRect) to capture screenshots.
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Robot.html
however, to get the current active window you would have to use OS specific extensions (mostly via JNI)