Beispiel #1
0
            private List <Keys> preKeysList = new List <Keys>();//存放被按下的控制键,用来生成具体的键
            private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
            {
                //如果该消息被丢弃(nCode<0)或者没有事件绑定处理程序则不会触发事件
                if ((nCode >= 0) && (OnKeyDownEvent != null || OnKeyUpEvent != null || OnKeyPressEvent != null))
                {
                    KeyboardHooks.KeyboardHookStruct KeyDataFromHook = (KeyboardHooks.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHooks.KeyboardHookStruct));
                    Keys keyData = (Keys)KeyDataFromHook.vkCode;
                    //按下控制键
                    if ((OnKeyDownEvent != null || OnKeyPressEvent != null) && (wParam == KeyboardHooks.WM_KEYDOWN || wParam == KeyboardHooks.WM_SYSKEYDOWN))
                    {
                        if (IsCtrlAltShiftKeys(keyData) && preKeysList.IndexOf(keyData) == -1)
                        {
                            preKeysList.Add(keyData);
                        }
                    }
                    //WM_KEYDOWN和WM_SYSKEYDOWN消息,将会引发OnKeyDownEvent事件
                    if (OnKeyDownEvent != null && (wParam == KeyboardHooks.WM_KEYDOWN || wParam == KeyboardHooks.WM_SYSKEYDOWN))
                    {
                        KeyEventArgs e = new KeyEventArgs(GetDownKeys(keyData));

                        OnKeyDownEvent(this, e);
                    }
                    //WM_KEYDOWN消息将引发OnKeyPressEvent
                    if (OnKeyPressEvent != null && wParam == KeyboardHooks.WM_KEYDOWN)
                    {
                        byte[] keyState = new byte[256];
                        KeyboardHooks.GetKeyboardState(keyState);
                        byte[] inBuffer = new byte[2];
                        if (KeyboardHooks.ToAscii(KeyDataFromHook.vkCode, KeyDataFromHook.scanCode, keyState, inBuffer, KeyDataFromHook.flags) == 1)
                        {
                            KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);
                            OnKeyPressEvent(this, e);
                        }
                    }
                    //松开控制键
                    if ((OnKeyDownEvent != null || OnKeyPressEvent != null) && (wParam == KeyboardHooks.WM_KEYUP || wParam == KeyboardHooks.WM_SYSKEYUP))
                    {
                        if (IsCtrlAltShiftKeys(keyData))
                        {
                            for (int i = preKeysList.Count - 1; i >= 0; i--)
                            {
                                if (preKeysList[i] == keyData)
                                {
                                    preKeysList.RemoveAt(i);
                                }
                            }
                        }
                    }
                    //WM_KEYUP和WM_SYSKEYUP消息,将引发OnKeyUpEvent事件
                    if (OnKeyUpEvent != null && (wParam == KeyboardHooks.WM_KEYUP || wParam == KeyboardHooks.WM_SYSKEYUP))
                    {
                        KeyEventArgs e = new KeyEventArgs(GetDownKeys(keyData));
                        OnKeyUpEvent(this, e);
                    }
                }
                return(KeyboardHooks.CallNextHookEx(hHook, nCode, wParam, lParam));
            }
Beispiel #2
0
            public void SetHook()
            {
                KeyboardHookDelegate = new KeyboardHooks.HookProc(KeyboardHookProc);
                Process       cProcess = Process.GetCurrentProcess();
                ProcessModule cModule  = cProcess.MainModule;
                var           mh       = KeyboardHooks.GetModuleHandle(cModule.ModuleName);

                hHook = KeyboardHooks.SetWindowsHookEx(KeyboardHooks.WH_KEYBOARD_LL, KeyboardHookDelegate, mh, 0);
            }
Beispiel #3
0
        private void Main_Load(object sender, EventArgs e)
        {
            font            = fontDialog1.Font = label1.Font;
            fColor          = colorDialog1.Color = label1.ForeColor;
            bColor          = colorDialog2.Color = panel2.BackColor;
            rbZheng.Checked = true;
            label2.Text     = "当前字体:\r\n" + label1.Font.Name + "," + label1.Font.Size.ToString() + "px";

            KeyboardHooks.SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 1 | 2);

            kh = new KeyboardHooks.KeyboardHook();
            kh.SetHook();
            kh.OnKeyDownEvent += kh_OnKeyDownEvent;
        }
Beispiel #4
0
 public void UnHook()
 {
     KeyboardHooks.UnhookWindowsHookEx(hHook);
 }