Esempio n. 1
0
        private IList<KeyboardButton> FindButtonList(KeyExEventArgs args)
        {
            short key = (short)args.KeyValue;
            if (key == KeyboardConstaint.VK_LCONTROL || key == KeyboardConstaint.VK_RCONTROL)
            {
                key = KeyboardConstaint.VK_CONTROL;
            }
            else if (key == KeyboardConstaint.VK_LSHIFT || key == KeyboardConstaint.VK_RSHIFT)
            {
                key = KeyboardConstaint.VK_SHIFT;
            }
            else if (key == KeyboardConstaint.VK_LMENU || key == KeyboardConstaint.VK_RMENU)
            {
                key = KeyboardConstaint.VK_MENU;
            }
            else if (key == KeyboardConstaint.VK_RWIN)
            {
                key = KeyboardConstaint.VK_LWIN;
            }

            if (combinationVKButtonsMap.ContainsKey(key))
            {
                return combinationVKButtonsMap[key];
            }

            IList<KeyboardButton> buttonList = new List<KeyboardButton>();
            foreach (System.Windows.Forms.Control ctrl in this.Controls)
            {
                KeyboardButton button = ctrl as KeyboardButton;

                if (button == null)
                {
                    continue;
                }

                short theKey = button.VKCode;
                if (theKey == key)
                {
                    IDictionary<KeyboardButton, KeyboardButton> buttonMap = new Dictionary<KeyboardButton, KeyboardButton>();
                    buttonMap.Add(this.btnENT, this.btnNUMENT);
                    buttonMap.Add(this.btnNUM0, this.btnINS);
                    buttonMap.Add(this.btnNUMDot, this.btnDEL);
                    buttonMap.Add(this.btnNUM1, this.btnEND);
                    buttonMap.Add(this.btnNUM2, this.btnDN);
                    buttonMap.Add(this.btnNUM3, this.btnPDN);
                    buttonMap.Add(this.btnNUM4, this.btnLA);
                    buttonMap.Add(this.btnNUM6, this.btnRA);
                    buttonMap.Add(this.btnNUM7, this.btnHM);
                    buttonMap.Add(this.btnNUM8, this.btnUP);
                    buttonMap.Add(this.btnNUM9, this.btnPUP);
                    if (buttonMap.ContainsKey(button))
                    {
                        if ((args.Flags & 0x00000001) == 0)
                        {
                            buttonList.Add(button);
                        }
                        else {
                            buttonList.Add(buttonMap[button]);
                        }
                    }
                    else if (buttonMap.Values.Contains(button))
                    {
                        if ((args.Flags & 0x00000001) == 0)
                        {
                            KeyboardButton keyButton = null;
                            foreach (KeyValuePair<KeyboardButton, KeyboardButton> entry in buttonMap)
                            {
                                if (object.ReferenceEquals(entry.Value, button))
                                {
                                    keyButton = entry.Key;
                                    break;
                                }
                            }
                            if (keyButton != null)
                            {
                                buttonList.Add(keyButton);
                            }
                        }
                        else {
                            buttonList.Add(button);
                        }
                    }
                    else {
                        buttonList.Add(button);
                    }
                }
            }

            return buttonList;
        }
Esempio n. 2
0
        private void SetButtonStatus(KeyExEventArgs args, bool isDown)
        {
            IList<KeyboardButton> buttonList = FindButtonList(args);

            if (buttonList.Count <= 0)
            {
                return;
            }

            short key = (short)args.KeyValue;
            if (spacialVKButtonsMap.ContainsKey(key))
            {
                if (!isDown)
                {
                    KeyboardButton button = spacialVKButtonsMap[key][0];
                    button.Checked = !button.Checked;
                }
            }
            else {
                foreach (KeyboardButton button in buttonList)
                {
                    button.Checked = isDown;
                }
            }

            SetButtonText();
        }
Esempio n. 3
0
 private void HookOnGlobalKeyDown(object sender, KeyExEventArgs e)
 {
     SetButtonStatus(e, true);
 }
Esempio n. 4
0
 private void HookOnGlobalKeyUp(object sender, KeyExEventArgs e)
 {
     SetButtonStatus(e, false);
 }
Esempio n. 5
0
        private IntPtr KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            //indicates if any of underlaing events set e.Handled flag
            bool handled = false;

            EventHandler<KeyExEventArgs> handlerKeyDown = this.Events[EventKeyDown] as EventHandler<KeyExEventArgs>;
            EventHandler<KeyExEventArgs> handlerKeyUp = this.Events[EventKeyUp] as EventHandler<KeyExEventArgs>;
            EventHandler<KeyPressExEventArgs> handlerKeyPress = this.Events[EventKeyPress] as EventHandler<KeyPressExEventArgs>;

            //it was ok and someone listens to events
            if ((nCode >= 0) && (handlerKeyDown != null || handlerKeyUp != null || handlerKeyPress != null))
            {
                //read structure KeyboardHookStruct at lParam
                KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                //raise KeyDown
                if (handlerKeyDown != null && ((int)wParam == WM_KEYDOWN || (int)wParam == WM_SYSKEYDOWN))
                {
                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyExEventArgs e = new KeyExEventArgs(keyData, MyKeyboardHookStruct.flags);
                    handlerKeyDown(this, e);
                    handled = handled || e.Handled;
                }

                // raise KeyPress
                if (handlerKeyPress != null && (int)wParam == WM_KEYDOWN)
                {
                    bool isDownShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);
                    bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);

                    byte[] keyState = new byte[256];
                    GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    if (ToAscii(MyKeyboardHookStruct.vkCode,
                          MyKeyboardHookStruct.scanCode,
                          keyState,
                          inBuffer,
                          MyKeyboardHookStruct.flags) == 1)
                    {
                        char key = (char)inBuffer[0];
                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))
                            key = Char.ToUpper(key);
                        KeyPressExEventArgs e = new KeyPressExEventArgs(key, MyKeyboardHookStruct.flags);
                        handlerKeyPress(this, e);
                        handled = handled || e.Handled;
                    }
                }

                // raise KeyUp
                if (handlerKeyUp != null && ((int)wParam == WM_KEYUP || (int)wParam == WM_SYSKEYUP))
                {
                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyExEventArgs e = new KeyExEventArgs(keyData, MyKeyboardHookStruct.flags);

                    handlerKeyUp(this, e);
                    handled = handled || e.Handled;
                }

            }

            //if event handled in application do not handoff to other listeners
            if (handled)
                return (IntPtr)1;
            else
                return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
        }