-
November 16th, 2017, 06:30 PM
#1
JNA GetMessage stops working after a short period?
So I need to get Windows 10 touch screen events so I created the below code which registers the JFrame for windows message events. The code below successfully intercepts the WM_GESTURENOTIFY exactly how I want it however after a brief period of time anywhere between 1 second and 60 seconds it stops receiving Windows messages and I have to restart it. Any idea what I am doing wrong?
Thanks!
Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hmi.gui;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser;
import java.awt.Color;
import java.awt.MouseInfo;
import javax.swing.JPanel;
public class TouchDetector {
public static class CWPSSTRUCT extends Structure {
public LPARAM lParam;
public WPARAM wParam;
public DWORD message;
public HWND hwnd;
@Override
protected List getFieldOrder() {
return Arrays.asList(new String[]{"lParam", "wParam", "message", "hwnd"});
}
}
public interface WinHookProc extends WinUser.HOOKPROC {
WinDef.LRESULT callback(int nCode, WinDef.WPARAM wParam, CWPSSTRUCT hookProcStruct);
}
public final class EventHookProc implements WinHookProc {
public WinUser.HHOOK hhook;
@Override
public LRESULT callback(int nCode, WPARAM wParam, CWPSSTRUCT hookProcStruct) {
if (nCode >= 0) {
if (hookProcStruct.message.intValue() == 282) {
System.out.println("YOU JUST TOUCHED x: " + MouseInfo.getPointerInfo().getLocation().x + " y: " + MouseInfo.getPointerInfo().getLocation().y);
}
}
return User32.INSTANCE.CallNextHookEx(hhook, nCode, wParam, hookProcStruct.getPointer());
}
}
public static interface GetMsgProc extends WinUser.HOOKPROC {
LRESULT callback(int nCode, WPARAM wParam, WinUser.MSG lParam);
}
public void init() {
try {
JFrame frame = new JFrame("TEST");
frame.setSize(500, 500);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setBackground(Color.red);
frame.add(panel);
// get the window handle for the main window/frame
final HWND hwnd = new HWND();
hwnd.setPointer(Native.getComponentPointer(frame));
// clear the error value
Native.setLastError(0);
// retrieve the threadID associated with the main window/frame
int windowThreadID = User32.INSTANCE.GetWindowThreadProcessId(hwnd, null);
if (windowThreadID == 0) {
int x = Native.getLastError();
throw new IllegalStateException("error calling GetWindowThreadProcessId when installing machine-shutdown handler " + x);
}
final EventHookProc proc = new EventHookProc();
proc.hhook = User32.INSTANCE.SetWindowsHookEx(4, new EventHookProc(), null, windowThreadID);
if (proc.hhook == null) {
int x = Native.getLastError();
throw new IllegalStateException("error calling SetWindowsHookEx when installing machine-shutdown handler " + x);
}
System.out.println("Installed WIndows Event hook procedure");
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
TouchDetector h = new TouchDetector();
h.init();
}
}
-
November 17th, 2017, 07:51 AM
#2
Re: JNA GetMessage stops working after a short period?
Ok seems I was not performing an unhook on app close which was causing problems on next run.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|