-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyInterceptor.cs
executable file
·146 lines (121 loc) · 4.5 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
139
140
141
142
143
144
145
146
// <copyright file="KeyInterceptor.cs">
// CopyCopyDict - Background app that opens a dictionary definition of a selected word by Ctrl+C+C
// (c) 2023 Artem Avramenko. https://github.com/ArtemAvramenko/CopyCopyDict
// License: MIT
// </copyright>
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace CopyCopyDict
{
class KeyInterceptor : IDisposable
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
[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)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private 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);
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
private readonly LowLevelKeyboardProc _proc;
private readonly AutoResetEvent _listenerEvent = new AutoResetEvent(false);
private readonly HotKeyWindow _hotKeyWindow = new HotKeyWindow();
private readonly IntPtr _hookID;
private string _keySequence = "";
private bool _isDisposed;
public KeyInterceptor()
{
_hotKeyWindow.Pressed += () =>
{
ResetHotKey();
_listenerEvent.Set();
};
var thread = new Thread(ListenerWorker);
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
_proc = HookCallback;
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
_hookID = SetWindowsHookEx(
WH_KEYBOARD_LL,
_proc,
GetModuleHandle(curModule.ModuleName),
0);
}
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
if ((Control.ModifierKeys & Keys.Control) == 0)
{
ResetHotKey();
}
else
{
var key = (Keys)Marshal.ReadInt32(lParam);
var isKeyDown = wParam == (IntPtr)WM_KEYDOWN;
if (key == Keys.Insert)
{
_keySequence += isKeyDown ? "I" : "i";
}
else if (key == Keys.C)
{
_keySequence += isKeyDown ? "C" : "c";
}
else
{
ResetHotKey();
}
if (_keySequence == "Cc" || _keySequence == "Ii")
{
SetHotKey(key);
}
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private void SetHotKey(Keys key)
{
ResetHotKey();
_hotKeyWindow.Register(KeyModifiers.Control, key);
}
private void ResetHotKey()
{
_keySequence = "";
_hotKeyWindow.Unregister();
}
private void ListenerWorker()
{
while (!_isDisposed)
{
_listenerEvent.WaitOne();
if (!_isDisposed && Pressed != null)
{
Pressed();
}
}
}
public event MethodInvoker Pressed;
public void Dispose()
{
if (!_isDisposed)
{
_isDisposed = true;
UnhookWindowsHookEx(_hookID);
_listenerEvent.Set();
}
}
}
}