-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathKeyboardIndicator.cs
More file actions
141 lines (121 loc) · 4.5 KB
/
Copy pathKeyboardIndicator.cs
File metadata and controls
141 lines (121 loc) · 4.5 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
138
139
140
141
using KeyboardIndicator.Properties;
using System;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace KeyboardIndicator
{
public partial class KeyboardIndicator : Form
{
public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public class KeyBoardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 256;
private const int NUM_KEYCODE = 144;//Num Lock键:VK_NUMLOCK (144)
private const int CAPS_KEYCODE = 20;//Caps Lock键:VK_CAPITAL (20)
private KeyboardIndicator.KeyBoardHookStruct kbh;
private KeyboardIndicator.HookProc gHookProc;
private int curCount;
private bool isRunning;
[DllImport("user32.dll")]
public static extern short GetKeyState(int nVirtKey);
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(int idHook, KeyboardIndicator.HookProc lpfn, IntPtr hInstance, int threadId);
private bool showNumLock = false;
private bool showCapsLock = false;
public KeyboardIndicator()
{
InitializeComponent();
try
{
showNumLock = ConfigurationManager.AppSettings["NumLock"].ToUpper() == "Y";
}
catch (Exception)
{
showNumLock = false;
}
try
{
showCapsLock = ConfigurationManager.AppSettings["CapsLock"].ToUpper() == "Y";
}
catch (Exception)
{
showCapsLock = false;
}
if (!showNumLock && !showCapsLock)
{
showNumLock = true;
}
this.SetStatus();
this.gHookProc = new KeyboardIndicator.HookProc(this.KeyBoardHookProc);
KeyboardIndicator.SetWindowsHookEx(WH_KEYBOARD_LL, this.gHookProc, IntPtr.Zero, 0);
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Close();
this.Dispose(true);
Application.ExitThread();
}
public int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
{
this.kbh = (KeyboardIndicator.KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardIndicator.KeyBoardHookStruct));
if (!this.isRunning && (this.kbh.vkCode == NUM_KEYCODE || this.kbh.vkCode == CAPS_KEYCODE))
{
ThreadPool.QueueUserWorkItem(delegate(object param0)
{
this.isRunning = true;
this.curCount = 200;
while (this.curCount > 0)
{
this.SetStatus();
Thread.Sleep(20);
this.curCount--;
}
this.curCount = 0;
this.isRunning = false;
});
}
return 0;
}
private void SetStatus()
{
this.notifyIconNUM.Visible = showNumLock;
this.notifyIconCAPS.Visible = showCapsLock;
if (showNumLock)
{
if (KeyboardIndicator.GetKeyState(NUM_KEYCODE) != 0)
{
this.notifyIconNUM.Icon = Resources.NumLockOpen;
this.notifyIconNUM.Text = "NumLock On";
}
else
{
this.notifyIconNUM.Icon = Resources.NumLockClose;
this.notifyIconNUM.Text = "NumLock Off";
}
}
if (showCapsLock)
{
if (KeyboardIndicator.GetKeyState(CAPS_KEYCODE) != 0)
{
this.notifyIconCAPS.Icon = Resources.CapsLockOpen;
this.notifyIconCAPS.Text = "CapsLock On";
}
else
{
this.notifyIconCAPS.Icon = Resources.CapsLockClose;
this.notifyIconCAPS.Text = "CapsLock Off";
}
}
}
}
}