Exemple #1
0
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            try
            {
                Keys KeyPressed = (Keys)wParam;

                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:
                    switch (KeyPressed)
                    {
                    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);

                        //case Keys.Back:
                        //    this.Text = Keys.None.ToString();
                        //    return IntPtr.Zero;
                    }

                    if (LocalModifier != ModifierKeys.None)
                    {
                        this.Text = HotKeyShared.CombineShortcut(LocalModifier, KeyPressed);
                    }
                    else
                    {
                        if (ForceModifiers)
                        {
                            this.Text      = "";
                            this.isFocused = false;
                            this.UpdateState();
                            System.Media.SystemSounds.Asterisk.Play();
                            MessageBox.Show("You have to specify a modifier like 'Control', 'Alt' or 'Shift'");
                        }
                        else
                        {
                            this.Text = KeyPressed.ToString();
                        }
                    }
                    return(IntPtr.Zero);;

                case KeyboardMessages.WmSyskeyup:
                case KeyboardMessages.WmKeyup:
                    if (!String.IsNullOrWhiteSpace(Text.Trim()) || this.Text != Keys.None.ToString())
                    {
                        if (HotKeyIsSetEvent != null)
                        {
                            var e = new HotKeyIsSetEventArgs(HotKeyIsSetEvent, UserKey, UserModifier);
                            base.RaiseEvent(e);
                            if (e.Cancel)
                            {
                                this.Text = "";
                                isFocused = false;
                                UpdateState();
                            }
                        }
                    }
                    return(IntPtr.Zero);
                }
            }
            catch (OverflowException) { }

            return(IntPtr.Zero);
        }
Exemple #2
0
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            try
            {
                Keys KeyPressed = (Keys)wParam;

                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:
                        switch (KeyPressed)
                        {
                            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;

                            //case Keys.Back:
                            //    this.Text = Keys.None.ToString();
                            //    return IntPtr.Zero;
                        }

                        if (LocalModifier != ModifierKeys.None)
                        {
                            this.Text = HotKeyShared.CombineShortcut(LocalModifier, KeyPressed);
                        }
                        else
                        {
                            if (ForceModifiers)
                            {
                                this.Text = "";
                                this.isFocused = false;
                                this.UpdateState();
                                System.Media.SystemSounds.Asterisk.Play();
                                MessageBox.Show("You have to specify a modifier like 'Control', 'Alt' or 'Shift'");
                            }
                            else
                            { this.Text = KeyPressed.ToString(); }
                        }
                        return IntPtr.Zero; ;

                    case KeyboardMessages.WmSyskeyup:
                    case KeyboardMessages.WmKeyup:
                        if (!String.IsNullOrWhiteSpace(Text.Trim()) || this.Text != Keys.None.ToString())
                        {
                            if (HotKeyIsSetEvent != null)
                            {
                                var e = new HotKeyIsSetEventArgs(HotKeyIsSetEvent, UserKey, UserModifier);
                                base.RaiseEvent(e);
                                if (e.Cancel)
                                {
                                    this.Text = "";
                                    isFocused = false;
                                    UpdateState();
                                }
                            }
                        }
                        return IntPtr.Zero;
                }
            }
            catch (OverflowException) { }

            return IntPtr.Zero;
        }