-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKeyInterceptor.cs
138 lines (118 loc) · 5.37 KB
/
KeyInterceptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using WinForms = System.Windows.Forms;
namespace _3launch
{
static class KeyInterceptor
{
private static readonly InterceptKeys.LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private const int KEYEVENTF_EXTENDEDKEY = 0x1;
private const int KEYEVENTF_KEYUP = 0x2;
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private static bool otherKeyPressed = false;
public static volatile MainWindow mainWindow = null;
public static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
int vkCode = Marshal.ReadInt32(lParam);
WinForms.Keys key = (WinForms.Keys) vkCode;
if (key == System.Windows.Forms.Keys.LWin)
{
if ((int) wParam == WM_KEYDOWN)
otherKeyPressed = false;
if ((int) wParam == WM_KEYUP)
{
if (otherKeyPressed)
{
// A win key hotkey, do nothing
}
else
{
// send some rubbish keystrokes to prevent start menu from popping up
keybd_event((int) System.Windows.Forms.Keys.LControlKey, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event((int) System.Windows.Forms.Keys.LShiftKey, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event((int) System.Windows.Forms.Keys.LMenu, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event((int) System.Windows.Forms.Keys.LWin, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event((int) System.Windows.Forms.Keys.None, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event((int) System.Windows.Forms.Keys.None, 0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
keybd_event((int) System.Windows.Forms.Keys.LWin, 0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
keybd_event((int) System.Windows.Forms.Keys.LMenu, 0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
keybd_event((int) System.Windows.Forms.Keys.LShiftKey, 0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
keybd_event((int) System.Windows.Forms.Keys.LControlKey, 0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
if (mainWindow.Visibility == Visibility.Visible)
{
mainWindow.clearAndHide();
}
else
{
mainWindow.Show();
mainWindow.Activate();
}
return (IntPtr) 1;
}
}
}
else
{
otherKeyPressed = true;
}
}
return InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam);
}
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
private static void DetachKeyboardHook()
{
if (_hookID != IntPtr.Zero)
InterceptKeys.UnhookWindowsHookEx(_hookID);
}
public static void start()
{
try
{
_hookID = InterceptKeys.SetHook(_proc);
}
catch
{
DetachKeyboardHook();
}
}
}
internal class InterceptKeys
{
#region Delegates
public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
#endregion
private const int WH_KEYBOARD_LL = 13;
public static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private 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)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
}