static void perform(int action, Keys key) { if (!Working) { return; } if (ExcludedKeys.Contains(key)) { return; } var fore = Win32ApiWrapper.GetForegroundWindow(); if (!MainWindows.Any(w => w.SameOrBelongTo(fore))) { return; } WorkingNotice?.BeginInvoke(fore, null, null); for (var i = AllWindows.Count - 1; i >= 0; i--) { var item = AllWindows[i]; var w = item.Key; if (w.SameOrBelongTo(fore)) { continue; } var r = Win32ApiWrapper.PostMessage(w, action, (int)key, 0); if (r == 0) { AllWindows.Remove(item); WindowLost?.BeginInvoke(w, null, null); } } }
/// <summary> /// Starts to sychronize keyboard events, will be failed if <see cref="MainWindows"/> is null or empty. /// </summary> public static void Start() { if (Working) { return; } Collect(); MainWindows = AllWindows.Where(w => w.Value).Select(w => w.Key); Working = MainWindows.Any(); if (!Working) { FailToStart?.BeginInvoke(null, null); return; } Started?.BeginInvoke(null, null); }