Skip to content
This repository was archived by the owner on Nov 12, 2022. It is now read-only.

Commit 066361c

Browse files
committed
Extracted WinAPI functions into its own class.
Things were getting a bit messy with winapi functions being set everywhere and constants being set everywhere. This is an initial attempt to refactor out all of that and put it into one place for easier usage in the future.
1 parent c298ae8 commit 066361c

8 files changed

+1167
-1167
lines changed

SuperPutty/Classes/KeyboardListener.cs

+10-47
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public KeyboardListener(frmSuperPutty form, GlobalHotkeys hotkeys)
3232
this.dispatcher = Dispatcher.CurrentDispatcher;
3333

3434
// We have to store the LowLevelKeyboardProc, so that it is not garbage collected runtime
35-
hookedLowLevelKeyboardProc = (InterceptKeys.LowLevelKeyboardProc)LowLevelKeyboardProc;
35+
hookedLowLevelKeyboardProc = (WinAPI.LowLevelKeyboardProc)LowLevelKeyboardProc;
3636

3737
// Set the hook
3838
hookId = InterceptKeys.SetHook(hookedLowLevelKeyboardProc);
@@ -118,7 +118,7 @@ private IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam)
118118
}
119119
}
120120

121-
return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam);
121+
return WinAPI.CallNextHookEx(hookId, nCode, wParam, lParam);
122122
}
123123

124124
/// <summary>
@@ -129,7 +129,7 @@ private IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam)
129129
/// <summary>
130130
/// Contains the hooked callback in runtime.
131131
/// </summary>
132-
private InterceptKeys.LowLevelKeyboardProc hookedLowLevelKeyboardProc;
132+
private WinAPI.LowLevelKeyboardProc hookedLowLevelKeyboardProc;
133133

134134
/// <summary>
135135
/// HookCallbackAsync procedure that calls accordingly the KeyDown or KeyUp events.
@@ -176,7 +176,7 @@ void KeyboardListener_KeyboardCallbackAsync(InterceptKeys.KeyEvent keyEvent, int
176176
/// </summary>
177177
public void Dispose()
178178
{
179-
InterceptKeys.UnhookWindowsHookEx(hookId);
179+
WinAPI.UnhookWindowsHookEx(hookId);
180180
}
181181

182182
#endregion
@@ -245,7 +245,6 @@ public RawKeyEventArgs(int VKCode, bool isSysKey, string Character)
245245
/// </summary>
246246
internal static class InterceptKeys
247247
{
248-
public delegate IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam);
249248
public static int WH_KEYBOARD_LL = 13;
250249

251250
/// <summary>
@@ -274,68 +273,32 @@ public enum KeyEvent : int
274273
WM_SYSKEYDOWN = 260
275274
}
276275

277-
public static IntPtr SetHook(LowLevelKeyboardProc proc)
276+
public static IntPtr SetHook(WinAPI.LowLevelKeyboardProc proc)
278277
{
279278
using (Process curProcess = Process.GetCurrentProcess())
280279
using (ProcessModule curModule = curProcess.MainModule)
281280
{
282-
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
281+
return WinAPI.SetWindowsHookEx(WH_KEYBOARD_LL, proc, WinAPI.GetModuleHandle(curModule.ModuleName), 0);
283282
}
284283
}
285284

286-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
287-
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
288-
289-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
290-
[return: MarshalAs(UnmanagedType.Bool)]
291-
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
292-
293-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
294-
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, UIntPtr wParam, IntPtr lParam);
295-
296-
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
297-
public static extern IntPtr GetModuleHandle(string lpModuleName);
298-
299285
#region Convert VKCode to string
300286
// Note: Sometimes single VKCode represents multiple chars, thus string.
301287
// E.g. typing "^1" (notice that when pressing 1 the both characters appear,
302288
// because of this behavior, "^" is called dead key)
303289

304-
[DllImport("user32.dll")]
305-
private static extern int ToUnicodeEx(uint wVirtKey, uint wScanCode, byte[] lpKeyState, [Out, MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pwszBuff, int cchBuff, uint wFlags, IntPtr dwhkl);
306-
307-
[DllImport("user32.dll")]
308-
private static extern bool GetKeyboardState(byte[] lpKeyState);
309-
310-
[DllImport("user32.dll")]
311-
private static extern uint MapVirtualKeyEx(uint uCode, uint uMapType, IntPtr dwhkl);
312-
313-
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
314-
private static extern IntPtr GetKeyboardLayout(uint dwLayout);
315-
316-
[DllImport("User32.dll")]
317-
private static extern IntPtr GetForegroundWindow();
318-
319-
[DllImport("User32.dll")]
320-
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
321-
322-
[DllImport("user32.dll")]
323-
private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
324290

325-
[DllImport("kernel32.dll")]
326-
private static extern uint GetCurrentThreadId();
327-
328-
private static uint lastVKCode = 0;
329-
private static uint lastScanCode = 0;
330-
private static byte[] lastKeyState = new byte[255];
331-
private static bool lastIsDead = false;
332291

333292
// The reason to not use this at all is because there's a bug in here somewhere.
334293
// The purpose of this function is to get the character that was pressed. The problem
335294
// is that somewhere along the way, in certain conditions dealing with our code, it takes
336295
// the focus away from an active window that is not ours. Look at usage at line ~100
337296
// to see how it is used.
338297
#if FALSE
298+
private static uint lastVKCode = 0;
299+
private static uint lastScanCode = 0;
300+
private static byte[] lastKeyState = new byte[255];
301+
private static bool lastIsDead = false;
339302

340303
/// <summary>
341304
/// Convert VKCode to Unicode.

0 commit comments

Comments
 (0)