Example #1
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>
        /// 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.");
         }
     }
 }
 /// <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);
        }
Example #6
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();
         }
     }
 }
Example #7
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);
            }
        }
Example #8
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);
            }
        }