示例#1
0
 /// <summary>Creates a ChordHotKey object.
 /// </summary>
 /// <param name="name">The unique identifier for this ChordHotKey.</param>
 /// <param name="basekey">The key to start the chord.</param>
 /// <param name="basemodifier">The modifier associated with the base key.</param>
 /// <param name="ChordHotKey">The LocalHotKey object to extract the chord key and modifier from.</param>
 /// <param name="enabled">Specifies that the hotkey is active,</param>
 public ChordHotKey(string name, ModifierKeys basemodifier, Keys basekey, LocalHotKey ChordHotKey, bool enabled)
 {
     Name          = name;
     BaseKey       = basekey;
     BaseModifier  = basemodifier;
     ChordKey      = ChordHotKey.Key;
     chordmodifier = ChordHotKey.Modifier;
     Enabled       = enabled;
 }
示例#2
0
 /// <summary>Unregisters a LocalHotKey.
 /// </summary>
 /// <param name="hotKey">The hotKey to be removed</param>
 /// <returns>True if success, otherwise false</returns>
 public bool RemoveLocalHotKey(LocalHotKey hotKey)
 {
     if (LocalHotKeyContainer.Remove(hotKey) == true)
     {
         --LocalHotKeyCount; return(true);
     }
     else
     {
         return(false);
     }
 }
示例#3
0
        /// <summary>Removes the hotkey(Local, Chord or Global) with the specified name.
        /// </summary>
        /// <param name="name">The name of the hotkey.</param>
        /// <returns>True if successful and false otherwise.</returns>
        public bool RemoveHotKey(string name)
        {
            LocalHotKey local = System.Linq.Enumerable.Where
                                    (LocalHotKeyContainer, item => item.Name == name).FirstOrDefault();

            //LocalHotKey local = LocalHotKeyContainer.Find
            //    (
            //    delegate(LocalHotKey l)
            //    {
            //        return (l.Name == name);
            //    }
            //);

            if (local != null)
            {
                return(RemoveLocalHotKey(local));
            }

            ChordHotKey chord = ChordHotKeyContainer.Where(item => item.Name == name).FirstOrDefault();

            //ChordHotKey chord = ChordHotKeyContainer.Find
            //    (
            //    delegate(ChordHotKey c)
            //    {
            //        return (c.Name == name);
            //    }
            //);

            if (chord != null)
            {
                return(RemoveChordHotKey(chord));
            }

            GlobalHotKey global = GlobalHotKeyContainer.Where(item => item.Name == name).FirstOrDefault();

            //GlobalHotKey global = GlobalHotKeyContainer.Find
            //    (
            //    delegate(GlobalHotKey g)
            //    {
            //        return (g.Name == name);
            //    }
            //);

            if (global != null)
            {
                return(RemoveGlobalHotKey(global));
            }

            return(false);
        }
示例#4
0
        /// <summary>Checks if a HotKey has been registered.
        /// </summary>
        /// <param name="name">The name of the HotKey.</param>
        /// <returns>True if the HotKey has been registered, false otherwise.</returns>
        public bool HotKeyExists(string name)
        {
            LocalHotKey local = LocalHotKeyContainer.Where(item => item.Name == name).FirstOrDefault();

            //LocalHotKey local = LocalHotKeyContainer.Find
            //    (
            //    delegate(LocalHotKey l)
            //    {
            //        return (l.Name == name);
            //    }
            //);

            if (local != null)
            {
                return(true);
            }

            ChordHotKey chord = ChordHotKeyContainer.Where(item => item.Name == name).FirstOrDefault();

            //ChordHotKey chord = ChordHotKeyContainer.Find
            //    (
            //    delegate(ChordHotKey c)
            //    {
            //        return (c.Name == name);
            //    }
            //);

            if (chord != null)
            {
                return(true);
            }

            GlobalHotKey global = GlobalHotKeyContainer.Where(item => item.Name == name).FirstOrDefault();

            //GlobalHotKey global = GlobalHotKeyContainer.Find
            //    (
            //    delegate(GlobalHotKey g)
            //    {
            //        return (g.Name == name);
            //    }
            //);

            if (global != null)
            {
                return(true);
            }

            return(false);
        }
示例#5
0
        /// <summary>Checks if this Hotkey is equal to another ChordHotkey or LocalHotkey.
        /// </summary>
        /// <param name="obj">The Hotkey to compare</param>
        /// <returns>True if equal, false otherwise.</returns>
        public override bool Equals(object obj)
        {
            LocalHotKey lhotKey = obj as LocalHotKey;

            if (lhotKey != null)
            {
                return(Equals(lhotKey));
            }

            ChordHotKey hotkey = obj as ChordHotKey;

            if (hotkey != null)
            {
                return(Equals(hotkey));
            }

            return(false);
        }
示例#6
0
        /// <summary>Registers a LocalHotKey.
        /// </summary>
        /// <param name="hotKey">The hotKey which will be added. Must not be null and can be registered only once.</param>
        /// <exception cref="HotKeyAlreadyRegisteredException">thrown if a LocalHotkey with the same name and or key and modifier has already been added.</exception>
        public bool AddLocalHotKey(LocalHotKey hotKey)
        {
            if (hotKey == null)
            {
                if (!this.SuppressException)
                {
                    throw new ArgumentNullException("value");
                }

                return(false);
            }
            if (hotKey.Key == 0)
            {
                if (!this.SuppressException)
                {
                    throw new ArgumentNullException("value.Key");
                }

                return(false);
            }

            //Check if a chord already has its BaseKey and BaseModifier.
            bool ChordExits = ChordHotKeyContainer.Exists
                              (
                delegate(ChordHotKey f)
            {
                return(f.BaseKey == hotKey.Key && f.BaseModifier == hotKey.Modifier);
            }
                              );

            if (LocalHotKeyContainer.Contains(hotKey) || ChordExits)
            {
                if (!this.SuppressException)
                {
                    throw new HotKeyAlreadyRegisteredException("HotKey already registered!", hotKey);
                }

                return(false);
            }

            LocalHotKeyContainer.Add(hotKey);
            ++LocalHotKeyCount;
            return(true);
        }
示例#7
0
 /// <summary>Compares this HotKey to another LocalHotKey.
 /// </summary>
 /// <param name="other">The LocalHotKey to compare.</param>
 /// <returns>True if equal, false otherwise.</returns>
 public bool Equals(LocalHotKey other)
 {
     return(BaseKey == other.Key && BaseModifier == other.Modifier);
 }
示例#8
0
 /// <summary>Creates a ChordHotKey object.
 /// </summary>
 /// <param name="name">The unique identifier for this ChordHotKey.</param>
 /// <param name="basekey">The key to start the chord.</param>
 /// <param name="basemodifier">The modifier associated with the base key.</param>
 /// <param name="ChordHotKey">The LocalHotKey object to extract the chord key and modifier from.</param>
 public ChordHotKey(string name, ModifierKeys basemodifier, Keys basekey, LocalHotKey ChordHotKey) :
     this(name, basemodifier, basekey, ChordHotKey, true)
 {
 }
示例#9
0
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (!Enabled)
            {
                return(IntPtr.Zero);
            }

            //For LocalHotKeys, determine if modifiers Alt, Shift and Control is pressed.
            Microsoft.VisualBasic.Devices.Keyboard UserKeyBoard = new Microsoft.VisualBasic.Devices.Keyboard();
            bool AltPressed     = UserKeyBoard.AltKeyDown;
            bool ControlPressed = UserKeyBoard.CtrlKeyDown;
            bool ShiftPressed   = UserKeyBoard.ShiftKeyDown;

            ModifierKeys LocalModifier = ModifierKeys.None;

            if (AltPressed)
            {
                LocalModifier = ModifierKeys.Alt;
            }
            if (ControlPressed)
            {
                LocalModifier |= ModifierKeys.Control;
            }
            if (ShiftPressed)
            {
                LocalModifier |= ModifierKeys.Shift;
            }

            switch ((KeyboardMessages)msg)
            {
            case (KeyboardMessages.WmSyskeydown):
            case (KeyboardMessages.WmKeydown):
                Keys keydownCode = (Keys)(int)wParam;

                if (KeyPressEvent != null)
                {
                    KeyPressEvent(this, new HotKeyEventArgs(keydownCode, LocalModifier, RaiseLocalEvent.OnKeyDown));
                }

                //Check if a chord has started.
                if (InChordMode)
                {
                    //Check if the Key down is a modifier, we'll have to wait for a real key.
                    switch (keydownCode)
                    {
                    case Keys.Control:
                    case Keys.ControlKey:
                    case Keys.LControlKey:
                    case Keys.RControlKey:
                    case Keys.Shift:
                    case Keys.ShiftKey:
                    case Keys.LShiftKey:
                    case Keys.RShiftKey:
                    case Keys.Alt:
                    case Keys.Menu:
                    case Keys.LMenu:
                    case Keys.RMenu:
                    case Keys.LWin:
                        return(IntPtr.Zero);
                    }

                    ChordHotKey ChordMain = ChordHotKeyContainer.Where(
                        item => (item.BaseKey == PreChordKey && item.BaseModifier == PreChordModifier &&
                                 item.ChordKey == keydownCode && item.ChordModifier == LocalModifier))
                                            .FirstOrDefault();

                    //    ChordHotKey ChordMain = ChordHotKeyContainer.Find
                    //    (
                    //    delegate(ChordHotKey cm)
                    //    {
                    //        return ((cm.BaseKey == PreChordKey) && (cm.BaseModifier == PreChordModifier) && (cm.ChordKey == keydownCode) && (cm.ChordModifier == LocalModifier));
                    //    }
                    //);

                    if (ChordMain != null)
                    {
                        ChordMain.RaiseOnHotKeyPressed();

                        if (ChordPressed != null && ChordMain.Enabled == true)
                        {
                            ChordPressed(this, new ChordHotKeyEventArgs(ChordMain));
                        }

                        InChordMode = false;
                        new Microsoft.VisualBasic.Devices.Computer().Audio.PlaySystemSound(System.Media.SystemSounds.Exclamation);
                        return(IntPtr.Zero);
                    }

                    InChordMode = false;
                    return(IntPtr.Zero);
                }

                //Check for a LocalHotKey.
                LocalHotKey KeyDownHotkey = (from items in LocalHotKeyContainer
                                             where items.Key == keydownCode && items.Modifier == LocalModifier
                                             where items.WhenToRaise == RaiseLocalEvent.OnKeyDown
                                             select items).FirstOrDefault();

                //LocalHotKey KeyDownHotkey = LocalHotKeyContainer.Find
                //    (
                //    delegate(LocalHotKey d)
                //    {
                //        return ((d.Key == keydownCode) && (d.Modifier == LocalModifier));
                //    }
                //);

                if (KeyDownHotkey != null)
                {
                    KeyDownHotkey.RaiseOnHotKeyPressed();
                    if (LocalHotKeyPressed != null && KeyDownHotkey.Enabled == true)
                    {
                        LocalHotKeyPressed(this, new LocalHotKeyEventArgs(KeyDownHotkey));
                    }

                    return(IntPtr.Zero);
                }

                //Check for ChordHotKeys.
                ChordHotKey ChordBase = System.Linq.Enumerable.Where(
                    ChordHotKeyContainer, item => item.BaseKey == keydownCode && item.BaseModifier == LocalModifier)
                                        .FirstOrDefault();

                //ChordHotKey ChordBase = ChordHotKeyContainer.Find
                //    (
                //    delegate(ChordHotKey c)
                //    {
                //        return ((c.BaseKey == keydownCode) && (c.BaseModifier == LocalModifier));
                //    }
                //);

                if (ChordBase != null)
                {
                    PreChordKey      = ChordBase.BaseKey;
                    PreChordModifier = ChordBase.BaseModifier;

                    var e = new PreChordHotKeyEventArgs(new LocalHotKey(ChordBase.Name, ChordBase.BaseModifier, ChordBase.BaseKey));
                    if (ChordStarted != null)
                    {
                        ChordStarted(this, e);
                    }


                    InChordMode = !e.HandleChord;
                    return(IntPtr.Zero);
                }

                InChordMode = false;
                return(IntPtr.Zero);

            case (KeyboardMessages.WmSyskeyup):
            case (KeyboardMessages.WmKeyup):
                Keys keyupCode = (Keys)(int)wParam;

                if (KeyPressEvent != null)
                {
                    KeyPressEvent(this, new HotKeyEventArgs(keyupCode, LocalModifier, RaiseLocalEvent.OnKeyDown));
                }

                LocalHotKey KeyUpHotkey = (from items in LocalHotKeyContainer
                                           where items.Key == keyupCode && items.Modifier == LocalModifier
                                           where items.WhenToRaise == RaiseLocalEvent.OnKeyUp
                                           select items).FirstOrDefault();

                //LocalHotKey KeyUpHotkey = LocalHotKeyContainer.Find
                //    (
                //    delegate(LocalHotKey u)
                //    {
                //        return ((u.Key == keyupCode) && (u.Modifier == LocalModifier));
                //    }
                //);

                if (KeyUpHotkey != null)
                {
                    KeyUpHotkey.RaiseOnHotKeyPressed();
                    if (LocalHotKeyPressed != null && KeyUpHotkey.Enabled == true)
                    {
                        LocalHotKeyPressed(this, new LocalHotKeyEventArgs(KeyUpHotkey));
                    }

                    return(IntPtr.Zero);
                }

                return(IntPtr.Zero);

            case KeyboardMessages.WmHotKey:

                GlobalHotKey Pressed = GlobalHotKeyContainer.Where(item => item.Id == (int)wParam).FirstOrDefault();

                //GlobalHotKey Pressed = GlobalHotKeyContainer.Find
                //    (
                //    delegate(GlobalHotKey g)
                //    {
                //        return (g.Id == (int)wParam);
                //    }
                //);

                Pressed.RaiseOnHotKeyPressed();
                if (GlobalHotKeyPressed != null)
                {
                    GlobalHotKeyPressed(this, new GlobalHotKeyEventArgs(Pressed));
                }
                break;
            }

            return(IntPtr.Zero);
        }
示例#10
0
 public HotKeyAlreadyRegisteredException(string message, LocalHotKey hotKey, Exception inner) : base(message, inner)
 {
     LocalKey = hotKey;
 }
示例#11
0
 public PreChordHotKeyEventArgs(LocalHotKey hotkey)
 {
     HotKey = hotkey;
 }
示例#12
0
 public LocalHotKeyEventArgs(LocalHotKey hotKey)
 {
     HotKey = hotKey;
 }