private void keyPlay() { var rand = new Random(Environment.TickCount); while (true) { Thread.Sleep(110); IntPtr hwndPtr = GetForegroundWindow(); // 10 seconds of hold off for user's event if ((Environment.TickCount - lastTimeRecorded) > 1000 * 10) { bKeyFlowPaused = false; } if (datePicker1.Value.Ticks < DateTime.Now.Ticks) { bKeyFlowStarted = false; } if (!bKeyFlowStarted || bKeyFlowPaused) { continue; } if (hwndPtr == thisWnd) { continue; } int winCount = Int16.Parse(txtWincount.Text); MKEvent mkevent = 0; if (events.Count > 0) { mkevent = events.Dequeue(); Thread.Sleep(rand.Next(100, 500)); if (mkevent != MKEvent.MOUSE_CLICK) { //continue; } int tmp = 0; switch (mkevent) { case MKEvent.MOUSE_CLICK: if (chkMousePos.Checked) { int mx = cx + rand.Next(80) - 40; int my = cy + rand.Next(80) - 40; Thread.Sleep(50); MouseSimulator.MouseMove2(mx, my); MouseSimulator.MouseDown(MouseButton.Left); //MouseSimulator.MouseMove(mx + 1, my + 1); Thread.Sleep(100); //MouseSimulator.MouseMove(mx - 100, my - 100); Thread.Sleep(100); //MouseSimulator.MouseMove(mx, my); Thread.Sleep(100); MouseSimulator.MouseUp(MouseButton.Left); } else { MouseSimulator.MouseMove2(rand.Next(this.cy * 2), rand.Next(this.cx * 2)); Thread.Sleep(150); MouseSimulator.MouseMove2(pinPos.X, pinPos.Y); MouseSimulator.MouseDown(MouseButton.Left); Thread.Sleep(150); MouseSimulator.MouseUp(MouseButton.Left); } break; case MKEvent.LINE_UP: { KeyboardSimulator.KeyPress(Keys.Up); } break; case MKEvent.LINE_DOWN: { KeyboardSimulator.KeyPress(Keys.Down); } break; case MKEvent.PAGE_UP: { KeyboardSimulator.KeyPress(Keys.PageUp); } break; case MKEvent.PAGE_DOWN: { KeyboardSimulator.KeyPress(Keys.PageDown); } break; case MKEvent.SWITCH_TAB: tmp = rand.Next(0, 15); if (tmp > 1) { KeyboardSimulator.KeyDown(Keys.LControlKey); while (tmp-- > 0) { KeyboardSimulator.KeyPress(Keys.Tab); Thread.Sleep(100); } KeyboardSimulator.KeyUp(Keys.LControlKey); } break; case MKEvent.SWITCH_WIN: { tmp = rand.Next(0, Math.Max(winCount, 0)); // top limit is exclusive if (tmp > 0) { KeyboardSimulator.KeyDown(Keys.Alt); while (tmp-- > 0) { KeyboardSimulator.KeyPress(Keys.Tab); Thread.Sleep(100); } KeyboardSimulator.KeyUp(Keys.Alt); } } break; default: break; } } } }
/** * enque key events for one minute **/ private void enqueEvents() { events.Clear(); Random rand = new Random(); int tabCnt = rand.Next(1, 30); int kmax = (int)txtKmax.Value, kmin = (int)txtKmin.Value; int mmax = Int16.Parse(txtMmax.Text), mmin = Int16.Parse(txtMmin.Text); int keyCount = kmin + rand.Next((kmax - kmin) / 3) * 3; keyCount = rand.Next(kmin, kmax); if (kmin == 0 && keyCount < kmax * .2) { keyCount = 0; } int mouseCount = mmin + rand.Next((mmax - mmin) / 3) * 3; mouseCount = rand.Next(mmin, mmax); if (mmin == 0 && mouseCount < mmax * 0.05) { mouseCount = 0; } //MessageBox.Show(keyCount.ToString()); //MessageBox.Show(mouseCount.ToString()); // generate key queue // one queue is valid for one minute /** * down flow for 3 minutes, upflow for 3 minutes * page down : 5% * key down: 95% **/ MKEvent pgKey = 0, arrKey = 0; if (minuteCounter < 3) {// down flow pgKey = MKEvent.PAGE_DOWN; arrKey = MKEvent.LINE_DOWN; } else { pgKey = MKEvent.PAGE_UP; arrKey = MKEvent.LINE_UP; } // queue Cat play // 0th : events.Enqueue(arrKey); events.Enqueue(arrKey); // 1st : mouse click half times for (int i = 0; i < mouseCount / 2 + 1; i++) { events.Enqueue(MKEvent.MOUSE_CLICK); } // 2nd : pageup int tmp = rand.Next(1, 2); while (tmp-- > 0) { events.Enqueue(pgKey); } // 3rd : switch tab events.Enqueue(MKEvent.SWITCH_TAB); // 4th : siwtch win events.Enqueue(MKEvent.SWITCH_WIN); // 5th : keyboard half times for (int i = 0; i < keyCount / 2; i++) { events.Enqueue(arrKey); } // 6th : mouse click for 2nd half times for (int i = 0; i < mouseCount / 2 + 1; i++) { events.Enqueue(MKEvent.MOUSE_CLICK); } // 8th : keyboard half times 2nd for (int i = 0; i < keyCount / 2; i++) { events.Enqueue(arrKey); } }