/// /// Events: keyboard global hotkeys /// /// <summary> /// Global keyboard hotkeys. /// </summary> private void OnHotkeyDown(Object sender, KeyboardHookEventArgs e) { switch (e.Name) { case "Kill Process": TryKillingCurrentFileProcess(); break; case "Close Process": TryClosingCurrentFileProcess(); break; } }
/// /// Actual hooker callback /// /// <summary> /// Callback that intercepts key presses. /// </summary> private IntPtr Callback(Int32 nCode, IntPtr wParam, IntPtr lParam) { // assume the hotkey won't match and will be forwarded: Boolean forward = true; if (nCode >= 0) { Int32 msg = wParam.ToInt32(); // we care about keyup/keydown messages: if ((msg == WM_KEYUP) || (msg == WM_SYSKEYUP) || (msg == WM_KEYDOWN) || (msg == WM_SYSKEYDOWN)) { // the virtual key code is the first KBDLLHOOKSTRUCT member: Int32 vkCode = Marshal.ReadInt32(lParam); // base key matches? if (hotkeys.ContainsKey(vkCode)) { Keys modifiers = PressedModifiers; // modifiers match? if (hotkeys[vkCode].Contains(modifiers)) { Keys key = (Keys)vkCode; Hotkey hotkey = new Hotkey(key, modifiers); String name = hotkeysToNames[hotkey]; // override forward with the current hotkey option: forward = hotkeysForward[hotkey]; KeyboardHookEventArgs e = new KeyboardHookEventArgs(name, key, modifiers); // call the appropriate event handler using the current dispatcher: if (msg == WM_KEYUP || msg == WM_SYSKEYUP) { if (HotkeyUp != null) { dispatcher.BeginInvoke(HotkeyUp, new Object[] { instance, e }); } } else { if (HotkeyDown != null) { dispatcher.BeginInvoke(HotkeyDown, new Object[] { instance, e }); } } } } } } // forward or return a dummy value other than 0: if (forward) { return(CallNextHookEx(hookID, nCode, wParam, lParam)); } else { return(new IntPtr(1)); } }
/// /// Events: multimedia keys /// private void OnHotkeyDown(Object sender, KeyboardHookEventArgs e) { switch (e.Name) { case "Toggle Play": { player.TogglePlay(); break; } case "Stop": { player.Stop(); break; } case "Toggle Mute": { player.ToggleMute(); break; } case "Volume Up": { Int32 volume = volumeTrackBar.TrackBar.Value; Int32 volume_max = volumeTrackBar.TrackBar.Maximum; Int32 volume_step = volumeTrackBar.TrackBar.SmallChange; volumeTrackBar.TrackBar.Value = Math.Min(volume + volume_step, volume_max); break; } case "Volume Down": { Int32 volume = volumeTrackBar.TrackBar.Value; Int32 volume_min = volumeTrackBar.TrackBar.Minimum; Int32 volume_step = volumeTrackBar.TrackBar.SmallChange; volumeTrackBar.TrackBar.Value = Math.Max(volume - volume_step, volume_min); break; } } }
/// /// Actual hooker callback /// /// <summary> /// Callback that intercepts key presses. /// </summary> private IntPtr Callback(Int32 nCode, IntPtr wParam, IntPtr lParam) { // assume the hotkey won't match and will be forwarded: Boolean forward = true; if (nCode >= 0) { Int32 msg = wParam.ToInt32(); // we care about keyup/keydown messages: if ((msg == WM_KEYUP) || (msg == WM_SYSKEYUP) || (msg == WM_KEYDOWN) || (msg == WM_SYSKEYDOWN)) { // the virtual key code is the first KBDLLHOOKSTRUCT member: Int32 vkCode = Marshal.ReadInt32(lParam); // base key matches? if (hotkeys.ContainsKey(vkCode)) { Keys modifiers = PressedModifiers; // modifiers match? if (hotkeys[vkCode].Contains(modifiers)) { Keys key = (Keys) vkCode; Hotkey hotkey = new Hotkey(key, modifiers); String name = hotkeysToNames[hotkey]; // override forward with the current hotkey option: forward = hotkeysForward[hotkey]; KeyboardHookEventArgs e = new KeyboardHookEventArgs(name, key, modifiers); // call the appropriate event handler using the current dispatcher: if (msg == WM_KEYUP || msg == WM_SYSKEYUP) { if (HotkeyUp != null) { dispatcher.BeginInvoke(HotkeyUp, new Object[] { instance, e }); } } else { if (HotkeyDown != null) { dispatcher.BeginInvoke(HotkeyDown, new Object[] { instance, e }); } } } } } } // forward or return a dummy value other than 0: if (forward) { return CallNextHookEx(hookID, nCode, wParam, lParam); } else { return new IntPtr(1); } }