private void RegisterScrolling(CompositeHook hook) { var mouse = hook.GetMouse(); var name = mouse.Information.InstanceName + " " + mouse.Information.InstanceGuid; ScrollUp = new KeyOrButton(new GamepadButton(name, "Scroll_Up")); ScrollDown = new KeyOrButton(new GamepadButton(name, "Scroll_Down")); hook.RegisterHotKey(ScrollUp); hook.RegisterHotKey(ScrollDown); }
public void RegisterHotKey(KeyOrButton keyOrButton) { if (keyOrButton.IsKey) { RegisterHotKey(keyOrButton.Key); } else { RegisterGamepadButton(keyOrButton.Button); } }
public CounterComponentSettings() { InitializeComponent(); Hook = new CompositeHook(); // Set default values. GlobalHotkeysEnabled = false; CounterFont = new Font("Segoe UI", 13, FontStyle.Regular, GraphicsUnit.Pixel); OverrideCounterFont = false; CounterTextColor = Color.FromArgb(255, 255, 255, 255); CounterValueColor = Color.FromArgb(255, 255, 255, 255); OverrideTextColor = false; BackgroundColor = Color.Transparent; BackgroundColor2 = Color.Transparent; BackgroundGradient = GradientType.Plain; CounterText = "Counter:"; InitialValue = 0; Increment = 1; // Hotkeys IncrementKey = new KeyOrButton(Keys.Add); DecrementKey = new KeyOrButton(Keys.Subtract); ResetKey = new KeyOrButton(Keys.NumPad0); // Set bindings. txtIncrement.Text = FormatKey(IncrementKey); txtDecrement.Text = FormatKey(DecrementKey); txtReset.Text = FormatKey(ResetKey); txtCounterText.DataBindings.Add("Text", this, "CounterText"); numInitialValue.DataBindings.Add("Value", this, "InitialValue"); numIncrement.DataBindings.Add("Value", this, "Increment"); chkGlobalHotKeys.DataBindings.Add("Checked", this, "GlobalHotkeysEnabled", false, DataSourceUpdateMode.OnPropertyChanged); chkFont.DataBindings.Add("Checked", this, "OverrideCounterFont", false, DataSourceUpdateMode.OnPropertyChanged); lblFont.DataBindings.Add("Text", this, "CounterFontString", false, DataSourceUpdateMode.OnPropertyChanged); chkColor.DataBindings.Add("Checked", this, "OverrideTextColor", false, DataSourceUpdateMode.OnPropertyChanged); btnColor.DataBindings.Add("BackColor", this, "CounterTextColor", false, DataSourceUpdateMode.OnPropertyChanged); btnColor3.DataBindings.Add("BackColor", this, "CounterValueColor", false, DataSourceUpdateMode.OnPropertyChanged); btnColor1.DataBindings.Add("BackColor", this, "BackgroundColor", false, DataSourceUpdateMode.OnPropertyChanged); btnColor2.DataBindings.Add("BackColor", this, "BackgroundColor2", false, DataSourceUpdateMode.OnPropertyChanged); cmbGradientType.DataBindings.Add("SelectedItem", this, "GradientString", false, DataSourceUpdateMode.OnPropertyChanged); // Assign event handlers. cmbGradientType.SelectedIndexChanged += cmbGradientType_SelectedIndexChanged; chkFont.CheckedChanged += chkFont_CheckedChanged; chkColor.CheckedChanged += chkColor_CheckedChanged; chkGlobalHotKeys.CheckedChanged += chkGlobalHotKeys_CheckedChanged; Load += CounterSettings_Load; RegisterHotKeys(); }
private string FormatKey(KeyOrButton key) { if (key != null) { var keyString = key.ToString(); if (key.IsButton) { var lastSpaceIndex = keyString.LastIndexOf(' '); if (lastSpaceIndex != -1) { keyString = keyString.Substring(0, lastSpaceIndex); } } return keyString; } return "None"; }
private void RegisterHotkey(CompositeHook hook, KeyOrButton key) { hook.RegisterHotKey(key); if (DeactivateHotkeysForOtherPrograms && key.IsKey && GlobalHotkeysEnabled) { var args = new System.Windows.Forms.KeyEventArgs(key.Key); var modifiers = (args.Alt ? ModifierKeys.Alt : ModifierKeys.None) | (args.Shift ? ModifierKeys.Shift : ModifierKeys.None) | (args.Control ? ModifierKeys.Control : ModifierKeys.None); HotkeyHook.Instance.RegisterHotKey(modifiers, args.KeyCode); } }
private void SetHotkeyHandlers(TextBox txtBox, Action<KeyOrButton> keySetCallback) { var oldText = txtBox.Text; txtBox.Text = "Set Hotkey..."; txtBox.Select(0, 0); KeyEventHandler handlerDown = null; KeyEventHandler handlerUp = null; EventHandler leaveHandler = null; EventHandlerT<GamepadButton> gamepadButtonPressed = null; Action unregisterEvents = () => { txtBox.KeyDown -= handlerDown; txtBox.KeyUp -= handlerUp; txtBox.Leave -= leaveHandler; Hook.AnyGamepadButtonPressed -= gamepadButtonPressed; }; handlerDown = (s, x) => { var key = x.KeyCode == Keys.Escape ? null : new KeyOrButton(x.KeyCode | x.Modifiers); if (x.KeyCode == Keys.ControlKey || x.KeyCode == Keys.ShiftKey || x.KeyCode == Keys.Menu) return; keySetCallback(key); unregisterEvents(); txtBox.Select(0, 0); chkGlobalHotkeys.Select(); txtBox.Text = FormatKey(key); }; handlerUp = (s, x) => { var key = x.KeyCode == Keys.Escape ? null : new KeyOrButton(x.KeyCode | x.Modifiers); if (x.KeyCode == Keys.ControlKey || x.KeyCode == Keys.ShiftKey || x.KeyCode == Keys.Menu) { keySetCallback(key); unregisterEvents(); txtBox.Select(0, 0); chkGlobalHotkeys.Select(); txtBox.Text = FormatKey(key); } }; leaveHandler = (s, x) => { unregisterEvents(); txtBox.Text = oldText; }; gamepadButtonPressed = (s, x) => { var key = new KeyOrButton(x); keySetCallback(key); unregisterEvents(); Action action = () => { txtBox.Select(0, 0); chkGlobalHotkeys.Select(); txtBox.Text = FormatKey(key); }; if (InvokeRequired) Invoke(action); else action(); }; txtBox.KeyDown += handlerDown; txtBox.KeyUp += handlerUp; txtBox.Leave += leaveHandler; Hook.AnyGamepadButtonPressed += gamepadButtonPressed; }
void hook_KeyOrButtonPressed(object sender, KeyOrButton e) { Action action = () => { if ((ActiveForm == this || Settings.GlobalHotkeysEnabled) && !ResetMessageShown && !IsInDialogMode) { if (Settings.SplitKey == e) { if (Settings.HotkeyDelay > 0) { var splitTimer = new System.Timers.Timer(Settings.HotkeyDelay * 1000f); splitTimer.Enabled = true; splitTimer.Elapsed += splitTimer_Elapsed; } else StartOrSplit(); } else if (Settings.UndoKey == e) { Model.UndoSplit(); } else if (Settings.SkipKey == e) { Model.SkipSplit(); } else if (Settings.ResetKey == e) { Reset(); } else if (Settings.PauseKey == e) { if (Settings.HotkeyDelay > 0) { var pauseTimer = new System.Timers.Timer(Settings.HotkeyDelay * 1000f); pauseTimer.Enabled = true; pauseTimer.Elapsed += pauseTimer_Elapsed; } else Model.Pause(); } else if (Settings.SwitchComparisonPrevious == e) Model.SwitchComparisonPrevious(); else if (Settings.SwitchComparisonNext == e) Model.SwitchComparisonNext(); } if (ActiveForm == this && !ResetMessageShown && !IsInDialogMode) { if (Settings.ScrollUp == e) Model.ScrollUp(); else if (Settings.ScrollDown == e) Model.ScrollDown(); } if (Settings.ToggleGlobalHotkeys == e) { Settings.GlobalHotkeysEnabled = !Settings.GlobalHotkeysEnabled; SetProgressBar(); } }; new Task(() => { try { Invoke(action); } catch (Exception ex) { Log.Error(ex); } }).Start(); }
private void txtReset_Enter(object sender, EventArgs e) { SetHotkeyHandlers((TextBox)sender, x => ResetKey = x); }
private string FormatKey(KeyOrButton key) { if (key == null) return "None"; string str = key.ToString(); if (key.IsButton) { int length = str.LastIndexOf(' '); if (length != -1) str = str.Substring(0, length); } return str; }
// Behaviour essentially Lifted from LiveSplit Settings. private void SetHotkeyHandlers(TextBox txtBox, Action<KeyOrButton> keySetCallback) { string oldText = txtBox.Text; txtBox.Text = "Set Hotkey..."; txtBox.Select(0, 0); KeyEventHandler handlerDown = null; KeyEventHandler handlerUp = null; EventHandler leaveHandler = null; EventHandlerT<GamepadButton> gamepadButtonPressed = null; // Remove Input handlers. Action unregisterEvents = () => { txtBox.KeyDown -= handlerDown; txtBox.KeyUp -= handlerUp; txtBox.Leave -= leaveHandler; Hook.AnyGamepadButtonPressed -= gamepadButtonPressed; }; // Handler for KeyDown handlerDown = (s, x) => { KeyOrButton keyOrButton = x.KeyCode == Keys.Escape ? null : new KeyOrButton(x.KeyCode | x.Modifiers); // No action for special keys. if (x.KeyCode == Keys.ControlKey || x.KeyCode == Keys.ShiftKey || x.KeyCode == Keys.Menu) return; keySetCallback(keyOrButton); unregisterEvents(); // Remove Focus. txtBox.Select(0, 0); chkGlobalHotKeys.Select(); txtBox.Text = FormatKey(keyOrButton); // Re-Register inputs. RegisterHotKeys(); }; // Handler for KeyUp (allows setting of special keys, shift, ctrl etc.). handlerUp = (s, x) => { KeyOrButton keyOrButton = x.KeyCode == Keys.Escape ? null : new KeyOrButton(x.KeyCode | x.Modifiers); // No action for normal keys. if (x.KeyCode != Keys.ControlKey && x.KeyCode != Keys.ShiftKey && x.KeyCode != Keys.Menu) return; keySetCallback(keyOrButton); unregisterEvents(); txtBox.Select(0, 0); chkGlobalHotKeys.Select(); txtBox.Text = FormatKey(keyOrButton); RegisterHotKeys(); }; leaveHandler = (s, x) => { unregisterEvents(); txtBox.Text = oldText; }; // Handler for gamepad/joystick inputs. gamepadButtonPressed = (s, x) => { KeyOrButton key = new KeyOrButton(x); keySetCallback(key); unregisterEvents(); Action keyOrButton = () => { txtBox.Select(0, 0); chkGlobalHotKeys.Select(); txtBox.Text = FormatKey(key); RegisterHotKeys(); }; // May not be in the UI thread (likely). if (InvokeRequired) Invoke(keyOrButton); else keyOrButton(); }; txtBox.KeyDown += handlerDown; txtBox.KeyUp += handlerUp; txtBox.Leave += leaveHandler; Hook.AnyGamepadButtonPressed += gamepadButtonPressed; }
public void SetSettings(XmlNode node) { var element = (XmlElement)node; GlobalHotkeysEnabled = SettingsHelper.ParseBool(element["GlobalHotkeysEnabled"]); CounterTextColor = SettingsHelper.ParseColor(element["CounterTextColor"]); CounterValueColor = SettingsHelper.ParseColor(element["CounterValueColor"]); CounterFont = SettingsHelper.GetFontFromElement(element["CounterFont"]); OverrideCounterFont = SettingsHelper.ParseBool(element["OverrideCounterFont"]); OverrideTextColor = SettingsHelper.ParseBool(element["OverrideTextColor"]); BackgroundColor = SettingsHelper.ParseColor(element["BackgroundColor"]); BackgroundColor2 = SettingsHelper.ParseColor(element["BackgroundColor2"]); GradientString = SettingsHelper.ParseString(element["BackgroundGradient"]); CounterText = SettingsHelper.ParseString(element["CounterText"]); InitialValue = SettingsHelper.ParseInt(element["InitialValue"]); Increment = SettingsHelper.ParseInt(element["Increment"]); XmlElement incrementElement = element["IncrementKey"]; IncrementKey = string.IsNullOrEmpty(incrementElement.InnerText) ? null : new KeyOrButton(incrementElement.InnerText); XmlElement decrementElement = element["DecrementKey"]; DecrementKey = string.IsNullOrEmpty(decrementElement.InnerText) ? null : new KeyOrButton(decrementElement.InnerText); XmlElement resetElement = element["ResetKey"]; ResetKey = string.IsNullOrEmpty(resetElement.InnerText) ? null : new KeyOrButton(resetElement.InnerText); RegisterHotKeys(); }
public void RegisterHotKey(KeyOrButton keyOrButton) { if (keyOrButton.IsKey) RegisterHotKey(keyOrButton.Key); else RegisterGamepadButton(keyOrButton.Button); }
// Basic support for keyboard/button input. private void hook_KeyOrButtonPressed(object sender, KeyOrButton e) { if ((Form.ActiveForm == state.Form && !Settings.GlobalHotkeysEnabled) || Settings.GlobalHotkeysEnabled) { if (e == Settings.IncrementKey) Counter.Increment(); if (e == Settings.DecrementKey) Counter.Decrement(); if (e == Settings.ResetKey) { Counter.Reset(); } } }