参考一下,可以用KeyDown或 KeyUp 来判断是否按下了Win+Dprotected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
        {            bool handled = false;            if (nCode > -1 && (KeyDown != null || KeyUp != null || KeyPress != null))
            {                KeyboardHookStruct keyboardHookStruct =
                    (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));                // Is Control being held down?
                bool control = ((GetKeyState(VK_LCONTROL) & 0x80) != 0) ||
                               ((GetKeyState(VK_RCONTROL) & 0x80) != 0);                // Is Shift being held down?
                bool shift = ((GetKeyState(VK_LSHIFT) & 0x80) != 0) ||
                             ((GetKeyState(VK_RSHIFT) & 0x80) != 0);                // Is Alt being held down?
                bool alt = ((GetKeyState(VK_LALT) & 0x80) != 0) ||
                           ((GetKeyState(VK_RALT) & 0x80) != 0);                // Is CapsLock on?
                bool capslock = (GetKeyState(VK_CAPITAL) != 0);                // Create event using keycode and control/shift/alt values found above
                KeyEventArgs e = new KeyEventArgs(
                    (Keys)(
                        keyboardHookStruct.vkCode |
                        (control ? (int)Keys.Control : 0) |
                        (shift ? (int)Keys.Shift : 0) |
                        (alt ? (int)Keys.Alt : 0)
                        ));                // Handle KeyDown and KeyUp events
                switch (wParam)
                {                    case WM_KEYDOWN:
                    case WM_SYSKEYDOWN:
                        if (KeyDown != null)
                        {
                            KeyDown(this, e);
                            handled = handled || e.Handled;
                        }
                        break;
                    case WM_KEYUP:
                    case WM_SYSKEYUP:
                        if (KeyUp != null)
                        {
                            KeyUp(this, e);
                            handled = handled || e.Handled;
                        }
                        break;                }                // Handle KeyPress event
                if (wParam == WM_KEYDOWN &&
                   !handled &&
                   !e.SuppressKeyPress &&
                    KeyPress != null)
                {                    byte[] keyState = new byte[256];
                    byte[] inBuffer = new byte[2];
                    GetKeyboardState(keyState);                    if (ToAscii(keyboardHookStruct.vkCode,
                              keyboardHookStruct.scanCode,
                              keyState,
                              inBuffer,
                              keyboardHookStruct.flags) == 1)
                    {                        char key = (char)inBuffer[0];
                        if ((capslock ^ shift) && Char.IsLetter(key))
                            key = Char.ToUpper(key);
                        KeyPressEventArgs e2 = new KeyPressEventArgs(key);
                        KeyPress(this, e2);
                        handled = handled || e.Handled;                    }                }            }            if (handled)
            {
                return 1;
            }
            else
            {
                return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
            }        }

解决方案 »

  1.   

    - - 单独拦截Win 是可以的 +D 是拦截不到不知道是不是我代码的问题如果这样是可以的 只是拦截到了键盘事件,米有根本上解决,可能用户用的是右下角的显示桌面按钮,这样的是没有按键消息的查了快一个星期了发现问题的根本是,显示桌面执行的消息,是不能通过用全局钩子拦截自己窗体接收的到的,不能像移动任务栏可以在程序中收到消息号为26的消息。之后发现 Win+D 和 显示桌面 按钮 最后执行的 是 Shell32.dll 中的 ToggleDesktop() 方法,而这方法执行后会向 Progman 桌面窗体发送消息,所有一开拦截的就是错的应该是拦截Progman 这个窗体消息谢谢 doubleu2005 这就你一个回答的,分都给你了 ^_^ 
      

  2.   

    请问如何拦截Progman这个窗体的消息呢,我也遇到这个问题了。