/// <summary> /// Detects a key or key combination and triggers the corresponding action. /// </summary> /// <param name="source"> /// An instance of Global or Application hook. Use <see cref="Hook.GlobalEvents" /> or <see cref="Hook.AppEvents" /> to /// create it. /// </param> /// <param name="map"> /// This map contains the list of key combinations mapped to corresponding actions. You can use a dictionary initilizer /// to easily create it. /// Whenever a listed combination will be detected a corresponding action will be triggered. /// </param> /// <param name="reset"> /// This optional action will be executed when some key was pressed but it was not part of any wanted combinations. /// </param> public static void OnCXCombination(this IKeyboardEvents source, string key, Dictionary <string, Action> map, Action reset = null) { source.KeyDown += (sender, e) => { Action action = reset; if (e.KeyCode.ToString() == key) { var state = KeyboardState.GetCurrent(); var hotkeyString = string.Empty; if (state.IsDown(Keys.Control)) { hotkeyString += "Control+"; } if (state.IsDown(Keys.Shift)) { hotkeyString += "Shift+"; } if (state.IsDown(Keys.Alt)) { hotkeyString += "Alt+"; } hotkeyString += e.KeyCode.ToString(); map.TryGetValue(hotkeyString, out action); } action?.Invoke(); }; }
public static void Subscribe() { globalHook = Hook.GlobalEvents(); globalHook.KeyPress += GlobalHook_KeyPress; var altN = Combination.FromString("Alt+N"); Action nextEpisode = () => { Functions.NextEpisode(Main.Default, Main.Default.epsList); }; var altP = Combination.FromString("Alt+P"); Action prevEpisode = () => { Functions.PrevEpisode(Main.Default, Main.Default.epsList); }; var altQ = Combination.FromString("Alt+Q"); Action quit = () => { Main.Default.QuitApp(); }; var keyCombs = new Dictionary <Combination, Action> { { altN, nextEpisode }, { altP, prevEpisode }, { altQ, quit } }; Hook.GlobalEvents().OnCombination(keyCombs); }
/// <summary> /// Detects a key or key combination and triggers the corresponding action. /// </summary> /// <param name="source"> /// An instance of Global or Application hook. Use <see cref="Hook.GlobalEvents" /> or <see cref="Hook.AppEvents" /> to /// create it. /// </param> /// <param name="map"> /// This map contains the list of key combinations mapped to corresponding actions. You can use a dictionary initilizer /// to easily create it. /// Whenever a listed combination will be detected a corresponding action will be triggered. /// </param> /// <param name="reset"> /// This optional action will be executed when some key was pressed but it was not part of any wanted combinations. /// </param> public static void OnCombination(this IKeyboardEvents source, IEnumerable <KeyValuePair <Combination, Action> > map, Action reset = null) { var watchlists = map.GroupBy(k => k.Key.TriggerKey) .ToDictionary(g => g.Key, g => g.ToArray()); source.KeyDown += (sender, e) => { var found = watchlists.TryGetValue(e.KeyCode, out KeyValuePair <Combination, Action>[] element); if (!found) { reset?.Invoke(); return; } var state = KeyboardState.GetCurrent(); var action = reset; var maxLength = 0; foreach (var current in element) { var matches = current.Key.Chord.All(state.IsDown); if (!matches) { continue; } if (maxLength > current.Key.ChordLength) { continue; } maxLength = current.Key.ChordLength; action = current.Value; } action?.Invoke(); }; }
public static IObservable <KeyCode> KeyUpObservable(this IKeyboardEvents source) { return(Observable .FromEventPattern <InputEventArgs <KeyInput> >(source, "KeyDown") .Select(ep => ep.EventArgs.Data.Key) ); }
/// <summary> /// Detects a key or key combination sequence and triggers the corresponding action. /// </summary> /// <param name="source"> /// An instance of Global or Application hook. Use <see cref="Hook.GlobalEvents" /> or /// <see cref="Hook.AppEvents" /> to create it. /// </param> /// <param name="map"> /// This map contains the list of sequences mapped to corresponding actions. You can use a dictionary initilizer to /// easily create it. /// Whenever a listed sequnce will be detected a corresponding action will be triggered. If two or more sequences match /// the longest one will be used. /// Example: sequences may A,B,C and B,C might be detected simultanously if user pressed first A then B then C. In this /// case only action corresponding /// to 'A,B,C' will be triggered. /// </param> public static void OnSequence(this IKeyboardEvents source, IEnumerable <KeyValuePair <Sequence, Action> > map) { var actBySeq = map.ToArray(); var endsWith = new Func <Queue <Combination>, Sequence, bool>((chords, sequence) => { var skipCount = chords.Count - sequence.Length; return(skipCount >= 0 && chords.Skip(skipCount).SequenceEqual(sequence)); }); var max = actBySeq.Select(p => p.Key).Max(c => c.Length); var min = actBySeq.Select(p => p.Key).Min(c => c.Length); var buffer = new Queue <Combination>(max); var wrapMap = actBySeq.SelectMany(p => p.Key).Select(c => new KeyValuePair <Combination, Action>(c, () => { buffer.Enqueue(c); if (buffer.Count > max) { buffer.Dequeue(); } if (buffer.Count < min) { return; } //Invoke action corresponding to the longest matching sequence actBySeq .Where(pair => endsWith(buffer, pair.Key)) .OrderBy(pair => pair.Key.Length) .Select(pair => pair.Value) .LastOrDefault() ?.Invoke(); })); OnCombination(source, wrapMap, buffer.Clear); }
internal static void Init(IKeyboardEvents platformImplementation) { implementation = platformImplementation; implementation.KeyboardHeightChanged += (double height) => { KeyboardHeightChanged?.Invoke(height); }; }
private void UnregisterHookHandlers(IKeyboardEvents hook) { hook.KeyDown -= hook_KeyDown; hook.KeyPress -= hook_KeyPress; hook.KeyUp -= hook_KeyUp; }
public static IObservable <KeyEvent> UpDownEvents(this IKeyboardEvents source) { return(source .KeyDownObservable() .Select(key => key.Down()) .Merge(source .KeyUpObservable() .Select(key => key.Down()))); }
private void RegisterHookHandlers(IKeyboardEvents hook) { //if (_configuration.LogKeyDown) hook.KeyDown += hook_KeyDown; hook.KeyPress += hook_KeyPress; //if (_configuration.LogKeyUp) hook.KeyUp += hook_KeyUp; }
private void SubscribeHooks() { globalMouseHook = Hook.GlobalEvents(); globalKeyboardHook = Hook.GlobalEvents(); globalMouseHook.MouseWheelExt += GlobalHookMouseWheel; globalMouseHook.MouseDownExt += GlobalHookMouseDown; globalKeyboardHook.KeyDown += GlobalHookKeyDown; globalKeyboardHook.KeyUp += GlobalHookKeyUp; }
public bool ConnectToDevice() { try { _hook = Hook.GlobalEvents(); return(true); } catch (Exception ex) { Log?.Invoke(this, new Core.LogMessage(Core.LogLevel.Error, LogTags.Connection, "Failed to connect to device", ex)); ConnectionError?.Invoke(this, ex); } return(false); }
private void Form1_Load(object sender, EventArgs e) { _mComboBoxLayout.Items.Clear(); _mComboBoxLayout.Items.Add(ITEM_BLACK_WIDOW); _mComboBoxLayout.Items.Add(ITEM_BLADE); _mComboBoxLayout.SelectedIndex = 0; _mKeyboardEvents = Hook.GlobalEvents(); _mKeyboardEvents.KeyDown += HandleKeyDown; InitBoard(); InitSnake(); _mTimerGame.Start(); }
public GlamWindow() { InitializeComponent(); this.TopMost = true; this.SetWindowTitle("Loading frame..."); this.GlamFrame.OnReady += OnGlamFrameReady; this.GlamFrame.OnPlaylistCued += OnPlaylistCued; this.GlamFrame.Initialize(VideoPlayerLocation); this.GlamFrame.VolumeFadeEnabled = true; ControlPanelHeight = this.panelControl.Height; this.Height -= ControlPanelHeight; this.panelControl.Hide(); this.CurrentMapId = UnknownMap; this.Core = new GlamCore(); this.Core.Load(); this.appSettings = new Settings(); this.FormClosing += OnWindowClosing; this.Resize += OnWindowResize; this.TitleHeight = this.PointToScreen(Point.Empty).Y - this.Top; // http://stackoverflow.com/questions/18429425/c-sharp-absolute-position-of-control-on-screen this.BorderWidth = this.PointToScreen(Point.Empty).X - this.Left; this.OnMapUpdateTimer = new System.Timers.Timer(); this.OnMapUpdateTimer.Interval = 1000; this.OnMapUpdateTimer.Elapsed += DelayedOnMapUpdateStart; this.KeyboardListener = Hook.GlobalEvents(); this.KeyboardListener.KeyDown += OnGlobalKeyDown; this.LoadFieldData(); }
/// <summary> /// Detects a key or key combination and triggers the corresponding action. /// </summary> /// <param name="source"> /// An instance of Global or Application hook. Use <see cref="Hook.GlobalEvents" /> or <see cref="Hook.AppEvents" /> to /// create it. /// </param> /// <param name="map"> /// This map contains the list of key combinations mapped to corresponding actions. You can use a dictionary initilizer /// to easily create it. /// Whenever a listed combination will be detected a corresponding action will be triggered. /// </param> /// <param name="reset"> /// This optional action will be executed when some key was pressed but it was not part of any wanted combinations. /// </param> public static void OnCXCombination(this IKeyboardEvents source, IEnumerable <KeyValuePair <CXHotkeyCombination, Action> > map, Action reset = null) { var watchlists = map.GroupBy(k => k.Key.TriggerKey) .ToDictionary(g => g.Key, g => g.ToArray()); source.KeyDown += (sender, e) => { if (!watchlists.TryGetValue(e.KeyCode, out KeyValuePair <CXHotkeyCombination, Action>[] element)) { reset?.Invoke(); return; } var state = KeyboardState.GetCurrent(); var action = reset; var maxLength = 0; int modifiersPressed = CountTrue(state.IsDown(Keys.Control), state.IsDown(Keys.Alt), state.IsDown(Keys.Shift)); foreach (var current in element) { if (current.Key.ChordLength < modifiersPressed) { continue; } if (!current.Key.Chord.All(state.IsDown)) { continue; } if (maxLength > current.Key.ChordLength) { continue; } maxLength = current.Key.ChordLength; action = current.Value; } action?.Invoke(); }; }
public void SetHook(KeyEventHandler callback) { IKeyboardEvents keyboardEvents = Hook.GlobalEvents(); keyboardEvents.KeyDown += callback; }
public DesktopRecorder() { GlobalHook = Hook.GlobalEvents(); GlobalHook.KeyPress += GlobalHookKeyPress; InitializeComponent(); }
public static IObservable <Keys> KeyUpObservable(this IKeyboardEvents source) { return(Observable .FromEventPattern <KeyEventArgs>(source, "KeyDown") .Select(ep => ep.EventArgs.KeyCode)); }