public bool PreFilterMessage(ref Message m)
        {
            if (!Enabled) { return false; }

            //Check if the form that the HotKeyManager is registered to is inactive.
            if (DisableOnManagerFormInactive)
                if (Form.ActiveForm != null && this.ManagerForm != Form.ActiveForm) { return false; }

            //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;

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

            switch ((KeyboardMessages)m.Msg)
            {
                case (KeyboardMessages.WmSyskeydown):
                case (KeyboardMessages.WmKeydown):
                    Keys keydownCode = (Keys)(int)m.WParam & Keys.KeyCode;

                    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 true;
                        }

                        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;
                            return true;
                        }

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

                    //Check for a LocalHotKey.
                    LocalHotKey KeyDownHotkey = LocalHotKeyContainer.Find
                        (
                        delegate(LocalHotKey d)
                        {
                            return ((d.Key == keydownCode) && (d.Modifier == LocalModifier)
                                && (d.WhenToRaise == RaiseLocalEvent.OnKeyDown));
                        }
                    );

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

                        return KeyDownHotkey.SuppressKeyPress;
                    }

                    //Check for ChordHotKeys.
                    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 true;
                    }

                    InChordMode = false;
                    return false;

                case (KeyboardMessages.WmSyskeyup):
                case (KeyboardMessages.WmKeyup):
                    Keys keyupCode = (Keys)(int)m.WParam & Keys.KeyCode;

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

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

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

                        return KeyUpHotkey.SuppressKeyPress;
                    }
                    return false;

                case (KeyboardMessages.WmHotKey):
                    //var lpInt = (int)m.LParam;
                    //Keys Key = (Keys)((lpInt >> 16) & 0xFFFF);
                    //Modifiers modifier = (Modifiers)(lpInt & 0xFFFF);

                    int Id = (int)m.WParam;

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

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

                    return true;

                default: return false;
            }
        }
Example #2
0
        public bool PreFilterMessage(ref Message m)
        {
            if (!Enabled)
            {
                return(false);
            }

            //Check if the form that the HotKeyManager is registered to is inactive.
            if (DisableOnManagerFormInactive)
            {
                if (Form.ActiveForm != null && this.ManagerForm != Form.ActiveForm)
                {
                    return(false);
                }
            }

            //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;

            Modifiers LocalModifier = Modifiers.None;

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

            switch ((KeyboardMessages)m.Msg)
            {
            case (KeyboardMessages.WmSyskeydown):
            case (KeyboardMessages.WmKeydown):
                Keys keydownCode = (Keys)(int)m.WParam & Keys.KeyCode;

                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(true);
                    }

                    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;
                        return(true);
                    }

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

                //Check for a LocalHotKey.
                LocalHotKey KeyDownHotkey = LocalHotKeyContainer.Find
                                            (
                    delegate(LocalHotKey d)
                {
                    return((d.Key == keydownCode) && (d.Modifier == LocalModifier) &&
                           (d.WhenToRaise == RaiseLocalEvent.OnKeyDown));
                }
                                            );

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

                    return(KeyDownHotkey.SuppressKeyPress);
                }

                //Check for ChordHotKeys.
                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(true);
                }

                InChordMode = false;
                return(false);

            case (KeyboardMessages.WmSyskeyup):
            case (KeyboardMessages.WmKeyup):
                Keys keyupCode = (Keys)(int)m.WParam & Keys.KeyCode;

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

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

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

                    return(KeyUpHotkey.SuppressKeyPress);
                }
                return(false);

            case (KeyboardMessages.WmHotKey):
                //var lpInt = (int)m.LParam;
                //Keys Key = (Keys)((lpInt >> 16) & 0xFFFF);
                //Modifiers modifier = (Modifiers)(lpInt & 0xFFFF);

                int Id = (int)m.WParam;

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

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

                return(true);

            default: return(false);
            }
        }
 void MyHotKeyManager_ChordStarted(object sender, PreChordHotKeyEventArgs e)
 {
     LogEvents("Chord Started... (" + e.Info() + ") waiting for the second key of chord.");
 }