void _Thread() { _tid = AThread.NativeId; AHookWin hookK = null, hookM = null; if (_usedEvents.Has(UsedEvents.Keyboard)) { hookK = AHookWin.Keyboard(_KeyboardHookProc); //note: don't use lambda, because then very slow JIT on first hook event } if (_usedEvents.Has(UsedEvents.Mouse)) { hookM = AHookWin.MouseRaw_(_MouseHookProc); } if (_usedEvents.Has(UsedEvents.MouseEdgeMove)) { _emDetector = new MouseTriggers.EdgeMoveDetector_(); } //tested: don't need JIT-compiling. Api.SetEvent(_eventStartStop); while (Api.GetMessage(out var m) > 0) { Api.DispatchMessage(m); } //AOutput.Write("hooks thread ended"); hookK?.Dispose(); hookM?.Dispose(); _emDetector = null; Api.SetEvent(_eventStartStop); }
void _ThreadProc() { AHookWin hk = null, hm = null; AHookAcc hwe = null; try { try { if (_block.Has(BIEvents.Keys)) { hk = AHookWin.Keyboard(_keyHookProc ??= _KeyHookProc); } if (_block.HasAny(BIEvents.MouseClicks | BIEvents.MouseMoving)) { hm = AHookWin.Mouse(_mouseHookProc ??= _MouseHookProc); } } catch (AuException e1) { ADebug.Print(e1); _block = 0; return; } //failed to hook //This prevents occassional inserting a foreign key after the first our-script-pressed key. //To reproduce, let our script send small series of chars in loop, and simultaneously a foreign script send other chars. ATime.DoEvents(); //AOutput.Write("started"); Api.SetEvent(_syncEvent); //the acc hook detects Ctrl+Alt+Del, Win+L, UAC consent, etc. SystemEvents.SessionSwitch only Win+L. try { hwe = new AHookAcc(AccEVENT.SYSTEM_DESKTOPSWITCH, 0, _winEventProc ??= _WinEventProc); } catch (AuException e1) { ADebug.Print(e1); } //failed to hook AWaitFor.Wait_(-1, WHFlags.DoEvents, _stopEvent, _threadHandle); if (_blockedKeys != null) { bool onlyUp = _discardBlockedKeys || ATime.WinMilliseconds - _startTime > c_maxResendTime; _blockedKeys.SendBlocked_(onlyUp); } //AOutput.Write("ended"); } finally { _blockedKeys = null; hk?.Dispose(); hm?.Dispose(); hwe?.Dispose(); Api.SetEvent(_syncEvent); } GC.KeepAlive(this); }