private void setHotkeyButton_Click(object sender, EventArgs e) { if (presetsComboBox.SelectedItem != null && mNewHotkey.NewHotkeyIsValid) { uint modifierKeyCode = 0; if ((mNewHotkey.NewHotkeyModifiers & Keys.Alt) > 0) { modifierKeyCode = modifierKeyCode | VirtualKeyCodes.MOD_ALT; } if ((mNewHotkey.NewHotkeyModifiers & Keys.Control) > 0) { modifierKeyCode = modifierKeyCode | VirtualKeyCodes.MOD_CONTROL; } if ((mNewHotkey.NewHotkeyModifiers & Keys.Shift) > 0) { modifierKeyCode = modifierKeyCode | VirtualKeyCodes.MOD_SHIFT; } uint keypressKeyCode = (uint)mNewHotkey.NewHotkeyKeyCode; VirtualHotkey virtualKeyCode = new VirtualHotkey(modifierKeyCode, keypressKeyCode); mPresetToHotkeyMap.SetHotkey(((DisplayPreset)presetsComboBox.SelectedItem).Name, virtualKeyCode); } }
/// <summary> /// Event handler for when a display preset has a hotkey set or cleared. Registers /// new hotkey and deregisters old hotkeys as appropriate. /// </summary> /// <param name="presetName">The name of the preset changed.</param> /// <param name="virtualHotkey">The new hotkey if applicable.</param> /// <param name="changeType">The type of change, either a hotkey has been set or removed.</param> private void OnPresetHotkeyMapChanged(string presetName, VirtualHotkey virtualHotkey, PresetToHotkeyMap.PresetToHotkeyMappingChangeType changeType) { if (changeType == PresetToHotkeyMap.PresetToHotkeyMappingChangeType.HotkeyRemoved) { int existingHotkeyId; if (mPresetToHotkeyId.TryGetValue(presetName, out existingHotkeyId)) { DeregisterHotkeyAndUpdateIdMapping(presetName, existingHotkeyId); } else { throw new Exception("Tried to deregister hotkey for unknown preset: " + presetName); } } else if (changeType == PresetToHotkeyMap.PresetToHotkeyMappingChangeType.HotkeySet) { // If a hotkey already exists for this, we need to deregister the old one // and register the new one. int existingHotkeyId; if (mPresetToHotkeyId.TryGetValue(presetName, out existingHotkeyId)) { DeregisterHotkeyAndUpdateIdMapping(presetName, existingHotkeyId); } RegisterHotkeyAndUpdateIdMapping(presetName, virtualHotkey); } }
/// <summary> /// Set the global hotkey of a display preset. /// </summary> /// <param name="presetName">The display preset name.</param> /// <param name="virtualHotkey">The virtual key code of the global hotkey with modifiers.</param> public void SetHotkey(string presetName, VirtualHotkey virtualHotkey) { // Use the Item property to set the value because if it already exists // we want to overwrite it. mPresetToHotkeyDictionary[presetName] = virtualHotkey; SaveMapping(); OnPresetToHotkeyMappingChanged?.Invoke(presetName, virtualHotkey, PresetToHotkeyMappingChangeType.HotkeySet); }
/// <summary> /// Registers a global hotkey through Windows API and associates the ID of the hotkey with the /// name of the display preset it is intended to trigger. /// </summary> /// <param name="presetName">The name of the preset to register a hotkey for.</param> /// <param name="virtualHotkey">The keycode and modifier flags of the hotkey.</param> private void RegisterHotkeyAndUpdateIdMapping(string presetName, VirtualHotkey virtualHotkey) { int newHotkeyId = (int)DateTime.Now.ToFileTimeUtc(); bool result = Hotkey.RegisterHotKey( IntPtr.Zero, newHotkeyId, virtualHotkey.ModifierKeyCode | VirtualKeyCodes.MOD_NOREPEAT, virtualHotkey.KeyCode); if (!result) { throw new Exception("Failed to register new hotkey for preset " + presetName + " with ID " + newHotkeyId + " with modifiers keycode " + virtualHotkey.ModifierKeyCode + " with keycode " + virtualHotkey.KeyCode); } mPresetToHotkeyId.Add(presetName, newHotkeyId); }
public PresetHotkeyPersistencePair(string presetName, VirtualHotkey virtualHotkey) { PresetName = presetName; HotkeyKeycode = virtualHotkey; }
private void OnPresetToHotkeyMapChanged(string presetName, VirtualHotkey keyCode, PresetToHotkeyMap.PresetToHotkeyMappingChangeType changeType) { // Dumbly update the entire working dictionary of preset to hotkey mappings. mPresetToHotkeyDictionaryForComboBox = mPresetToHotkeyMap.GetHotkeyMappings(); ShowSelectedPresetKeyCombination(((DisplayPreset)presetsComboBox.SelectedItem).Name); }