Getting active window information in Java

Save yourself some pain and use JNA. You will need to download jna.jar and jna-platform.jar for the Win32 API. The pinvoke wiki and MSDN are useful for finding the right system calls. Anyway, here is the code to print the title and process of the currently active window. import static enumeration.EnumerateWindows.Kernel32.*; import static enumeration.EnumerateWindows.Psapi.*; import … Read more

Working example of JNA mouse hook

Yep, here is the code… public class CWMouseHook { public final User32 USER32INST; public final Kernel32 KERNEL32INST; public CWMouseHook() { if(!Platform.isWindows()) { throw new UnsupportedOperationException(“Not supported on this platform.”); } USER32INST = User32.INSTANCE; KERNEL32INST = Kernel32.INSTANCE; mouseHook=hookTheMouse(); Native.setProtected(true); } public static LowLevelMouseProc mouseHook; public HHOOK hhk; public Thread thrd; public boolean threadFinish = true; public … Read more

JNA Keyboard Hook in Windows

It appears you need to call GetMessage or PeekMessage, which is odd – it is not mentioned in the documentation for Hooks or LowLevelKeyboardProc. I don’t know enough about this part of the API to guess at the reason. I just used the example classes: import com.sun.jna.examples.win32.*; public class Callback { public static User32.HHOOK hHook; … Read more