Exemplo n.º 1
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();
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
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);
            }
        }