Home > Back-end >  JNA realize how close the mouse hook?
JNA realize how close the mouse hook?

Time:03-26

Code is as follows:
 

Package the mouse;

Import the Java. The awt. AWTException;
Import the Java. The awt. MouseInfo;
Import the Java. The awt. Robot;
import java.util.Date;

The import com. Sun. Jna. Platform. The win32. Kernel32;
The import com. Sun. Jna. Platform. The win32. User32;
The import com. Sun. Jna. Platform. The win32. WinDef;
The import com. Sun. Jna. Platform. The win32. WinUser;

Public class MouseCatch implements Runnable {

/* *
* get the mouse state interval
*/
Public static final ints MOUSE_TIME_GET_STAT_DELAY=1000;
/* *
* the mouse
*/
Protected the static final ints _MOUSE_MOVED=512;
/* *
* click press
*/
Protected the static final ints _MOUSE_LEFT_PRESSED=513;
/* *
* click pop-up
*/
Protected the static final ints _MOUSE_LEFT_POPS_UP=514;
/* *
* right press
*/
Protected the static final ints _MOUSE_RIGHT_PRESSED=516;
/* *
* right-click pop-up
*/
Protected the static final ints _MOUSE_RIGHT_POPS_UP=517;
/* *
The * key press
*/
Protected the static final ints _MOUSE_MIDDLE_PRESSED=519;
/* *
* button pop-up
*/
Protected the static final ints _MOUSE_MIDDLE_POPS_UP=520;
/* *
* keys scroll
*/
Protected the static final ints _MOUSE_MIDDLE_ROTATED=522;

Public static void main (String [] args) throws AWTException, InterruptedException {
System. The out. Println (" is under monitoring,,,, ");
MouseCatch hook=new MouseCatch ();
New Thread (hook.) start ();
//hook. SetHookOn ();

//Thread. Sleep (MOUSE_TIME_GET_STAT_DELAY);
Hook. SetHookOff ();

}

Public void outstr (String STR) {
System.out.println(str);
}

Private Boolean mouseMoved=false;
Private Boolean mouseLeftPressed=false;
Private Boolean mouseRightPressed=false;
Private Boolean mouseMiddlePressed=false;
Private Boolean mouseMiddleRotated=false;

Final User32 lib=User32. INSTANCE;
The static volatile Date last=new Date ();
//is the handle to a Windows, in this case, the specified is the type of mouse User32 WH_MOUSE_LL
Private WinUser. HHOOK HHK;
Private WinUser. LowLevelKeyboardProc mouseProc=new WinUser. LowLevelKeyboardProc () {

@ Override
Public WinDef. LRESULT callback (int nCode, WinDef. WPARAM WPARAM, WinUser. The KBDLLHOOKSTRUCT event) {
If (nCode & gt;=0) {
//MouseCatch. Last=new Date ();
SetMouseMiddleRotated (false);
SetMouseMoved (false);

The switch (wParam. IntValue ()) {
Case _MOUSE_MOVED:
//System. Out. Println (" mouse ");
//System. Out. Println (" mouse "+ event. DwExtraInfo. ToPointer ());
SetMouseMoved (true);
break;
Case _MOUSE_LEFT_PRESSED:
//System. Out. Println (" the left mouse button pressed ");
SetMouseleftPressed (true);
break;
Case _MOUSE_LEFT_POPS_UP:
//System. Out. Println (" the left mouse button pop-up ");
SetMouseleftPressed (false);
break;
Case _MOUSE_RIGHT_PRESSED:
//System. Out. Println (" the right mouse button click ");
SetMouseRightPressed (true);
break;
Case _MOUSE_RIGHT_POPS_UP:
//System. Out. Println (" the right mouse button pop-up ");
SetMouseRightPressed (false);
break;
Case _MOUSE_MIDDLE_PRESSED:
//System. Out. Println (" middle mouse button pressed ");
SetMouseMiddlePressed (true);
break;
Case _MOUSE_MIDDLE_POPS_UP:
//System. Out. Println (" middle mouse button pop-up ");
SetMouseMiddlePressed (false);
break;
Case _MOUSE_MIDDLE_ROTATED:
//System. Out. Println (" middle mouse button scroll ");
SetMouseMiddleRotated (true);
break;
Default:
SetMouseMiddleRotated (false);
SetMouseMoved (false);
System. The out. Println (wParam. IntValue ());
}

//outstr (String. The valueOf (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "));
//outstr (String. The valueOf (isMouseMoved ()));
//outstr (String. The valueOf (isMouseLeftPressed ()));
//outstr (String. The valueOf (isMouseRightPressed ()));
//outstr (String. The valueOf (isMouseMiddlePressed ()));
//outstr (String. The valueOf (isMouseMiddleRotated ()));
}
Return the lib. CallNextHookEx (HHK, nCode wParam, null);
}
};

@ Override
Public void the run () {
System. The out. Println (" setHookOn th ");
SetHookOn ();
System. Err. Println (" setHookOn TTH ");
While (true) {
System. Err. Println (" setHookOn th ");
Try {
Thread.sleep (MOUSE_TIME_GET_STAT_DELAY);
{} catch InterruptedException (e)
//TODO to automatically generate the catch block of
e.printStackTrace();
}
If (System. CurrentTimeMillis () - MouseCatch. Last, getTime () & gt; {
=10000)MouseCatch. Last=new Date ();
}
SetHookOff ();
}
}

Public void setHookOn () {

//get assigned module handles
WinDef. HMODULE hMod=Kernel32. INSTANCE. GetModuleHandle (null);
//install hooks, returns a handle to the hook
//HHK=User32. The INSTANCE. The SetWindowsHookEx (User32. WH_MOUSE_LL mouseProc, hMod, 0).
HHK=lib. SetWindowsHookEx (User32. WH_MOUSE_LL mouseProc, hMod, 0).
Int result;
WinUser. MSG MSG=new WinUser. MSG ();
//this function retrieves the message from the calling thread's message queue, and put it in the specified structure
//User32. INSTANCE. GetMessage (MSG, null, 0, 0);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related