由于我笨并买了一个没有任何指示灯的键盘,我决定写一个快速程序来弹出Caps,Scroll和Num Lock的状态。
我决定使用WinForms,因为这是我很满意的,而我的主要表单类似乎是这样:
globalKeyboardHook gkh = new globalKeyboardHook();
public Indicators()
{
InitializeComponent();
}
public void CheckKeyCaps()
{
notifyIcon1.BalloonTipText = (IsKeyLocked(Keys.CapsLock)) ? "Caps Lock: ON" : "Caps Lock: OFF";
notifyIcon1.ShowBalloonTip(100);
}
public void CheckKeyScroll()
{
notifyIcon1.BalloonTipText = (IsKeyLocked(Keys.Scroll)) ? "Scroll Lock: ON" : "Scroll Lock: OFF";
notifyIcon1.ShowBalloonTip(100);
}
public void CheckKeyNum()
{
notifyIcon1.BalloonTipText = IsKeyLocked(Keys.NumLock) ? "Num Lock: ON" : "Num Lock: OFF";
notifyIcon1.ShowBalloonTip(100);
}
private void IndicatorsLoad(object sender, EventArgs e)
{
gkh.HookedKeys.Add(Keys.Scroll);
gkh.HookedKeys.Add(Keys.CapsLock);
gkh.HookedKeys.Add(Keys.NumLock);
gkh.KeyUp += gkh_KeyUp;
Visible = false;
ShowInTaskbar = false;
notifyIcon1.Icon = new Icon(SystemIcons.Application, 40, 40);
}
public void gkh_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Scroll:
CheckKeyScroll();
break;
case Keys.CapsLock:
CheckKeyCaps();
break;
case Keys.NumLock:
CheckKeyNum();
break;
}
}
我使用this library添加键盘钩子,现在它只会切换CapsLock键的弹出窗口。按Num或Scroll Lock只会使弹出窗口显示“Scroll Lock:ON”或“Num Lock:ON”。
我已经尝试使用交换机上的断点运行调试器,事件处理程序,我无法做出正面或反面。我对编程比较新(6个月),所以我假设我在某个地方犯了错误,但我不够精明,无法弄明白。