Ejemplo n.º 1
0
        /// <summary>
        /// 注册热键,窗体句柄,热键ID,辅助键,实键
        /// </summary>
        private void RegisterHotKey()
        {
            const HotKeyModifiers hm = HotKeyModifiers.ModControl | HotKeyModifiers.ModAlt;
            var success = HotKeyHelper.RegisterHotKey(this.Handle, 0, hm, Keys.A);

            Console.WriteLine("--热键注册:" + success);
            if (success)
            {
                return;
            }
            MessageBox.Show("神话快捷截图,热键 Ctrl + Alt + A 已被其它程序占用\n使用替代方案 Alt + A",
                            Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Information);
            var success2 = HotKeyHelper.RegisterHotKey(this.Handle, 0, HotKeyModifiers.ModAlt, Keys.A);

            if (success2)
            {
                return;
            }
            var result = MessageBox.Show("神话快捷截图,热键 Alt + A 已被其它程序占用,程序将退出",
                                         Resources.app_name, MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (result == DialogResult.OK)
            {
                Close();
            }
        }
Ejemplo n.º 2
0
 public void RegisterHotKey(Keys key, HotKeyModifiers modifiers)
 {
     if (!FindHotKey(key, modifiers).Equals(HotKeyData.Empty))
     {
         this.registerHotKey(key, modifiers);
     }
 }
Ejemplo n.º 3
0
        private void registerHotKey(Keys key, HotKeyModifiers modifiers)
        {
            string atomName = string.Empty;
            IntPtr atomId;

            atomName = key.ToString() + modifiers.ToString() + Environment.TickCount.ToString();
            if (atomName.Length > 255)
            {
                atomName = atomName.Substring(0, 255);
            }

            atomId = GlobalAddAtom(atomName);
            if (atomId == IntPtr.Zero)
            {
                throw new Exception("Impossible d'enregistrer l'atome du raccourci !");
            }

            if (!RegisterHotKey(this.Handle, atomId.ToInt32(), (int)modifiers, (int)key))
            {
                GlobalDeleteAtom(atomId);
                //throw new Exception("Impossible d'enregistrer le raccourci !");
            }

            this.hotkeys.Add(new HotKeyData(key, modifiers, atomId));
        }
Ejemplo n.º 4
0
        public void SetHotkey(Keys key, HotKeyModifiers mod)
        {
            Config.Instance.Hotkey         = key;
            Config.Instance.HotKeyModifier = mod;

            m_hotkey.ListenForHotKey(key, mod);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 为被管理的 Window 注册新的快捷键。
        /// </summary>
        /// <param name="fsModifiers">指定快捷键的修饰键部分。</param>
        /// <param name="key">指定快捷键的非修饰键部分。</param>
        /// <param name="handler">指定此快捷键按下时应当在 UI 线程上被调用的委托。</param>
        /// <exception cref="ArgumentNullException">当指定的委托为 null 时抛出。</exception>
        /// <exception cref="InvalidOperationException">当指定的快捷键已被注册时抛出。</exception>
        /// <exception cref="System.ComponentModel.Win32Exception">当注册快捷键遇到 Windows API 返回错误时抛出。</exception>
        public void Register(HotKeyModifiers fsModifiers, Key key, Action handler)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            int vk = KeyInterop.VirtualKeyFromKey(key);
            int id = GetIDFromKeyCombination(fsModifiers, vk);

            try
            {
                handlers.Add(id, handler);
            }
            catch (ArgumentException) // ID already registered
            {
                throw new InvalidOperationException("This hot key has already been registered.");
            }

            if (!WinApi.RegisterHotKey(hwnd, id, fsModifiers, unchecked ((uint)vk)))
            {
                var e = new System.ComponentModel.Win32Exception();
                handlers.Remove(id);
                throw e;
            }
        }
Ejemplo n.º 6
0
        internal IHotKey RegisterHotKey(VirtualKey key, HotKeyModifiers modifiers = 0)
        {
            KeyValuePair <int, Win32HotKey> hotKeyKeyValuePair = Win32HotKey.RegisterHotKey(_windowHandleSource.Handle, key, modifiers);

            _registeredHotKeys.Add(hotKeyKeyValuePair.Key, hotKeyKeyValuePair.Value);

            return(hotKeyKeyValuePair.Value);
        }
Ejemplo n.º 7
0
 public Hotkey(Keys key, HotKeyModifiers modifiers, Action <Preset> func, Preset preset)
 {
     this.key       = key;
     this.modifiers = modifiers;
     this.func      = func;
     this.preset    = preset;
     Hotkeys.hotkeys.Add(this);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Check whether specified Hotkey modifier available or not from given HotKey
 /// </summary>
 /// <param name="key">HotKey</param>
 /// <param name="keyModifier">Key Modifier from Keys enum</param>
 /// <param name="hotKeyModifier">Hotkey modifier</param>
 /// <returns>Matched Hotkeymodifier</returns>
 private static HotKeyModifiers CheckModifier(Keys key, Keys keyModifier, HotKeyModifiers hotKeyModifier)
 {
     if ((key & keyModifier) == keyModifier)
     {
         return(hotKeyModifier);
     }
     return(HotKeyModifiers.None);
 }
Ejemplo n.º 9
0
        public void UnregisterHotKey(Keys key, HotKeyModifiers modifiers)
        {
            HotKeyData hkData = FindHotKey(key, modifiers);

            if (!hkData.Equals(HotKeyData.Empty)) {
                unregisterHotKey(hkData);
                hotkeys.Remove(hkData);
            }
        }
Ejemplo n.º 10
0
 public void UnregisterHotKey(Keys key, HotKeyModifiers modifiers)
 {
     HotKeyData hkData = FindHotKey(key, modifiers);
     if (!hkData.Equals(HotKeyData.Empty))
     {
         unregisterHotKey(hkData);
         hotkeys.RemoveAt(hotkeys.FindIndex(hk => hk.AtomID_Equals(hkData)));
     }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// 从指定的快捷键组合计算唯一快捷键 ID。
 /// </summary>
 /// <param name="fsModifiers">指定快捷键的修饰键部分。</param>
 /// <param name="vk">指定快捷键的非修饰键部分的 Win32 虚拟键值。</param>
 /// <returns>快捷键 ID。</returns>
 /// <remarks>保证不同的快捷键组合与不同的快捷键 ID 一一对应。</remarks>
 static int GetIDFromKeyCombination(HotKeyModifiers fsModifiers, int vk)
 {
     // Valid ID range is [0x0000, 0xBFFF]
     // We are producing IDs within [0x0001, 0x0FFE]
     unchecked
     {
         return((((int)fsModifiers) << 8) | vk);
     }
 }
Ejemplo n.º 12
0
        // --------------------------------------------------------------------------
        /// <summary>
        /// Assign a key to a specific action.  Returns an id to allow you to stop
        /// listening to this key.
        /// </summary>
        // --------------------------------------------------------------------------
        public int ListenForHotKey(System.Windows.Input.Key key, HotKeyModifiers modifiers, Action doThis)
        {
            var formsKey = (Keys)KeyInterop.VirtualKeyFromKey(key);

            var hotkeyId = _idSeed++;

            _hotKeyActions[hotkeyId] = doThis;
            RegisterHotKey(_windowHandle, hotkeyId, (uint)modifiers, (uint)formsKey);
            return(hotkeyId);
        }
Ejemplo n.º 13
0
        public static void Register(HotKeyModifiers modifiers, Keys keys, EventHandler <KeyPressedEventArgs> handler)
        {
            Initialize();

            lock (SyncRoot)
            {
                Items.Add(new HotKey(modifiers, keys, handler));
                RegisterHotKey(IntPtr.Zero, Items.Count, modifiers, keys);
            }
        }
Ejemplo n.º 14
0
        public void UnregisterHotKey(Keys key, HotKeyModifiers modifiers)
        {
            HotKeyData hkData = FindHotKey(key, modifiers);

            if (!hkData.Equals(HotKeyData.Empty))
            {
                unregisterHotKey(hkData);
                hotkeys.Remove(hkData);
            }
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Removes the hot key.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="modifiers">The modifiers.</param>
 public void RemoveHotKey(Keys key, HotKeyModifiers modifiers)
 {
     if (registerHotKeys.Contains(new HotKeysData {
         Key = key, Modifiers = modifiers
     }, new HotKeyEqualityComparer()))
     {
         var data = registerHotKeys.Find(hk => hk.Key == key && hk.Modifiers == modifiers);
         this.UnregisterHotKeys(data);
         registerHotKeys.Remove(data);
     }
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Register the suppiled Hotkey
        /// </summary>
        /// <param name="winHandle">Handle for hotkey event receiver</param>
        /// <param name="id">Global atom id for given hotkey</param>
        /// <param name="key">HotKey</param>
        /// <returns>success flag of register action</returns>
        public static bool RegisterKey(System.Windows.Forms.Control winHandle, int id, Keys key)
        {
            HotKeyModifiers keyModifierNone = HotKeyModifiers.None;

            keyModifierNone |= CheckModifier(key, Keys.Control, HotKeyModifiers.Control);
            keyModifierNone |= CheckModifier(key, Keys.Alt, HotKeyModifiers.Alt);
            keyModifierNone |= CheckModifier(key, Keys.Shift, HotKeyModifiers.Shift);
            keyModifierNone |= CheckModifier(key, Keys.LWin, HotKeyModifiers.Windows);
            keyModifierNone |= CheckModifier(key, Keys.RWin, HotKeyModifiers.Windows);
            return(RegisterHotKey(winHandle.Handle, id, keyModifierNone, key & Keys.KeyCode));
        }
Ejemplo n.º 17
0
        private Win32HotKey(int id, IntPtr mainWindowHandle, VirtualKey key, HotKeyModifiers modifiers)
        {
            _id = id;
            _mainWindowHandle = mainWindowHandle;

            int registerResult = RegisterHotKey(mainWindowHandle, id, (uint)modifiers, (uint)key);

            if (registerResult == 0)
            {
                throw new InvalidOperationException("Did not manage to register specified Win32HotKey.");
            }
        }
Ejemplo n.º 18
0
        public static bool RegisterKey(IntPtr hwnd, int hotkeyId, Keys key)
        {
            HotKeyModifiers keyModifier = HotKeyModifiers.None;

            keyModifier |= CheckModifier(key, Keys.Control, HotKeyModifiers.Control);
            keyModifier |= CheckModifier(key, Keys.Alt, HotKeyModifiers.Alt);
            keyModifier |= CheckModifier(key, Keys.Shift, HotKeyModifiers.Shift);
            keyModifier |= CheckModifier(key, Keys.LWin, HotKeyModifiers.Windows);
            keyModifier |= CheckModifier(key, Keys.RWin, HotKeyModifiers.Windows);

            return(RegisterHotKey(hwnd, hotkeyId, keyModifier, key & Keys.KeyCode));
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Adds the hot key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="modifiers">The modifiers.</param>
        public void AddHotKey(Keys key, HotKeyModifiers modifiers)
        {
            if (this.registerHotKeys.Contains(
                    new HotKeysData {
                Key = key, Modifiers = modifiers
            },
                    new HotKeyEqualityComparer()))
            {
                return;
            }

            RegisterHotKeys(key, modifiers);
        }
Ejemplo n.º 20
0
        private HotKeyData FindHotKey(Keys key, HotKeyModifiers modifiers)
        {
            HotKeyData hkData;

            for (int i = 0; i < this.hotkeys.Count; i++)
            {
                hkData = (HotKeyData)hotkeys[i];
                if (hkData.Key == key && hkData.Modifiers == modifiers)
                {
                    return(hkData);
                }
            }

            return(HotKeyData.Empty);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// 注册快捷键
        /// </summary>
        /// <param name="hwnd">持有快捷键窗口的句柄</param>
        /// <param name="fsModifiers">组合键</param>
        /// <param name="key">快捷键</param>
        /// <param name="callBack">回调函数</param>
        public static bool TryRegist(IntPtr hwnd, HotKeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack)
        {
            var _hwndSource = HwndSource.FromHwnd(hwnd);

            _hwndSource.AddHook(WndProc);

            int id = _keyId++;

            var vk = KeyInterop.VirtualKeyFromKey(key);

            if (RegisterHotKey(hwnd, id, fsModifiers, (uint)vk))
            {
                _keyMap[id] = callBack;
                return(true);
            }
            return(false);
        }
Ejemplo n.º 22
0
 public void RegisterHotKey(Keys key, HotKeyModifiers modifiers)
 {
     bool accepted = false;
     while (!accepted)
     {
         if (!FindHotKey(key, modifiers).Equals(HotKeyData.Empty))
         {
             this.registerHotKey(key, modifiers);
             accepted = true;
         }
         else
         {
             this.UnregisterHotKey(key, modifiers);
             accepted = false;
         }
     }
 }
Ejemplo n.º 23
0
        public static bool Unregister(HotKeyModifiers modifiers, Keys keys)
        {
            Initialize();

            lock (SyncRoot)
            {
                for (int i = 0; i < Items.Count; i++)
                {
                    var item = Items[i];
                    if (item != null && item.Keys == keys && item.Modifiers == modifiers)
                    {
                        Items[i] = null;
                        return(UnregisterHotKey(IntPtr.Zero, i + 1) == 1);
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 24
0
        static bool RegisterHotKey(HotKey hk)
        {
            if (FindHotKeyAtom(hk) != 0)
            {
                return(true);
            }

            IntPtr atom     = GlobalAddAtom(Guid.NewGuid().ToString());
            int    hotKeyId = atom.ToInt32();

            if (hotKeyId == 0)
            {
                return(false);
            }

            HotKeyModifiers fsModifiers = 0;

            if (hk.CtrlKey)
            {
                fsModifiers |= HotKeyModifiers.MOD_CONTROL;
            }
            if (hk.ShiftKey)
            {
                fsModifiers |= HotKeyModifiers.MOD_SHIFT;
            }
            if (hk.AltKey)
            {
                fsModifiers |= HotKeyModifiers.MOD_ALT;
            }

            if (!RegisterHotKey(Global.Desktop.Handle, hotKeyId, (uint)fsModifiers, hk.KeyCode))
            {
                return(false);
            }

            lock (m_HotKeys)
            {
                m_HotKeys[hotKeyId] = hk;
            }

            return(true);
        }
Ejemplo n.º 25
0
        /// <summary>
        /// 为被管理的 Window 注销指定的此前注册的快捷键。
        /// </summary>
        /// <param name="fsModifiers">指定快捷键的修饰键部分。</param>
        /// <param name="key">指定快捷键的非修饰键部分。</param>
        /// <exception cref="InvalidOperationException">当指定的快捷键未被注册时抛出。</exception>
        /// <exception cref="System.ComponentModel.Win32Exception">当注销快捷键遇到错误时抛出。</exception>
        public void Unregister(HotKeyModifiers fsModifiers, Key key)
        {
            int vk = KeyInterop.VirtualKeyFromKey(key);
            int id = GetIDFromKeyCombination(fsModifiers, vk);

            if (!WinApi.UnregisterHotKey(hwnd, id))
            {
                var e = new System.ComponentModel.Win32Exception();
                if (e.NativeErrorCode == ERROR_HOTKEY_NOT_REGISTERED)
                {
                    throw new InvalidOperationException("This hot key has not been registered.");
                }
                else
                {
                    throw e;
                }
            }
            // Since WinAPI succeeds, this cannot fail:
            handlers.Remove(id);
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Registers the hot keys.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="modifiers">The modifiers.</param>
        private void RegisterHotKeys(Keys key, HotKeyModifiers modifiers)
        {
            var    atomName = string.Empty;
            IntPtr atomId;

            atomName = key.ToString() + modifiers.ToString() + Environment.TickCount.ToString();
            atomName = atomName.Substring(0, Math.Min(atomName.Length, 255));

            atomId = GlobalAddAtom(atomName);
            if (atomId == IntPtr.Zero)
            {
                throw new Exception("Unable to save shortcut atom !");
            }

            if (!RegisterHotKey(this.owner.Handle, atomId.ToInt32(), (int)modifiers, (int)key))
            {
                GlobalDeleteAtom(atomId);
                throw new Exception("Unable to save shortcut !");
            }

            this.registerHotKeys.Add(new HotKeysData(key, modifiers, atomId));
        }
Ejemplo n.º 27
0
        public Hotkey(Window handlerWindow, Action hotKeyHandler, Keys key, HotKeyModifiers modifiers)
        {
            m_onHotKeyPressed = hotKeyHandler;

            // Create a unique Id for this class in this instance
            string atomName = Thread.CurrentThread.ManagedThreadId.ToString("X8") + GetType().FullName;

            HotkeyID = Win32.GlobalAddAtom(atomName);

            // Set up the hook to listen for hot keys
            m_windowHandle = new WindowInteropHelper(handlerWindow).EnsureHandle();
            if (m_windowHandle == null)
            {
                throw new ApplicationException(
                          "Cannot find window handle. Try calling this on or after OnSourceInitialized()"
                          );
            }

            HwndSource source = HwndSource.FromHwnd(m_windowHandle);

            source.AddHook(HwndHook);

            ListenForHotKey(key, modifiers);
        }
Ejemplo n.º 28
0
 // --------------------------------------------------------------------------
 /// <summary>
 /// Tell what key you want to listen for.  Returns an id representing
 /// this particular key combination.  Use this in your handler to
 /// disambiguate what key was pressed.
 /// </summary>
 // --------------------------------------------------------------------------
 public uint ListenForHotKey(Keys key, HotKeyModifiers modifiers)
 {
     RegisterHotKey(_windowHandle, HotkeyID, (uint)modifiers, (uint)key);
     return((uint)modifiers | (((uint)key) << 16));
 }
Ejemplo n.º 29
0
 internal HotKeyBind(Shortcut shortcut, VirtualKey key, HotKeyModifiers modifiers)
 {
     Shortcut  = shortcut;
     Key       = key;
     Modifiers = modifiers;
 }
Ejemplo n.º 30
0
 public void RegisterHotKey(Keys key, HotKeyModifiers modifiers)
 {
     if (!FindHotKey(key, modifiers).Equals(HotKeyData.Empty)) {
         this.registerHotKey(key, modifiers);
     }
 }
Ejemplo n.º 31
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HotKeysData"/> struct.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="modifiers">The modifiers.</param>
 /// <param name="atomId">The atom id.</param>
 public HotKeysData(Keys key, HotKeyModifiers modifiers, IntPtr atomId)
 {
     this.Key       = key;
     this.Modifiers = modifiers;
     this.AtomID    = atomId;
 }
Ejemplo n.º 32
0
 private HotKeyData FindHotKey(Keys key, HotKeyModifiers modifiers)
 {
     for (int i = 0; i < hotkeys.Count; i++)
         if (hotkeys[i].Key == key && hotkeys[i].Modifiers == modifiers)
             return hotkeys[i];
     return HotKeyData.Empty;
 }
Ejemplo n.º 33
0
        private void registerHotKey(Keys key, HotKeyModifiers modifiers)
        {
            if (m.FATAL)
                return;

            string atomName = String.Empty;
            IntPtr atomId;
            atomName = key.ToString() + modifiers.ToString() + Environment.TickCount.ToString();

            if (atomName.Length > 255)
                atomName = atomName.Substring(0, 255);

            atomId = GlobalAddAtom(atomName);
            if (atomId == IntPtr.Zero)
                m.FatalAlert("L'atomId est invalide !", "Erreur", ToolTipIcon.Error);
            else
            {
                if (!RegisterHotKey(this.Handle, atomId.ToInt32(), (int)modifiers, (int)key))
                {
                    GlobalDeleteAtom(atomId);
                    m.FatalAlert("Impossible d'enregistrer le raccourci <" + key.ToString() + "+" + modifiers.ToString().Replace("Control", "Ctrl") + "> car celui-ci est déjà utilisé par une autre application..", "Erreur", ToolTipIcon.Error);
                }
                else
                    hotkeys.Add(new HotKeyData(key, modifiers, atomId));
            }
        }
Ejemplo n.º 34
0
 public HotKeyData(Keys key, HotKeyModifiers modifiers, IntPtr atomId)
 {
     this.Key = key;
     this.Modifiers = modifiers;
     this.AtomID = atomId;
 }
Ejemplo n.º 35
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HotKeysEventArgs"/> class.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="modifiers">The modifiers.</param>
 public HotKeysEventArgs(Keys key, HotKeyModifiers modifiers) {
     this.Key = key;
     this.Modifiers = modifiers;
 }
Ejemplo n.º 36
0
        private void registerHotKey(Keys key, HotKeyModifiers modifiers)
        {
            string atomName = string.Empty;
            IntPtr atomId;
            atomName = key.ToString() + modifiers.ToString() + Environment.TickCount.ToString();
            if (atomName.Length > 255) {
                atomName = atomName.Substring(0,255);
            }

            atomId = GlobalAddAtom(atomName);
            if (atomId == IntPtr.Zero) {
                throw new Exception("Impossible d'enregistrer l'atome du raccourci !");
            }

            if (!RegisterHotKey(this.Handle, atomId.ToInt32(), (int)modifiers, (int)key)) {
                GlobalDeleteAtom(atomId);
                throw new Exception("Impossible d'enregistrer le raccourci !");
            }

            this.hotkeys.Add(new HotKeyData(key, modifiers, atomId));
        }
Ejemplo n.º 37
0
 private static extern bool RegisterHotKey(IntPtr hWnd, int id, HotKeyModifiers fsModifiers, uint vk);
Ejemplo n.º 38
0
 public HotKey(string name, Keys keyCode, HotKeyModifiers modifiers)
 {
     Name      = name;
     KeyCode   = keyCode;
     Modifiers = modifiers;
 }
Ejemplo n.º 39
0
 internal KeyPressedEventArgs(HotKeyModifiers modifier, Keys key)
 {
     _modifier = modifier;
     _key      = key;
 }
Ejemplo n.º 40
0
        private HotKeyData FindHotKey(Keys key, HotKeyModifiers modifiers)
        {
            HotKeyData hkData;
            for (int i=0; i<this.hotkeys.Count; i++) {
                hkData = (HotKeyData)hotkeys[i];
                if (hkData.Key == key && hkData.Modifiers == modifiers) {
                    return hkData;
                }
            }

            return HotKeyData.Empty;
        }