Ejemplo n.º 1
0
 /// <summary>
 /// Disable keyboard hooking.
 /// </summary>
 /// <returns>True if disabled correctly.</returns>
 public static bool Disable()
 {
     if (Enabled == true)
     {
         try
         {
             Hooks.UnhookWindowsHookEx(hHook);
             Enabled = false;
             return(true);
         }
         catch
         {
             Enabled = true;
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Start the keyboard hook.
 /// </summary>
 /// <returns>True if no exceptions.</returns>
 public static bool Enable()
 {
     if (Enabled == false)
     {
         try
         {
             using (Process curProcess = Process.GetCurrentProcess())
                 using (ProcessModule curModule = curProcess.MainModule)
                     hHook = Hooks.SetWindowsHookEx((int)Hooks.HookType.WH_KEYBOARD_LL, hookproc, Hooks.GetModuleHandle(curModule.ModuleName), 0);
             Enabled = true;
             return(true);
         }
         catch
         {
             Enabled = false;
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 3
0
        private static IntPtr Filter(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool result = true;

            if (nCode >= 0)
            {
                if (wParam == (IntPtr)Hooks.WM_KEYDOWN ||
                    wParam == (IntPtr)Hooks.WM_SYSKEYDOWN)
                {
                    int vkCode = Marshal.ReadInt32(lParam);
                    if ((Keys)vkCode == Keys.LControlKey ||
                        (Keys)vkCode == Keys.RControlKey)
                    {
                        Control = true;
                    }
                    else if ((Keys)vkCode == Keys.LShiftKey ||
                             (Keys)vkCode == Keys.RShiftKey)
                    {
                        Shift = true;
                    }
                    else if ((Keys)vkCode == Keys.RMenu ||
                             (Keys)vkCode == Keys.LMenu)
                    {
                        Alt = true;
                    }
                    else if ((Keys)vkCode == Keys.RWin ||
                             (Keys)vkCode == Keys.LWin)
                    {
                        Win = true;
                    }
                    else
                    {
                        result = OnKeyDown((Keys)vkCode);
                    }
                }
                else if (wParam == (IntPtr)Hooks.WM_KEYUP ||
                         wParam == (IntPtr)Hooks.WM_SYSKEYUP)
                {
                    int vkCode = Marshal.ReadInt32(lParam);
                    if ((Keys)vkCode == Keys.LControlKey ||
                        (Keys)vkCode == Keys.RControlKey)
                    {
                        Control = false;
                    }
                    else if ((Keys)vkCode == Keys.LShiftKey ||
                             (Keys)vkCode == Keys.RShiftKey)
                    {
                        Shift = false;
                    }
                    else if ((Keys)vkCode == Keys.RMenu ||
                             (Keys)vkCode == Keys.LMenu)
                    {
                        Alt = false;
                    }
                    else if ((Keys)vkCode == Keys.RWin ||
                             (Keys)vkCode == Keys.LWin)
                    {
                        Win = false;
                    }
                    else
                    {
                        result = OnKeyUp((Keys)vkCode);
                    }
                }
            }

            return(result ? Hooks.CallNextHookEx(hHook, nCode, wParam, lParam) : new IntPtr(1));
        }