Esempio n. 1
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. 2
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. 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).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. 4
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);
            }
        }