private void doWork() { //Create hotkey sequence (action means a delegate without parameter and return void, each action pointer to a method) List <Action> hotkeys = new List <Action>(); if (chk1.Checked) { hotkeys.Add(() => KeyMouseEvents.keyPress(System.Windows.Forms.Keys.D1, 0, hwnd)); } if (chk2.Checked) { hotkeys.Add(() => KeyMouseEvents.keyPress(System.Windows.Forms.Keys.D2, 0, hwnd)); } if (chk3.Checked) { hotkeys.Add(() => KeyMouseEvents.keyPress(System.Windows.Forms.Keys.D3, 0, hwnd)); } if (chk4.Checked) { hotkeys.Add(() => KeyMouseEvents.keyPress(System.Windows.Forms.Keys.D4, 0, hwnd)); } if (chkLeft.Checked) { hotkeys.Add(() => KeyMouseEvents.mouseLClick()); } if (chkRight.Checked) { hotkeys.Add(() => KeyMouseEvents.mouseLClick()); } while (true) { hotkeys[rndGenerator.Next(hotkeys.Count)].Invoke(); //Generate a delay factor between 0.5-1.0 Thread.Sleep((int)(rndGenerator.Next(5, 11) / 10 * delay)); } }
protected override void WndProc(ref System.Windows.Forms.Message m) { try { if (m.Msg == WM_HOTKEY) { switch (m.WParam.ToInt32()) { case 1: if (chkHotkeyFun.Checked) { hwnd = KeyMouseEvents.GetForegroundWindow(); KeyMouseEvents.keyPress(System.Windows.Forms.Keys.F1, 0, hwnd); } if (isRunning) { return; } hwnd = WindowsInfo.FindWindow("D3 Main Window Class", null); if (hwnd == IntPtr.Zero) { MessageBox.Show("找不到Diablo 3視窗"); return; } if (hwnd != WindowsInfo.GetForegroundWindow()) { MessageBox.Show("Diablo 3並不是前景視窗"); return; } if (!chk1.Checked && !chk2.Checked && !chk3.Checked && !chk4.Checked && !chkLeft.Checked && !chkRight.Checked) { MessageBox.Show("請選擇按鍵"); return; } isRunning = true; Work = new System.Threading.Thread(this.doWork); Detector = new System.Threading.Thread(this.detection); Work.Start(); Detector.Start(); break; case 2: if (chkHotkeyFun.Checked) { hwnd = KeyMouseEvents.GetForegroundWindow(); KeyMouseEvents.keyPress(System.Windows.Forms.Keys.F2, 0, hwnd); } if (isRunning) { Work.Abort(); Detector.Abort(); //release keyboard and mouse if (chk1.Checked) { KeyMouseEvents.keyPress(System.Windows.Forms.Keys.D1, 0, hwnd); } if (chk2.Checked) { KeyMouseEvents.keyPress(System.Windows.Forms.Keys.D2, 0, hwnd); } if (chk3.Checked) { KeyMouseEvents.keyPress(System.Windows.Forms.Keys.D3, 0, hwnd); } if (chk4.Checked) { KeyMouseEvents.keyPress(System.Windows.Forms.Keys.D4, 0, hwnd); } if (chkLeft.Checked) { KeyMouseEvents.mouseLClick(); } if (chkRight.Checked) { KeyMouseEvents.mouseRClick(); } isRunning = false; } break; } } } catch (Exception ex) { MessageBox.Show(ex.StackTrace + ": " + ex.Message); } finally { base.WndProc(ref m); } }