posts - 59, comments - 244, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Mouse Hook

Posted on 2011-02-21 21:32 penngo 阅读(917) 评论(0)  编辑  收藏 所属分类: Java
把之前写的鼠标钩子http://www.blogjava.net/pengo/archive/2010/12/17/340938.html代码封装下,放在博客里,下次要用时方便点,该部分代码已用在ColorCat中。
MouseHookProc.java
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser.HOOKPROC;

public interface MouseHookProc extends HOOKPROC{
    LRESULT callback(
int nCode, WPARAM wParam, MouseHookStruct lParam);
}

MouseHookStruct.java
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.BaseTSD.ULONG_PTR;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser.POINT;
/**
 * 屏幕取色工具
 * 
 * 
@author penngo(http://www.blogjava.net/pengo/)
 
*/

public class MouseHookStruct extends Structure
{
    
public static class ByReference extends MouseHookStruct implements Structure.ByReference {};
    
public POINT pt;
    
public HWND hwnd;
    
public int wHitTestCode;
    
public ULONG_PTR dwExtraInfo;
}

MouseHookListener.java
import com.sun.jna.platform.win32.WinDef.WPARAM;
/**
 * 屏幕取色工具
 * 
 * 
@author penngo(http://www.blogjava.net/pengo/)
 
*/

public interface MouseHookListener{
    
void mouseMove(int nCode, WPARAM wParam,
            MouseHookStruct lParam);
    
void lButtonDown(int nCode, WPARAM wParam,
            MouseHookStruct lParam);
    
void lButtonUp(int nCode, WPARAM wParam,
            MouseHookStruct lParam);
    
void rButtonDown(int nCode, WPARAM wParam,
            MouseHookStruct lParam);
    
void rButtonUp(int nCode, WPARAM wParam,
            MouseHookStruct lParam);
    
void mButtonDown(int nCode, WPARAM wParam,
            MouseHookStruct lParam);
    
void mButtonUp(int nCode, WPARAM wParam,
            MouseHookStruct lParam);
}

MouseHook.java
import com.sun.jna.Platform;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinDef.HMODULE;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser.HHOOK;
import com.sun.jna.platform.win32.WinUser.MSG;
/**
 * 
@author penngo(http://www.blogjava.net/pengo/)
 
*/

public class MouseHook {
    
public static final int WM_MOUSEMOVE = 512;
    
public static final int WM_LBUTTONDOWN = 513;
    
public static final int WM_LBUTTONUP = 514;
    
public static final int WM_RBUTTONDOWN = 516;
    
public static final int WM_RBUTTONUP = 517;
    
public static final int WM_MBUTTONDOWN = 519;
    
public static final int WM_MBUTTONUP = 520;
    
public User32 lib;
    
private static HHOOK hhk;
    
private MouseHookListener hookListener;
    
private HMODULE hMod;
    
private boolean isWindows = false;
    
private boolean isStart = false;
    
public MouseHook() {
        isWindows 
= Platform.isWindows();
        
if(isWindows){
            lib 
= User32.INSTANCE;
            hMod 
= Kernel32.INSTANCE.GetModuleHandle(null);
        }

        
    }


    
public void addMouseHookListener(MouseHookListener hookListener) {
        
this.hookListener = hookListener;
    }


    
public void startWindowsHookEx() {
        MouseHookProc hookProc 
= new MouseHookProc(){
            
public LRESULT callback(int nCode, WPARAM wParam,
                    MouseHookStruct lParam) 
{
                
if (nCode >= 0{
                    
switch (wParam.intValue()) {
                    
case MouseHook.WM_MOUSEMOVE:
                        hookListener.mouseMove(nCode, wParam, lParam);
                        
break;
                    
case MouseHook.WM_LBUTTONDOWN:
                        hookListener.lButtonDown(nCode, wParam, lParam);
                        
break;
                    
case MouseHook.WM_LBUTTONUP:
                        hookListener.lButtonUp(nCode, wParam, lParam);
                        
break;
                    
case MouseHook.WM_RBUTTONDOWN:
                        hookListener.rButtonDown(nCode, wParam, lParam);
                        
break;
                    
case MouseHook.WM_RBUTTONUP:
                        hookListener.rButtonUp(nCode, wParam, lParam);
                        
break;
                    
case MouseHook.WM_MBUTTONDOWN:
                        hookListener.mButtonDown(nCode, wParam, lParam);
                        
break;
                    
case MouseHook.WM_MBUTTONUP:
                        hookListener.mButtonUp(nCode, wParam, lParam);
                        
break;
                    }

                }

                
return lib.CallNextHookEx(hhk, nCode, wParam,
                        lParam.getPointer());
            }

        }
;
        
if(isWindows){
            isStart 
= true;
            lib.SetWindowsHookEx(WinUser.WH_MOUSE_LL, hookProc, hMod, 
0);
            MSG msg 
= new MSG();
            lib.GetMessage(msg, 
null00);
        }

    }


    
public void stopWindowsHookEx() {
        
if(isWindows && isStart){
            lib.UnhookWindowsHookEx(hhk);
            isStart 
= false;
        }

    }

}

使用例子,很简单,只需实现一个MouseHookListener接口就行了,在MouseHookListener里面实现你要干的事就行了。
TestMain.java
import com.sun.jna.platform.win32.WinDef.WPARAM;

public class TestMain {
    
public static void main(String[] args) throws Exception{
        MouseHook mouseHook 
= new MouseHook();
        mouseHook.addMouseHookListener(
new MouseHookListener() {
            
public void mouseMove(int nCode, WPARAM wParam,
                    MouseHookStruct lParam)
{
                
int x = lParam.pt.x;
                
int y = lParam.pt.y;
                System.out.println(x 
+ " " + y);
            }

            
public void lButtonDown(int nCode, WPARAM wParam,
                    MouseHookStruct lParam)
{
            }

            
public void lButtonUp(int nCode, WPARAM wParam,
                    MouseHookStruct lParam)
{
            }

            
public void rButtonDown(int nCode, WPARAM wParam,
                    MouseHookStruct lParam)
{
            }

            
public void rButtonUp(int nCode, WPARAM wParam,
                    MouseHookStruct lParam)
{
            }

            
public void mButtonDown(int nCode, WPARAM wParam,
                    MouseHookStruct lParam)
{
            }

            
public void mButtonUp(int nCode, WPARAM wParam,
                    MouseHookStruct lParam)
{
            }

        }
);
        mouseHook.startWindowsHookEx();
        Thread.sleep(
10000);
        mouseHook.stopWindowsHookEx();
    }

}
以上代码需要jna包支持。

代码放在ColorCat里面,svn下载地址:https://colorcat.googlecode.com/svn/trunk/src/com/colorcat/mouse,需要的自己下载。

只有注册用户登录后才能发表评论。


网站导航: