コード例 #1
0
        public async Task SendMouseActions(string actions)
        {
            var mouseActions = new List <IMouseAction>();

            foreach (Match m in MouseActionsRegex.Matches(actions))
            {
                if (m.Groups.ContainsKey("Button") && !string.IsNullOrWhiteSpace(m.Groups["ButtonValue"].Value))
                {
                    var mouseclick = new MouseClick()
                    {
                        Button    = m.Groups["ButtonValue"].Value,
                        Direction = ClickDirection.PressAndRelease,
                        Duration  = AverageClickDuration,
                    };

                    if (m.Groups.ContainsKey("Direction"))
                    {
                        switch (m.Groups["Direction"].Value.ToLowerInvariant())
                        {
                        case "press":
                        case "down":
                            mouseclick.Direction = ClickDirection.Press;
                            mouseclick.Duration  = Duration.Infinite;
                            break;

                        case "release":
                        case "up":
                            mouseclick.Direction = ClickDirection.Release;
                            mouseclick.Duration  = Duration.Infinite;
                            break;

                        default:
                            mouseclick.Direction = ClickDirection.PressAndRelease;
                            mouseclick.Duration  = AverageClickDuration;
                            break;
                        }
                    }

                    if (m.Groups.ContainsKey("Repeats") && int.TryParse(m.Groups["Repeats"].Value, out int repeats))
                    {
                        for (int i = 0; i < Math.Abs(repeats); i++)
                        {
                            mouseActions.Add(mouseclick);
                        }
                    }
                    else
                    {
                        mouseActions.Add(mouseclick);
                    }
                }
                else if (m.Groups.ContainsKey("Move") && !string.IsNullOrWhiteSpace(m.Groups["XValue"].Value) && !string.IsNullOrWhiteSpace(m.Groups["YValue"].Value))
                {
                    short xVal = 0;
                    short yVal = 0;
                    if (short.TryParse(m.Groups["XValue"].Value, out short x))
                    {
                        xVal = x;
                    }
                    if (short.TryParse(m.Groups["YValue"].Value, out short y))
                    {
                        yVal = y;
                    }
                    var mouseMove = new MouseMove()
                    {
                        X = xVal,
                        Y = yVal
                    };
                    mouseActions.Add(mouseMove);
                }
                else if (m.Groups.ContainsKey("Scroll") && !string.IsNullOrWhiteSpace(m.Groups["ScrollValue"].Value))
                {
                    var mouseScroll = new MouseScroll()
                    {
                        Amount = sbyte.Parse(m.Groups["ScrollValue"].Value),
                    };
                    mouseActions.Add(mouseScroll);
                }
                else if (m.Groups.ContainsKey("Tilt") && !string.IsNullOrWhiteSpace(m.Groups["TiltValue"].Value))
                {
                    var mouseTilt = new MouseTilt()
                    {
                        Amount = sbyte.Parse(m.Groups["TiltValue"].Value),
                    };
                    mouseActions.Add(mouseTilt);
                }
            }

            // Now that we have the mouse actions, press them.
            foreach (var mouseAction in mouseActions)
            {
                switch (mouseAction)
                {
                case MouseClick mouseClick:
                    await SendMouseClick(mouseClick.Button, mouseClick.Direction, mouseClick.Duration);

                    break;

                case MouseMove mouseMove:
                    SendMouseMove(mouseMove.X, mouseMove.Y);
                    break;

                case MouseScroll mouseScroll:
                    SendMouseScroll(mouseScroll.Amount);
                    break;

                case MouseTilt mouseTilt:
                    SendMouseTilt(mouseTilt.Amount);
                    break;
                }
            }
        }
コード例 #2
0
 public async Task SendMouseClick(MouseClick mouseClick)
 {
     await Mouse.SendMouseClick(mouseClick.Button, mouseClick.Direction, mouseClick.Duration);
 }