示例#1
0
        void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            tbMsg.Visibility = Visibility.Hidden;

            //when alt is pressed, the real key should be e.SystemKey
            Key key = (e.Key == Key.System ? e.SystemKey : e.Key);

            SpecialKeyState specialKeyState = GlobalHotkey.Instance.CheckModifiers();

            var hotkeyModel = new HotkeyModel(
                specialKeyState.AltPressed,
                specialKeyState.ShiftPressed,
                specialKeyState.WinPressed,
                specialKeyState.CtrlPressed,
                key);

            var hotkeyString = hotkeyModel.ToString();

            if (hotkeyString == tbHotkey.Text)
            {
                return;
            }

            Dispatcher.InvokeAsync(async () =>
            {
                await Task.Delay(500);
                SetHotkey(hotkeyModel);
            });
        }
示例#2
0
 public void RemoveHotkey(HotkeyModel hotkey)
 {
     if (_hotkeys.ContainsKey(hotkey))
     {
         _hotkeys.Remove(hotkey);
     }
 }
示例#3
0
 public void SetHotkey(HotkeyModel hotkey, Action action)
 {
     if (!_hotkeys.ContainsKey(hotkey) && hotkey.Key != Key.None && hotkey.ModifierKeys.Length > 0)
     {
         _hotkeys[hotkey] = action;
     }
     else
     {
         throw new ArgumentException("hotkey existed");
     }
 }
示例#4
0
        public void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true)
        {
            CurrentHotkey = keyModel;

            tbHotkey.Text = CurrentHotkey.ToString();
            tbHotkey.Select(tbHotkey.Text.Length, 0);

            if (triggerValidate)
            {
                CurrentHotkeyAvailable = CheckHotkeyAvailability();
                if (!CurrentHotkeyAvailable)
                {
                    tbMsg.Foreground = new SolidColorBrush(Colors.Red);
                    tbMsg.Text = InternationalizationManager.Instance.GetTranslation("hotkeyUnavailable");
                }
                else
                {
                    tbMsg.Foreground = new SolidColorBrush(Colors.Green);
                    tbMsg.Text = InternationalizationManager.Instance.GetTranslation("succeed");
                }
                tbMsg.Visibility = Visibility.Visible;
                OnHotkeyChanged();
            }
        }
 private void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
 {
     string hotkeyStr = hotkey.ToString();
     try
     {
         HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
     }
     catch (Exception)
     {
         string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
         MessageBox.Show(errorMsg);
     }
 }
示例#6
0
        void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            tbMsg.Visibility = Visibility.Hidden;

            //when alt is pressed, the real key should be e.SystemKey
            Key key = (e.Key == Key.System ? e.SystemKey : e.Key);

            SpecialKeyState specialKeyState = GlobalHotkey.Instance.CheckModifiers();

            var hotkeyModel = new HotkeyModel(
                specialKeyState.AltPressed,
                specialKeyState.ShiftPressed,
                specialKeyState.WinPressed,
                specialKeyState.CtrlPressed,
                key);

            var hotkeyString = hotkeyModel.ToString();

            if (hotkeyString == tbHotkey.Text)
            {
                return;
            }

            Dispatcher.DelayInvoke("HotkeyAvailabilityTest",
                o =>
                {
                    SetHotkey(hotkeyModel);
                },
                TimeSpan.FromMilliseconds(500));
        }
示例#7
0
 public void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
 {
     var hotkey = new HotkeyModel(hotkeyStr);
     SetHotkey(hotkey, action);
 }
示例#8
0
        private IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            const int pressed     = 0; // HC_ACTION
            IntPtr    intercepted = (IntPtr)1;

            if (nCode == pressed)
            {
                var m    = (WindowsMessage)wParam.ToInt32();
                var info = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
                if (m == WindowsMessage.KEYDOWN || m == WindowsMessage.SYSKEYDOWN)
                {
                    var pressedHotkey = new HotkeyModel(info.vkCode);
                    if (pressedHotkey.Key != Key.None &&
                        pressedHotkey.ModifierKeys.Length != 0)
                    {
                        if (!Capturing)
                        {
                            if (_hotkeys.ContainsKey(pressedHotkey))
                            {
                                _modifierPressed = true;
                                var action = _hotkeys[pressedHotkey];
                                action();
                                return(intercepted);
                            }
                            else
                            {
                                return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                            }
                        }
                        else if (HotkeyCaptured != null)
                        {
                            _modifierPressed = true;
                            var args = new HotkeyCapturedEventArgs
                            {
                                Hotkey    = pressedHotkey,
                                Available = !_hotkeys.ContainsKey(pressedHotkey),
                            };
                            HotkeyCaptured.Invoke(this, args);
                            return(intercepted);
                        }
                        else
                        {
                            return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                        }
                    }
                    else
                    {
                        return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                    }
                }
                else if (m == WindowsMessage.KEYUP)
                {
                    // if win+r is pressed, windows star menu will still popup
                    // so we need to discard keyup event
                    if (_modifierPressed)
                    {
                        _modifierPressed = false;
                        if (info.vkCode == Key.LWIN || info.vkCode == Key.RWIN || info.vkCode == Key.WIN)
                        {
                            return(intercepted);
                        }
                        else
                        {
                            return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                        }
                    }
                    else
                    {
                        return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                    }
                }
                else
                {
                    return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
                }
            }
            else
            {
                return(InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam));
            }
        }