Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
244 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/********************************************** | ||
* Extreme AltF4 * | ||
* (C) 2017 Marcel Bulla * | ||
* https://github.com/markellus/Extreme-AltF4 * | ||
* See file LICENSE for license information * | ||
**********************************************/ | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Windows.Forms; | ||
using static AltF4.NativeMethods; | ||
|
||
namespace AltF4 | ||
{ | ||
internal class AltF4Handler | ||
{ | ||
public static AltF4Handler Get() | ||
{ | ||
return _instance; | ||
} | ||
|
||
private static AltF4Handler _instance = new AltF4Handler(); | ||
|
||
private static SafeList<Keys> _listKeys; | ||
|
||
private static IntPtr _hookID = IntPtr.Zero; | ||
|
||
private bool _fired; | ||
|
||
private LowLevelKeyboardProc _callback; | ||
|
||
public event EventHandler OnAltF4; | ||
|
||
private AltF4Handler() | ||
{ | ||
_listKeys = new SafeList<Keys>(); | ||
_callback = HookCallback; | ||
_hookID = SetHook(_callback); | ||
_fired = false; | ||
} | ||
|
||
~AltF4Handler() | ||
{ | ||
UnhookWindowsHookEx(_hookID); | ||
} | ||
|
||
private IntPtr SetHook(LowLevelKeyboardProc proc) | ||
{ | ||
using (Process curProcess = Process.GetCurrentProcess()) | ||
{ | ||
using (ProcessModule curModule = curProcess.MainModule) | ||
{ | ||
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, | ||
GetModuleHandle(curModule.ModuleName), 0); | ||
} | ||
} | ||
} | ||
|
||
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) | ||
{ | ||
if (nCode < 0) | ||
{ | ||
return CallNextHookEx(_hookID, nCode, wParam, lParam); | ||
} | ||
|
||
int vkCode = Marshal.ReadInt32(lParam); | ||
Keys code = (Keys)vkCode; | ||
|
||
if (KeyDown(wParam)) | ||
{ | ||
_listKeys.SafeAdd(code); | ||
} | ||
else if (KeyUp(wParam)) | ||
{ | ||
_listKeys.SafeRemove(code); | ||
} | ||
|
||
if (_listKeys.Contains(Keys.F4) && | ||
(_listKeys.Contains(Keys.LMenu) || _listKeys.Contains(Keys.Alt))) | ||
{ | ||
if(!_fired) | ||
{ | ||
_fired = true; | ||
OnAltF4?.Invoke(this, EventArgs.Empty); | ||
} | ||
return (IntPtr)1; | ||
} | ||
else | ||
{ | ||
_fired = false; | ||
} | ||
|
||
return CallNextHookEx(_hookID, nCode, wParam, lParam); | ||
} | ||
|
||
private bool KeyDown(IntPtr wParam) | ||
{ | ||
return (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN); | ||
} | ||
|
||
private bool KeyUp(IntPtr wParam) | ||
{ | ||
return (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/********************************************** | ||
* Extreme AltF4 * | ||
* (C) 2017 Marcel Bulla * | ||
* https://github.com/markellus/Extreme-AltF4 * | ||
* See file LICENSE for license information * | ||
**********************************************/ | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace AltF4 | ||
{ | ||
internal static class NativeMethods | ||
{ | ||
public const int WH_KEYBOARD_LL = 13; | ||
public const int WM_KEYDOWN = 0x0100; | ||
public const int WM_SYSKEYDOWN = 0x0104; | ||
public const int WM_KEYUP = 0x0101; | ||
public const int WM_SYSKEYUP = 0x0105; | ||
|
||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] | ||
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); | ||
|
||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] | ||
[return: MarshalAs(UnmanagedType.Bool)] | ||
public static extern bool UnhookWindowsHookEx(IntPtr hhk); | ||
|
||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] | ||
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); | ||
|
||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] | ||
public static extern IntPtr GetModuleHandle(string lpModuleName); | ||
|
||
internal delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/********************************************** | ||
* Extreme AltF4 * | ||
* (C) 2017 Marcel Bulla * | ||
* https://github.com/markellus/Extreme-AltF4 * | ||
* See file LICENSE for license information * | ||
**********************************************/ | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace AltF4 | ||
{ | ||
internal class SafeList<T> : List<T> | ||
{ | ||
public void SafeAdd(T item) | ||
{ | ||
if (!this.Contains(item)) | ||
{ | ||
this.Add(item); | ||
} | ||
} | ||
|
||
public void SafeRemove(T item) | ||
{ | ||
if (this.Contains(item)) | ||
{ | ||
this.Remove(item); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.