3

How do I capture the mouse in a Java application so that all mouse events (even ones that happen if the mouse is moved outside the app window) are seen by the Java app? This is like the Windows SetCapture function.

asked Aug 26, 2010 at 0:29

4 Answers 4

3

You don't; the JVM, or more specifically AWT, only generates input events when Windows sends it input events, and the JVM only registers for those events which occur within it's window.

You might be able to pull it off using JNI, but then again you might not - it will depend if you can get your hands on the information required by the underlying API. Since that's likely to be a window handle, you won't have what you need to invoke the API, even from JNI.

answered Aug 26, 2010 at 0:40
Sign up to request clarification or add additional context in comments.

Comments

1

You have to hook the mouse at the operating system level. Windows(Swing, AWT, MFC, etc....) are only aware of mouse movements within their bounds. If you need a way to access the current position of the mouse regardless of where the mouse is on the screen, you need to write an Input Hook: Input Hooks. You can then use JNI or read the STDOUT from a win32 console application designed to use the Input Hook to forward mouse events/positions to your Java code. I use the latter method in some of my user interface test cases with success.

answered Nov 3, 2012 at 1:37

Comments

0

I needed to do that too!

I after searching the web I found that its possible to use the moveMouse in java.awt.Robot.

Basically use Robot to move the mouse into center of your frame. If user moves it: check how much and move it back to center.

No additional packets or JNI are needed for this (my demo uses JOGL and vecmath but that's for the graphics). Is it good enough? Try the demo, its here:

http://www.eit.se/hb/misc/java/examples/FirstPersonJavaProtoGame/

If the above solution is not good enough then perhaps lwjgl is what you need:

http://www.lwjgl.org/javadoc/org/lwjgl/input/Mouse.html

/Henrik Björkman

answered Feb 14, 2013 at 12:16

Comments

0

Just use the system-hook library available on gitHub https://github.com/kristian/system-hook

This only apply to windows-based systems but really simple to implement.

Sample usage

import lc.kra.system.keyboard.GlobalKeyboardHook;
import lc.kra.system.keyboard.event.GlobalKeyAdapter;
import lc.kra.system.keyboard.event.GlobalKeyEvent;
public class GlobalKeyboardExample {
 private static boolean run = true;
 public static void main(String[] args) {
 // might throw a UnsatisfiedLinkError if the native library fails to load or a RuntimeException if hooking fails 
 GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook();
 System.out.println("Global keyboard hook successfully started, press [escape] key to shutdown.");
 keyboardHook.addKeyListener(new GlobalKeyAdapter() {
 @Override public void keyPressed(GlobalKeyEvent event) {
 System.out.println(event);
 if(event.getVirtualKeyCode()==GlobalKeyEvent.VK_ESCAPE)
 run = false;
 }
 @Override public void keyReleased(GlobalKeyEvent event) {
 System.out.println(event); }
 });
 try {
 while(run) Thread.sleep(128);
 } catch(InterruptedException e) { /* nothing to do here */ }
 finally { keyboardHook.shutdownHook(); }
 }
}
answered Nov 4, 2016 at 10:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.