Beispiel #1
0
 private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
 {
     if (nCode >= 0 && (this.KeyDown != null || this.KeyUp != null || this.KeyPress != null))
     {
         KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
         if (this.KeyDown != null && (wParam == 256 || wParam == 260))
         {
             Keys         keyData = (Keys)MyKeyboardHookStruct.vkCode;
             KeyEventArgs e       = new KeyEventArgs(keyData);
             this.KeyDown(this, e);
         }
         if (this.KeyPress != null && wParam == 256)
         {
             byte[] keyState = new byte[256];
             GlobalHook.GetKeyboardState(keyState);
             byte[] inBuffer = new byte[2];
             if (GlobalHook.ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1)
             {
                 KeyPressEventArgs e2 = new KeyPressEventArgs((char)inBuffer[0]);
                 this.KeyPress(this, e2);
             }
         }
         if (this.KeyUp != null && (wParam == 257 || wParam == 261))
         {
             Keys         keyData = (Keys)MyKeyboardHookStruct.vkCode;
             KeyEventArgs e       = new KeyEventArgs(keyData);
             this.KeyUp(this, e);
         }
     }
     return(GlobalHook.CallNextHookEx(GlobalHook._hKeyboardHook, nCode, wParam, lParam));
 }
Beispiel #2
0
 private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
 {
     if (nCode >= 0 && this.OnMouseActivity != null)
     {
         MouseButtons button = MouseButtons.None;
         if (wParam != 513)
         {
             if (wParam == 516)
             {
                 button = MouseButtons.Right;
             }
         }
         else
         {
             button = MouseButtons.Left;
         }
         int clickCount = 0;
         if (button != MouseButtons.None)
         {
             if (wParam == 515 || wParam == 518)
             {
                 clickCount = 2;
             }
             else
             {
                 clickCount = 1;
             }
         }
         MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
         MouseEventArgs  e = new MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0);
         this.OnMouseActivity(this, e);
     }
     return(GlobalHook.CallNextHookEx(GlobalHook._hMouseHook, nCode, wParam, lParam));
 }
Beispiel #3
0
 public void Stop(int hMouseHook, int hKeyboardHook)
 {
     if (hMouseHook != 0)
     {
         GlobalHook.UnhookWindowsHookEx(hMouseHook);
     }
     if (hKeyboardHook != 0)
     {
         GlobalHook.UnhookWindowsHookEx(hKeyboardHook);
     }
 }
Beispiel #4
0
 private void Form1_Load(object sender, EventArgs e)
 {
     btnInstallHook.Enabled = true;
     btnUnInstall.Enabled   = false;
     //初始化钩子对象
     if (hook == null)
     {
         hook                  = new GlobalHook();
         hook.KeyDown         += new KeyEventHandler(hook_KeyDown);
         hook.KeyPress        += new KeyPressEventHandler(hook_KeyPress);
         hook.KeyUp           += new KeyEventHandler(hook_KeyUp);
         hook.OnMouseActivity += new MouseEventHandler(hook_OnMouseActivity);
     }
 }
Beispiel #5
0
        public bool Start()
        {
            bool result;

            if (GlobalHook._hMouseHook == 0)
            {
                this.MouseHookProcedure = new GlobalHook.GlobalHookProc(this.MouseHookProc);
                try
                {
                    GlobalHook._hMouseHook = GlobalHook.SetWindowsHookEx(14, this.MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
                }
                catch (Exception err_4B)
                {
                    Time_Dates.TimeDates.WriteErroTOLog(err_4B.ToString());
                }
                if (GlobalHook._hMouseHook == 0)
                {
                    this.Stop();
                    result = false;
                    return(result);
                }
            }
            if (GlobalHook._hKeyboardHook == 0)
            {
                this.KeyboardHookProcedure = new GlobalHook.GlobalHookProc(this.KeyboardHookProc);
                try
                {
                    GlobalHook._hKeyboardHook = GlobalHook.SetWindowsHookEx(13, this.KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
                }
                catch (Exception err2_B7)
                {
                    Time_Dates.TimeDates.WriteErroTOLog(err2_B7.ToString());
                }
                if (GlobalHook._hKeyboardHook == 0)
                {
                    this.Stop();
                    result = false;
                    return(result);
                }
            }
            result = true;
            return(result);
        }
Beispiel #6
0
        public void Stop()
        {
            bool retMouse    = true;
            bool retKeyboard = true;

            if (GlobalHook._hMouseHook != 0)
            {
                retMouse = GlobalHook.UnhookWindowsHookEx(GlobalHook._hMouseHook);
                GlobalHook._hMouseHook = 0;
            }
            if (GlobalHook._hKeyboardHook != 0)
            {
                retKeyboard = GlobalHook.UnhookWindowsHookEx(GlobalHook._hKeyboardHook);
                GlobalHook._hKeyboardHook = 0;
            }
            if (!retMouse || !retKeyboard)
            {
            }
        }