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

            SetTheme();

            // 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();
            }
        }
Esempio n. 2
0
        public static void SendPasteKey()
        {
            var shiftKey = new KeyboardKey(Keys.ShiftKey);
            var altKey   = new KeyboardKey(Keys.Alt);
            var ctrlKey  = new KeyboardKey(Keys.ControlKey);
            var vKey     = new KeyboardKey(Keys.V);

            // Before injecting a paste command, first make sure that no modifiers are already
            // being pressed (which will throw off the Ctrl+v).
            // Since key state is notoriously unreliable, set a max sleep so that we don't get stuck
            var maxSleep = 250;

            // minimum sleep time
            Thread.Sleep(150);

            while (maxSleep > 0 && (shiftKey.State != 0 || altKey.State != 0 || ctrlKey.State != 0))
            {
                Thread.Sleep(maxSleep);
                maxSleep -= 50;
            }

            // Press keys in sequence. Don't use PressAndRelease since that seems to be too fast for most applications and the sequence gets lost.
            ctrlKey.Press();
            vKey.Press();
            Thread.Sleep(25);
            vKey.Release();
            Thread.Sleep(25);
            ctrlKey.Release();
        }
Esempio n. 3
0
        private void ActivateWindow()
        {
            KeyboardKey altKey        = new KeyboardKey(Keys.Alt);
            bool        altKeyPressed = false;

            if ((altKey.AsyncState & 0x8000) == 0)
            {
                altKey.Press();
                altKeyPressed = true;
            }

            Context.API.ShowApp();

            if (altKeyPressed)
            {
                altKey.Release();
            }
        }
Esempio n. 4
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);
            }
        }
Esempio n. 5
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).Sleep(30);
                }

                // Control
                if (settings.Control)
                {
                    simulator.Keyboard.KeyDown(VirtualKeyCode.LCONTROL).Sleep(30);
                }

                // Alt
                if (settings.Alt)
                {
                    simulator.Keyboard.KeyDown(VirtualKeyCode.LMENU).Sleep(30);
                }

                // Shift
                if (settings.Shift)
                {
                    simulator.Keyboard.KeyDown(VirtualKeyCode.LSHIFT).Sleep(30);
                }

                // 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).Sleep(30);
                }

                // Release Alt
                if (settings.Alt)
                {
                    simulator.Keyboard.KeyUp(VirtualKeyCode.LMENU).Sleep(30);
                }

                // Release Control
                if (settings.Control)
                {
                    simulator.Keyboard.KeyUp(VirtualKeyCode.LCONTROL).Sleep(30);
                }

                // Release Windows
                if (settings.Windows)
                {
                    simulator.Keyboard.KeyUp(VirtualKeyCode.LWIN).Sleep(30);
                }
            }
        }
Esempio n. 6
0
        private void SendShortcutKeys(HotKeySettings settings)
        {
            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();
                List <VirtualKeyCode> modifiedKeys = new List <VirtualKeyCode>();
                List <VirtualKeyCode> keys         = new List <VirtualKeyCode>();

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

                        var key = (VirtualKeyCode)k;
                        keys.Add(key);
                    }
                }

                if (settings.Windows)
                {
                    modifiedKeys.Add(VirtualKeyCode.LWIN);
                }
                if (settings.Control)
                {
                    modifiedKeys.Add(VirtualKeyCode.LCONTROL);
                }
                if (settings.Alt)
                {
                    modifiedKeys.Add(VirtualKeyCode.LMENU);
                }
                if (settings.Shift)
                {
                    modifiedKeys.Add(VirtualKeyCode.LSHIFT);
                }

                if (modifiedKeys.Count == 0)
                {
                    if (keys.Count != 0)
                    {
                        simulator.Keyboard.KeyPress(keys.ToArray()).Sleep(30);
                    }
                }
                else
                {
                    if (keys.Count != 0)
                    {
                        simulator.Keyboard.ModifiedKeyStroke(modifiedKeys, keys).Sleep(30);
                    }
                    else
                    {
                        simulator.Keyboard.KeyPress(modifiedKeys.ToArray()).Sleep(30);
                    }
                }
            }
        }
Esempio n. 7
0
        private bool AdjustVolume(VolumeSettings settings)
        {
            if (settings == null)
            {
                return(false);
            }

            try
            {
                InputSimulator simulator = new InputSimulator();
                int            t         = settings.Percent / 2;

                switch ((Method)settings.Method)
                {
                case Method.VolumeUp:
                    for (int i = 0; i < t; i++)
                    {
                        simulator.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.VOLUME_UP);
                    }
                    break;

                case Method.VolumeDown:
                    for (int i = 0; i < t; i++)
                    {
                        simulator.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.VOLUME_DOWN);
                    }
                    break;

                case Method.Mute:
                    simulator.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.VOLUME_MUTE);
                    break;
                }

                return(true);
            }
            catch
            {
                int t = settings.Percent / 2;

                switch ((Method)settings.Method)
                {
                case Method.VolumeUp:
                    var volumeUpKey = new KeyboardKey(Keys.VolumeUp);
                    for (int i = 0; i < t; i++)
                    {
                        volumeUpKey.Press();
                        Thread.Sleep(3);
                        volumeUpKey.Release();
                    }
                    break;

                case Method.VolumeDown:
                    var volumeDownKey = new KeyboardKey(Keys.VolumeDown);
                    for (int i = 0; i < t; i++)
                    {
                        volumeDownKey.Press();
                        Thread.Sleep(3);
                        volumeDownKey.Release();
                    }
                    break;

                case Method.Mute:
                    var muteKey = new KeyboardKey(Keys.VolumeMute);
                    muteKey.PressAndRelease();
                    break;
                }
                return(false);
            }
        }
Esempio n. 8
0
 public static bool ResendByKeybdEvent(IEnumerable <Keys> modifierKeys, IEnumerable <Keys> keys)
 {
     try
     {
         if (modifierKeys != null && modifierKeys.Any())
         {
             foreach (var keyCode in modifierKeys)
             {
                 KeyboardKey keyboardKey = new KeyboardKey(keyCode);
                 if (!keyboardKey.IsGloballyPressed)
                 {
                     keyboardKey.Press();
                     Thread.Sleep(10);
                     if (!keyboardKey.IsGloballyPressed)
                     {
                         return(false);
                     }
                 }
             }
             if (keys != null && keys.Any())
             {
                 foreach (var keyCode in keys)
                 {
                     KeyboardKey keyboardKey = new KeyboardKey(keyCode);
                     keyboardKey.Press();
                     Thread.Sleep(10);
                     if (!keyboardKey.IsGloballyPressed)
                     {
                         return(false);
                     }
                     keyboardKey.Release();
                 }
             }
             foreach (var keyCode in modifierKeys)
             {
                 KeyboardKey keyboardKey = new KeyboardKey(keyCode);
                 keyboardKey.Release();
                 Thread.Sleep(10);
                 if (keyboardKey.IsGloballyPressed)
                 {
                     return(false);
                 }
             }
         }
         else if (keys != null && keys.Any())
         {
             foreach (var keyCode in keys)
             {
                 KeyboardKey keyboardKey = new KeyboardKey(keyCode);
                 keyboardKey.Press();
                 Thread.Sleep(10);
                 if (!keyboardKey.IsGloballyPressed)
                 {
                     return(false);
                 }
                 keyboardKey.Release();
             }
         }
     }
     catch (Exception)
     {
         return(false);
     }
     return(true);
 }