/// <summary>
        /// Reset all modifier keys and remember in this object which modifier keys
        /// have been set.
        /// </summary>
        public LockKeyResetter()
        {
            for (int i = 0; i < MODIFIER_KEYS.Length; i++)
            {
                KeyboardKey k     = new KeyboardKey(MODIFIER_KEYS[i]);
                short       dummy = k.AsyncState; // reset remembered status
                if (k.AsyncState != 0)
                {
                    simpleModifiers[i] = true;
                    k.Release();
                }
            }
            KeyboardKey capslockKey   = new KeyboardKey(Keys.CapsLock);
            int         capslockstate = capslockKey.State;

            capslock = ((capslockstate & 0x01) == 0x01);
            if (capslock)
            {
                // press caps lock
                capslockKey.PressAndRelease();
                Application.DoEvents();
                if ((capslockKey.State & 0x01) == 0x01)
                {
                    // press shift
                    new KeyboardKey(Keys.ShiftKey).PressAndRelease();
                }
                Application.DoEvents();
                if ((capslockKey.State & 0x01) == 0x01)
                {
                    throw new Exception("Cannot disable caps lock.");
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// Reset all modifier keys and remember in this object which modifier keys
 /// have been set.
 /// </summary>
 public LockKeyResetter()
 {
     for (int i = 0; i < MODIFIER_KEYS.Length; i++)
     {
         KeyboardKey k = new KeyboardKey(MODIFIER_KEYS[i]);
         short dummy = k.AsyncState; // reset remembered status
         if (k.AsyncState != 0)
         {
             simpleModifiers[i] = true;
             k.Release();
         }
     }
     KeyboardKey capslockKey = new KeyboardKey(Keys.CapsLock);
     int capslockstate = capslockKey.State;
     capslock = ((capslockstate & 0x01) == 0x01);
     if (capslock)
     {
         // press caps lock
         capslockKey.PressAndRelease();
         Application.DoEvents();
         if ((capslockKey.State & 0x01) == 0x01)
         {
             // press shift
             new KeyboardKey(Keys.ShiftKey).PressAndRelease();
         }
         Application.DoEvents();
         if ((capslockKey.State & 0x01) == 0x01)
         {
             throw new Exception("Cannot disable caps lock.");
         }
     }
 }
 /// <summary>
 /// Set all modifier keys that have been set before. Since this class implements
 /// <see cref="IDisposable"/>, you can use the <c>using</c>
 /// keyword in C# to automatically set modifier keys when you have finished.
 /// </summary>
 public void Dispose()
 {
     if (capslock)
     {
         // press caps lock
         KeyboardKey capslockKey = new KeyboardKey(Keys.CapsLock);
         capslockKey.PressAndRelease();
         Application.DoEvents();
         if ((capslockKey.State & 0x01) != 0x01)
         {
             throw new Exception("Cannot enable caps lock.");
         }
     }
 }
Beispiel #4
0
 public WindowInformation(MainForm mf)
 {
     this.mf = mf;
     shiftKey = new KeyboardKey(Keys.ShiftKey);
     ctrlKey = new KeyboardKey(Keys.ControlKey);
     altKey = new KeyboardKey(Keys.Menu);
     InitializeComponent();
     menuButtons = new Button[] {
         menuButton1, menuButton2, menuButton3,
         menuButton4, menuButton5, menuButton6,
         menuButton7, menuButton8, menuButton9
     };
     clearCopied_Click(null, null);
     defaultPanel.BringToFront();
     menuPanel.BringToFront();
     listPanel.BringToFront();
     infoPanel.BringToFront();
     tmrUpdate.Enabled = true;
 }
 /// <summary>
 /// Set all modifier keys that have been set before. Since this class implements
 /// <see cref="IDisposable"/>, you can use the <c>using</c>
 /// keyword in C# to automatically set modifier keys when you have finished.
 /// </summary>
 public void Dispose()
 {
     if (capslock)
     {
         // press caps lock
         KeyboardKey capslockKey = new KeyboardKey(Keys.CapsLock);
         capslockKey.PressAndRelease();
         Application.DoEvents();
         if ((capslockKey.State & 0x01) != 0x01)
         {
             throw new Exception("Cannot enable caps lock.");
         }
     }
     for (int i = MODIFIER_KEYS.Length - 1; i >= 0; i--)
     {
         if (simpleModifiers[i])
         {
             new KeyboardKey(MODIFIER_KEYS[i]).Press();
         }
     }
 }
        /// <summary>
        /// Reset all modifier keys and remember in this object which modifier keys
        /// have been set.
        /// </summary>
        public LockKeyResetter()
        {
            KeyboardKey capslockKey   = new KeyboardKey(Keys.CapsLock);
            int         capslockstate = capslockKey.State;

            capslock = ((capslockstate & 0x01) == 0x01);
            if (capslock)
            {
                // press caps lock
                capslockKey.PressAndRelease();
                Application.DoEvents();
                if ((capslockKey.State & 0x01) == 0x01)
                {
                    // press shift
                    new KeyboardKey(Keys.ShiftKey).PressAndRelease();
                }
                Application.DoEvents();
                if ((capslockKey.State & 0x01) == 0x01)
                {
                    throw new Exception("Cannot disable caps lock.");
                }
            }
            Release(Keys.ShiftKey, Keys.ControlKey, Keys.Menu, Keys.LWin, Keys.RWin);
        }
Beispiel #7
0
 /// <summary>
 /// Set all modifier keys that have been set before. Since this class implements
 /// <see cref="IDisposable"/>, you can use the <c>using</c> 
 /// keyword in C# to automatically set modifier keys when you have finished.
 /// </summary>
 public void Dispose()
 {
     if (capslock) {
         // press caps lock
         KeyboardKey capslockKey = new KeyboardKey(Keys.CapsLock);
         capslockKey.PressAndRelease();
         Application.DoEvents();
         if ((capslockKey.State & 0x01) != 0x01)
             throw new Exception("Cannot enable caps lock.");
     }
     for (int i = MODIFIER_KEYS.Length-1; i >= 0; i--)
     {
         if (simpleModifiers[i])
         {
             new KeyboardKey(MODIFIER_KEYS[i]).Press();
         }
     }
 }
Beispiel #8
0
        private static void SendStroke(string stroke, StrokeBehavior behavior)
        {
            stroke = stroke.ToLower();

            Keys key = Keys.None;
            switch (stroke)
            {
                case "alt":
                    key = Keys.Menu;
                    break;
                case "ctrl":
                    key = Keys.ControlKey;
                    break;
                case "shift":
                    key = Keys.ShiftKey;
                    break;
                case "win":
                    key = Keys.LWin;
                    break;
                default:
                    int num = 0;
                    string code = stroke;
                    if (int.TryParse(code, out num))
                    {
                        code = "d" + num;
                    }
                    try
                    {
                        key = (Keys)Enum.Parse(typeof(Keys), code, true);
                    }
                    catch { }
                    break;
            }
            if (key != Keys.None)
            {
                KeyboardKey k = new KeyboardKey(key);
                switch (behavior)
                {
                    case StrokeBehavior.Press:
                        k.Press();
                        break;
                    case StrokeBehavior.Release:
                        k.Release();
                        break;
                    case StrokeBehavior.PressAndRelease:
                        k.PressAndRelease();
                        break;
                }
            }
            else if (stroke.Length >= 2)
            {
                string head = stroke.Substring(0, 2);

                KeyboardKey.MouseAbsolutePos = false;
                switch (head)
                {
                    case "mv":
                        if (stroke.Contains("%"))
                        {
                            stroke = stroke.Replace("%", "");
                            KeyboardKey.MouseAbsolutePos = true;
                        }
                        string[] xy = stroke.Substring(2).Trim('(', ')').Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        ptMouse.X = Double.Parse(xy[0]);
                        ptMouse.Y = double.Parse(xy[1]);
                        if (KeyboardKey.MouseAbsolutePos) { ptMouse.X *= 655.35; ptMouse.Y *= 655.35; }
                        KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.Move, ptMouse, 0, 0);
                        break;
                    case "ml":
                        switch (behavior)
                        {
                            case StrokeBehavior.Press:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.LeftDown, ptMouse, 0, 0);
                                break;
                            case StrokeBehavior.Release:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.LeftUp, ptMouse, 0, 0);
                                break;
                            case StrokeBehavior.PressAndRelease:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.LeftDown, ptMouse, 0, 0);
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.LeftUp, ptMouse, 0, 0);
                                break;
                        }
                        break;
                    case "mr":
                        switch (behavior)
                        {
                            case StrokeBehavior.Press:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.RightDown, ptMouse, 0, 0);
                                break;
                            case StrokeBehavior.Release:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.RightUp, ptMouse, 0, 0);
                                break;
                            case StrokeBehavior.PressAndRelease:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.RightDown, ptMouse, 0, 0);
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.RightUp, ptMouse, 0, 0);
                                break;
                        }
                        break;
                    case "mm":
                        switch (behavior)
                        {
                            case StrokeBehavior.Press:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.MiddleDown, ptMouse, 0, 0);
                                break;
                            case StrokeBehavior.Release:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.MiddleUp, ptMouse, 0, 0);
                                break;
                            case StrokeBehavior.PressAndRelease:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.MiddleDown, ptMouse, 0, 0);
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.MiddleUp, ptMouse, 0, 0);
                                break;
                        }
                        break;
                    case "mw":
                        int delta = int.Parse(stroke.Substring(2).Trim('(', ')'));
                        KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.Wheel, ptMouse, delta, 0);
                        break;
                    case "m1":
                    case "m2":
                        int x = int.Parse(head.Substring(1, 1));
                        switch (behavior)
                        {
                            case StrokeBehavior.Press:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.XDown, ptMouse, x, 0);
                                break;
                            case StrokeBehavior.Release:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.XUp, ptMouse, x, 0);
                                break;
                            case StrokeBehavior.PressAndRelease:
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.XDown, ptMouse, x, 0);
                                KeyboardKey.InjectMouseEvent(KeyboardKey.MouseFlag.XUp, ptMouse, x, 0);
                                break;
                        }
                        break;

                    default:
                        throw new Exception(Archer.Resource.Exception_StrokeFailed);
                }
            }
            else
            {
                Core.Report(Archer.Resource.Exception_StrokeFailed);
            }
        }
        private void HotkeyPreview_OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            // The text box grabs all input
            e.Handled = true;

            // Fetch the actual shortcut key
            var key = (e.Key == Key.System ? e.SystemKey : e.Key);

            // Ignore modifier keys
            if (key == Key.LeftShift || key == Key.RightShift
                || key == Key.LeftCtrl || key == Key.RightCtrl
                || key == Key.LeftAlt || key == Key.RightAlt
                || key == Key.LWin || key == Key.RWin)
            {
                return;
            }

            var previewHotkeyModel = new HotkeyViewModel();
            previewHotkeyModel.Ctrl = (Keyboard.Modifiers & ModifierKeys.Control) != 0;
            previewHotkeyModel.Shift = (Keyboard.Modifiers & ModifierKeys.Shift) != 0;
            previewHotkeyModel.Alt = (Keyboard.Modifiers & ModifierKeys.Alt) != 0;

            var winLKey = new KeyboardKey(Keys.LWin);
            var winRKey = new KeyboardKey(Keys.RWin);
            previewHotkeyModel.Windows = (winLKey.State & 0x8000) == 0x8000 || (winRKey.State & 0x8000) == 0x8000;
            previewHotkeyModel.KeyCode = key;

            var previewText = previewHotkeyModel.ToString();

            // Jump to the next element if the user presses only the Tab key
            if (previewText == "Tab")
            {
                ((UIElement)sender).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                return;
            }

            HotkeyPreview.Text = previewText;
            _hotkeyViewModel = previewHotkeyModel;
        }
Beispiel #10
0
        private void ActivateAndFocusMainWindow()
        {
            // What happens below looks a bit weird, but for Switcheroo to get focus when using the Alt+Tab hook,
            // it is needed to simulate an Alt keypress will bring Switcheroo to the foreground. Otherwise Switcheroo
            // will become the foreground window, but the previous window will retain focus, and receive keep getting
            // the keyboard input.
            // http://www.codeproject.com/Tips/76427/How-to-bring-window-to-top-with-SetForegroundWindo

            var thisWindowHandle = new WindowInteropHelper(this).Handle;
            var thisWindow = new AppWindow(thisWindowHandle);

            var altKey = new KeyboardKey(Keys.Alt);
            var altKeyPressed = false;

            // Press the Alt key if it is not already being pressed
            if ((altKey.AsyncState & 0x8000) == 0)
            {
                altKey.Press();
                altKeyPressed = true;
            }

            // Bring the Switcheroo window to the foreground
            Show();
            SystemWindow.ForegroundWindow = thisWindow;
            Activate();

            // Release the Alt key if it was pressed above
            if (altKeyPressed)
            {
                altKey.Release();
            }
        }
Beispiel #11
0
        private void SendShortcutKeys(HotKeySettings settings)
        {
            if (settings == null)
                return;
            if (settings.Windows &&
              settings.KeyCode.Count != 0 && settings.KeyCode[0] == Keys.L)
            {
                LockWorkStation();
                return;
            }

            if (settings.SendByKeybdEvent)
            {

                // Create keyboard keys to represent hot key combinations
                KeyboardKey winKey = new KeyboardKey(Keys.LWin);
                KeyboardKey controlKey = new KeyboardKey(Keys.LControlKey);
                KeyboardKey altKey = new KeyboardKey(Keys.LMenu);
                KeyboardKey shiftKey = new KeyboardKey(Keys.LShiftKey);

                // Deceide which keys to press
                // Windows
                if (settings.Windows)
                    winKey.Press();

                // Control
                if (settings.Control)
                    controlKey.Press();

                // Alt
                if (settings.Alt)
                    altKey.Press();

                // Shift
                if (settings.Shift)
                    shiftKey.Press();

                // Modifier
                if (settings.KeyCode != null)
                    foreach (var k in settings.KeyCode)
                    {
                        KeyboardKey modifierKey = new KeyboardKey(k);
                        if (!String.IsNullOrEmpty(modifierKey.KeyName))
                            modifierKey.PressAndRelease();
                    }
                // Release Shift
                if (settings.Shift)
                    shiftKey.Release();

                // Release Alt
                if (settings.Alt)
                    altKey.Release();

                // Release Control
                if (settings.Control)
                    controlKey.Release();

                // Release Windows
                if (settings.Windows)
                    winKey.Release();
            }
            else
            {

                InputSimulator simulator = new InputSimulator();

                // Deceide which keys to press
                // Windows
                if (settings.Windows)
                    simulator.Keyboard.KeyDown(VirtualKeyCode.LWIN);

                // Control
                if (settings.Control)
                    simulator.Keyboard.KeyDown(VirtualKeyCode.LCONTROL);

                // Alt
                if (settings.Alt)
                    simulator.Keyboard.KeyDown(VirtualKeyCode.LMENU);

                // Shift
                if (settings.Shift)
                    simulator.Keyboard.KeyDown(VirtualKeyCode.LSHIFT);

                // Modifier
                if (settings.KeyCode != null)
                    foreach (var k in settings.KeyCode)
                    {
                        if (!Enum.IsDefined(typeof(VirtualKeyCode), k.GetHashCode())) continue;

                        var key = (VirtualKeyCode)k;
                        simulator.Keyboard.KeyPress(key).Sleep(30);
                    }
                // Release Shift
                if (settings.Shift)
                    simulator.Keyboard.KeyUp(VirtualKeyCode.LSHIFT);

                // Release Alt
                if (settings.Alt)
                    simulator.Keyboard.KeyUp(VirtualKeyCode.LMENU);

                // Release Control
                if (settings.Control)
                    simulator.Keyboard.KeyUp(VirtualKeyCode.LCONTROL);

                // Release Windows
                if (settings.Windows)
                    simulator.Keyboard.KeyUp(VirtualKeyCode.LWIN);
            }
        }