Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (117 loc) · 4.37 KB
/
Copy pathProgram.cs
File metadata and controls
137 lines (117 loc) · 4.37 KB
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
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
namespace KeyLocker
{
static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new TrayAppContext());
}
}
public class TrayAppContext : ApplicationContext
{
private NotifyIcon trayIcon;
private ToolStripMenuItem toggleItem;
private KeyboardHook hook;
private bool locked = false;
public TrayAppContext()
{
hook = new KeyboardHook();
var menu = new ContextMenuStrip();
toggleItem = new ToolStripMenuItem("Lock Keyboard", null, OnToggle);
menu.Items.Add(toggleItem);
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add("Exit", null, OnExit);
trayIcon = new NotifyIcon
{
Icon = SystemIcons.Shield,
Text = "KeyLocker - Unlocked",
Visible = true,
ContextMenuStrip = menu
};
// Left-click tray icon also toggles (quick access)
trayIcon.MouseClick += (s, e) =>
{
if (e.Button == MouseButtons.Left)
ToggleLock();
};
}
private void OnToggle(object sender, EventArgs e) => ToggleLock();
private void ToggleLock()
{
locked = !locked;
if (locked)
{
hook.Install();
trayIcon.Icon = SystemIcons.Error; // red-ish visual cue
trayIcon.Text = "KeyLocker - LOCKED (click to unlock)";
toggleItem.Text = "Unlock Keyboard";
trayIcon.ShowBalloonTip(1500, "KeyLocker", "Keyboard is now LOCKED. Click tray icon to unlock.", ToolTipIcon.Info);
}
else
{
hook.Uninstall();
trayIcon.Icon = SystemIcons.Shield;
trayIcon.Text = "KeyLocker - Unlocked";
toggleItem.Text = "Lock Keyboard";
trayIcon.ShowBalloonTip(1500, "KeyLocker", "Keyboard unlocked.", ToolTipIcon.Info);
}
}
private void OnExit(object sender, EventArgs e)
{
hook.Uninstall();
trayIcon.Visible = false;
Application.Exit();
}
}
public class KeyboardHook
{
private const int WH_KEYBOARD_LL = 13;
private IntPtr hookId = IntPtr.Zero;
private LowLevelKeyboardProc proc;
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
public KeyboardHook()
{
proc = HookCallback;
}
public void Install()
{
if (hookId != IntPtr.Zero) return;
using (var curProcess = System.Diagnostics.Process.GetCurrentProcess())
using (var curModule = curProcess.MainModule)
{
hookId = SetWindowsHookEx(WH_KEYBOARD_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
public void Uninstall()
{
if (hookId == IntPtr.Zero) return;
UnhookWindowsHookEx(hookId);
hookId = IntPtr.Zero;
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
// Swallow every key event: return non-zero (1) instead of calling next hook.
if (nCode >= 0)
{
return (IntPtr)1;
}
return CallNextHookEx(hookId, nCode, wParam, lParam);
}
[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);
}
}