Skip to content

Commit 668bec3

Browse files
committed
Windows hooks in seperate file
1 parent 883bd9b commit 668bec3

7 files changed

Lines changed: 116 additions & 92 deletions

File tree

Live-WPM/FormWPM.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Live-WPM/FormWPM.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using System.Windows.Forms;
1010
using System.Windows.Input;
1111

12-
namespace Stx
12+
namespace LiveWPM
1313
{
1414
public partial class FormWPM : Form
1515
{
@@ -22,11 +22,23 @@ public partial class FormWPM : Form
2222
public FormWPM()
2323
{
2424
InitializeComponent();
25+
FormClosing += FormWPM_FormClosing;
26+
}
27+
28+
private void FormWPM_FormClosing(object sender, FormClosingEventArgs e)
29+
{
30+
WindowsKeyboardHook.OnGlobalKey -= WindowsKeyboardHook_OnGlobalKey;
2531
}
2632

2733
private void FormWPM_Load(object sender, EventArgs e)
2834
{
2935
history = new int[MOVING_AVERAGE];
36+
WindowsKeyboardHook.OnGlobalKey += WindowsKeyboardHook_OnGlobalKey;
37+
}
38+
39+
private void WindowsKeyboardHook_OnGlobalKey(Keys obj)
40+
{
41+
currentKeyPresses++;
3042
}
3143

3244
private void ShiftHistory(int lastValue)

Live-WPM/Live-WPM.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
77
<ProjectGuid>{8B2D0D71-7DD8-4DB7-BA5D-185B676245CA}</ProjectGuid>
88
<OutputType>WinExe</OutputType>
9-
<RootNamespace>life_wpm</RootNamespace>
10-
<AssemblyName>life-wpm</AssemblyName>
9+
<RootNamespace>LiveWPM</RootNamespace>
10+
<AssemblyName>LiveWPM</AssemblyName>
1111
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
1212
<FileAlignment>512</FileAlignment>
1313
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
@@ -55,6 +55,7 @@
5555
</Compile>
5656
<Compile Include="Program.cs" />
5757
<Compile Include="Properties\AssemblyInfo.cs" />
58+
<Compile Include="WindowsKeyboardHook.cs" />
5859
<EmbeddedResource Include="FormWPM.resx">
5960
<DependentUpon>FormWPM.cs</DependentUpon>
6061
</EmbeddedResource>
@@ -66,6 +67,7 @@
6667
<Compile Include="Properties\Resources.Designer.cs">
6768
<AutoGen>True</AutoGen>
6869
<DependentUpon>Resources.resx</DependentUpon>
70+
<DesignTime>True</DesignTime>
6971
</Compile>
7072
<None Include="Properties\Settings.settings">
7173
<Generator>SettingsSingleFileGenerator</Generator>

Live-WPM/Program.cs

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
using System.Threading.Tasks;
77
using System.Windows.Forms;
88

9-
namespace Stx
9+
namespace LiveWPM
1010
{
1111
static class Program
1212
{
13-
private static FormWPM formWPM;
14-
1513
/// <summary>
1614
/// The main entry point for the application.
1715
/// </summary>
@@ -20,52 +18,10 @@ static void Main()
2018
{
2119
Application.EnableVisualStyles();
2220
Application.SetCompatibleTextRenderingDefault(false);
23-
formWPM = new FormWPM();
24-
_hookID = SetHook(_proc);
25-
Application.Run(formWPM);
26-
UnhookWindowsHookEx(_hookID);
21+
WindowsKeyboardHook.EnableHook();
22+
Application.Run(new FormWPM());
23+
WindowsKeyboardHook.DisableHook();
2724
}
2825

29-
private const int WH_KEYBOARD_LL = 13;
30-
private const int WM_KEYDOWN = 0x0100;
31-
private static LowLevelKeyboardProc _proc = HookCallback;
32-
private static IntPtr _hookID = IntPtr.Zero;
33-
34-
private static IntPtr SetHook(LowLevelKeyboardProc proc)
35-
{
36-
using (Process curProcess = Process.GetCurrentProcess())
37-
using (ProcessModule curModule = curProcess.MainModule)
38-
{
39-
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
40-
GetModuleHandle(curModule.ModuleName), 0);
41-
}
42-
}
43-
44-
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
45-
46-
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
47-
{
48-
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
49-
{
50-
//int vkCode = Marshal.ReadInt32(lParam);
51-
//Keys k = (Keys)vkCode;
52-
formWPM.currentKeyPresses++;
53-
}
54-
55-
return CallNextHookEx(_hookID, nCode, wParam, lParam);
56-
}
57-
58-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
59-
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
60-
61-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
62-
[return: MarshalAs(UnmanagedType.Bool)]
63-
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
64-
65-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
66-
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
67-
68-
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
69-
private static extern IntPtr GetModuleHandle(string lpModuleName);
7026
}
7127
}

Live-WPM/Properties/Resources.Designer.cs

Lines changed: 19 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Live-WPM/Properties/Settings.Designer.cs

Lines changed: 9 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Live-WPM/WindowsKeyboardHook.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Windows.Forms;
9+
10+
namespace LiveWPM
11+
{
12+
public static class WindowsKeyboardHook
13+
{
14+
private const int WH_KEYBOARD_LL = 13;
15+
private const int WM_KEYDOWN = 0x0100;
16+
private static IntPtr hookPtr = IntPtr.Zero;
17+
18+
public static event Action<Keys> OnGlobalKey;
19+
20+
public static void EnableHook()
21+
{
22+
hookPtr = SetHook(HookCallback);
23+
}
24+
25+
public static void DisableHook()
26+
{
27+
UnhookWindowsHookEx(hookPtr);
28+
}
29+
30+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
31+
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
32+
33+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
34+
[return: MarshalAs(UnmanagedType.Bool)]
35+
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
36+
37+
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
38+
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
39+
40+
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
41+
private static extern IntPtr GetModuleHandle(string lpModuleName);
42+
43+
private static IntPtr SetHook(LowLevelKeyboardProc proc)
44+
{
45+
using (Process curProcess = Process.GetCurrentProcess())
46+
using (ProcessModule curModule = curProcess.MainModule)
47+
{
48+
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
49+
GetModuleHandle(curModule.ModuleName), 0);
50+
}
51+
}
52+
53+
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
54+
55+
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
56+
{
57+
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
58+
{
59+
int vkCode = Marshal.ReadInt32(lParam);
60+
OnGlobalKey.Invoke((Keys)vkCode);
61+
}
62+
63+
return CallNextHookEx(hookPtr, nCode, wParam, lParam);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)