コード例 #1
0
ファイル: EventListener.cs プロジェクト: medmsyk/wendigocs
 /// <summary>
 /// Listen for device event.
 /// </summary>
 /// <param name="state">Device state.</param>
 /// <param name="oldState">Old device state.</param>
 private void Listen(DeviceState state, DeviceState oldState)
 {
     if (deviceEvent.IsKeyEvent())
     {
         ListenKeyEvent(deviceEvent, state, oldState);
     }
     else if (deviceEvent.IsMouseEvent())
     {
         ListenMouseEvent(deviceEvent, state, oldState);
     }
 }
コード例 #2
0
ファイル: EventImitator.cs プロジェクト: medmsyk/wendigocs
        /// <summary>
        /// Play device events.
        /// </summary>
        /// <param name="path">File path.</param>
        /// <param name="startKeys">Keys to start playing.</param>
        /// <param name="stopKeys">Keys to stop playing.</param>
        public static void Play(string path, Keys[] startKeys, Keys[] stopKeys)
        {
            string eventName = EventBuilder.GetName(new string[] { nameof(EventImitator), "play", "{0}", path });

            EventDispatcher.KeyPress(
                String.Format(eventName, "start"), startKeys, (e) => {
                // Do not raise exceptions in this callback.
                lock (playerLocker) {
                    if (!File.Exists(path))
                    {
                        Notify($"Not found {path}");
                        return;
                    }
                    if (recorders.ContainsKey(path))
                    {
                        Notify($"Now recording to {path}");
                        return;
                    }
                    if (players.Contains(path))
                    {
                        Notify($"Already playing {path}");
                        return;
                    }

                    Notify($"Started playing {path}");
                    players.Add(path);
                }

                long timestamp       = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                List <Keys> downKeys = new List <Keys>();
                int lineNo           = 1;

                using (StreamReader reader = new StreamReader(path)) {
                    while (reader.Peek() != -1)
                    {
                        if (!players.Contains(path))
                        {
                            break;                                 // Stopped
                        }
                        string line = reader.ReadLine();
                        if (line == "")
                        {
                            continue;
                        }

                        try {
                            string[] values = line.Split(',');
                            long elapsed    = long.Parse(values[0]);

                            int diff = (int)(elapsed - (DateTimeOffset.Now.ToUnixTimeMilliseconds() - timestamp));
                            if (diff > 0)
                            {
                                Thread.Sleep(diff);
                            }

                            DeviceEvent deviceEvent = (DeviceEvent)Enum.Parse(typeof(DeviceEvent), values[1]);

                            if (deviceEvent.IsKeyEvent())
                            {
                                Keys key = (Keys)Enum.Parse(typeof(Keys), values[2]);

                                if (deviceEvent == DeviceEvent.KeyDown)
                                {
                                    if (!downKeys.Contains(key))
                                    {
                                        downKeys.Add(key);
                                    }
                                    EventSimulator.KeyDown(key);
                                }
                                else if (deviceEvent == DeviceEvent.KeyUp)
                                {
                                    if (downKeys.Contains(key))
                                    {
                                        downKeys.Remove(key);
                                    }
                                    EventSimulator.KeyUp(key);
                                }
                                else if (deviceEvent == DeviceEvent.KeyPress)
                                {
                                    if (downKeys.Contains(key))
                                    {
                                        downKeys.Remove(key);
                                    }
                                    EventSimulator.KeyPress(key);
                                }
                            }
                            else if (deviceEvent.IsMouseEvent())
                            {
                                int x = int.Parse(values[2]);
                                int y = int.Parse(values[3]);

                                if (deviceEvent == DeviceEvent.MouseMove)
                                {
                                    EventSimulator.Point(new Point(x, y), true);
                                }
                                else if (deviceEvent == DeviceEvent.MouseWheel)
                                {
                                    EventSimulator.Wheel(y);
                                }
                                else if (deviceEvent == DeviceEvent.MouseTilt)
                                {
                                    EventSimulator.Tilt(x);
                                }
                            }
                        } catch {
                            Notify($"Failed to parse line {lineNo}.\n{path}: {line}");
                            break;
                        }

                        lineNo += 1;
                    }
                }

                // Release all pressed keys.
                foreach (Keys key in downKeys)
                {
                    EventSimulator.KeyUp(key);
                }

                lock (playerLocker) {
                    if (players.Contains(path))
                    {
                        Notify($"Finished playing {path}");
                        players.Remove(path);
                    }
                }
            }, forSystem: true
                );
            EventDispatcher.KeyPress(
                String.Format(eventName, "stop"), stopKeys, (e) => {
                // Do not raise exceptions in this callback.
                lock (playerLocker) {
                    if (players.Contains(path))
                    {
                        Notify($"Stopped playing {path}");
                        players.Remove(path);
                    }
                }
            }, forSystem: true
                );
        }