Example #1
0
        /// <summary>
        /// 模拟键盘操作
        /// </summary>
        public static void SimulateKeyBehavior(IList <HookKeyEventArgs> KeyHisDataList)
        {
            //----遍历历史记录,重新执行一遍-----
            var lastHis     = KeyHisDataList.First();
            var hisDataList = KeyHisDataList.ToList();

            foreach (var hookKeyEventArgs in hisDataList)
            {
                var timeDiff = hookKeyEventArgs.EventTimeStamp - lastHis.EventTimeStamp;
                Thread.Sleep(timeDiff);//休眠
                switch (hookKeyEventArgs.InputKeyType)
                {
                case KeyType.KeyDown:
                    KeyboardSimulator.KeyDown(hookKeyEventArgs.Key);
                    break;

                case KeyType.KeyUp:
                    KeyboardSimulator.KeyUp(hookKeyEventArgs.Key);
                    break;

                case KeyType.None:
                    //处理字符串连贯输入
                    var handle   = WindowHWND.ChildWindowFromPoint();
                    var encoding = Encoding.GetEncoding("GB2312");
                    WindowHWND.InputString(handle, hookKeyEventArgs.InputString, encoding);
                    break;
                }
                lastHis = hookKeyEventArgs;
            }
        }
Example #2
0
 /// <summary>
 /// 测试功能
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void TestMenuItem_OnClick(object sender, RoutedEventArgs e)
 {
     for (int i = 0; i < 5; i++)
     {
         Thread.Sleep(5000);
         var handle = WindowHWND.ChildWindowFromPoint();
         Console.WriteLine(string.Format("ChildWindowFromPoint:{0}", handle));
         var encoding = Encoding.GetEncoding("GB2312");
         WindowHWND.InputString(handle, "WM_CHAR:this is sparta!", encoding);
         WindowHWND.InputString(handle, "WM_CHAR:这就是斯巴达!", encoding);
         WindowHWND.InputMessage(handle, "WM_SETTEXT:斯巴达克斯。");
     }
 }
Example #3
0
        /// <summary>
        /// 连贯模拟鼠标和键盘
        /// </summary>
        public static void SimulateAll(IList <HookMouseEventArgs> MouseHisDataList, IList <HookKeyEventArgs> KeyHisDataList)
        {
            Dictionary <object, DateTime> allEvents = new Dictionary <object, DateTime>();

            foreach (var hookMouseEventArgse in MouseHisDataList)
            {
                allEvents.Add(hookMouseEventArgse, hookMouseEventArgse.EventTimeStamp);
            }
            foreach (var hookKeyEventArgse in KeyHisDataList)
            {
                allEvents.Add(hookKeyEventArgse, hookKeyEventArgse.EventTimeStamp);
            }
            var orderedEventsList = allEvents.OrderBy(x => x.Value).ToList();

            if (orderedEventsList.Count == 0)
            {
                return;
            }
            var lastOpTime = orderedEventsList.First().Value;//上一次操作时间

            foreach (var keyValuePair in orderedEventsList)
            {
                var timeDiff = keyValuePair.Value - lastOpTime;
                Thread.Sleep(timeDiff);//休眠
                if (keyValuePair.Key is HookMouseEventArgs)
                {
                    var hookMouseEventArgse = keyValuePair.Key as HookMouseEventArgs;
                    MouseSimulator.Position = hookMouseEventArgse.MousePosition;
                    switch (hookMouseEventArgse.MouseEventType)
                    {
                    case MouseEventType.MouseDown:
                        MouseSimulator.MouseDown(hookMouseEventArgse.ClickButton);
                        UIThread.Invoke(() =>
                        {
                            ShowMouseIndicator(MouseIndicatorWinView, hookMouseEventArgse.MousePosition, new SolidColorBrush(Color.FromRgb(255, 0, 0)));
                        });
                        break;

                    case MouseEventType.MouseUp:
                        MouseSimulator.MouseUp(hookMouseEventArgse.ClickButton);
                        UIThread.Invoke(() =>
                        {
                            ShowMouseIndicator(MouseIndicatorWinView, hookMouseEventArgse.MousePosition, new SolidColorBrush(Color.FromRgb(26, 58, 246)));
                        });
                        break;

                    case MouseEventType.MouseWheel:
                        MouseSimulator.MouseWheel(hookMouseEventArgse.MouseWheelDelta);
                        break;

                    case MouseEventType.MouseMove:
                        MouseSimulator.MouseMove(hookMouseEventArgse.MousePosition.X, hookMouseEventArgse.MousePosition.Y, true);
                        break;
                    }
                }
                else if (keyValuePair.Key is HookKeyEventArgs)
                {
                    var hookKeyEventArgs = keyValuePair.Key as HookKeyEventArgs;
                    switch (hookKeyEventArgs.InputKeyType)
                    {
                    case KeyType.KeyDown:
                        KeyboardSimulator.KeyDown(hookKeyEventArgs.Key);
                        break;

                    case KeyType.KeyUp:
                        KeyboardSimulator.KeyUp(hookKeyEventArgs.Key);
                        break;

                    case KeyType.None:
                        //处理字符串连贯输入
                        var handle   = WindowHWND.ChildWindowFromPoint();
                        var encoding = Encoding.GetEncoding("GB2312");
                        WindowHWND.InputString(handle, hookKeyEventArgs.InputString, encoding);
                        Debug.WriteLine(string.Format("Handle:{0} Encoding:{1} Content:{2}", handle, encoding.BodyName, hookKeyEventArgs.InputString));
                        break;
                    }
                }
                lastOpTime = keyValuePair.Value;
            }
        }