2013年10月29日星期二

About WinUser keyboard and mouse hook

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.HOOKPROC;
import com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT;
import com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc;
import com.sun.jna.platform.win32.WinUser.MSG;

/**
 * @author Administrator
 */
public class MyHooker extends Thread{


private static HHOOK kHHOOK=null;
private static HHOOK mHHOOK=null;
private static HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null); 
private static MSG msg=new MSG();

public void run(){
HOOKPROC kHOOK=new LowLevelKeyboardProc(){
     public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam) {  
     MyThread.i=0;
     return User32.INSTANCE.CallNextHookEx(kHHOOK, nCode,wParam,lParam.getPointer());
     }
     }; 
     HOOKPROC mHOOK=new LowLevelKeyboardProc(){
     public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT lParam) {  
     MyThread.i=0;
     return User32.INSTANCE.CallNextHookEx(mHHOOK, nCode,wParam,lParam.getPointer());
     }
     }; 
     kHHOOK= User32.INSTANCE.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL,kHOOK,hMod,0);  
mHHOOK= User32.INSTANCE.SetWindowsHookEx(WinUser.WH_MOUSE_LL,mHOOK,hMod,0); 
System.out.println("^^^^^^^^^^^^^^^^");

User32.INSTANCE.GetMessage(msg,null,0,0);

/*while(true){
boolean a=User32.INSTANCE.PeekMessage(msg,null,0,0,1);
System.out.println("PeekMessage:"+a);
}*/
System.out.println("$$$$$$$$$$$$$$$$");
}

public static void unHook(){
System.out.println("unHook start...");
//User32.INSTANCE.DispatchMessage(msg);
User32.INSTANCE.UnhookWindowsHookEx(kHHOOK);  
User32.INSTANCE.UnhookWindowsHookEx(mHHOOK); 
System.out.println("unHook end");

}
}


code above
Why perform User32.INSTANCE.GetMessage (msg, null, 0,0);
have HOOKPROC of callBack call ?
There are not removed can call GetMessage method callBack
mainly because GetMessage blocking the thread ;
MFC system hook I have seen
not understand that piece
initiate a lot more advice of delivery


------ Solution ------------------------------------ --------
never played jna, Bangniding next .
------ For reference only -------------------------------------- -
Am I a cold field Timor ?
Top about !
------ For reference only -------------------------------------- -

post has died of knot it to you

没有评论:

发表评论