以前使用的显示终端设备,没有配备大键盘,只有一个数字小键盘,是用来输密码的。以后客户有需求,需要控制屏幕提示信息的显示,敏感信息他们想显示的时候就显示,不想显示的时候就显示默认信息。由于软件界面是全屏的,也不能给客户配个鼠标在界面上加上菜单,让他们去点。我就利用了数字小键盘上的Num Lock键,灯亮的时候就是显示信息,他们不想显示的时候就把数字小键盘上的NUM灯按灭,这样也很直观,方便。要达到这一效果首先要能获取Num Lock键的状态,还要能控制Num Lock键的亮灭。
我用C#实现,因为要调用API, 首先引用using System.Runtime.InteropServices;
然后是声明:
1: /// <summary>
2: /// 获取键状态
3: /// </summary>
4: /// <param name="keyCode"></param>
5: /// <returns></returns>
6: [DllImport("user32.dll",CharSet = CharSet.Auto,
7: ExactSpelling = true,
8: CallingConvention = CallingConvention.Winapi)]
9: public static extern short GetKeyState(int keyCode);
10:
11:
12: /// <summary>
13: /// 改变键状态
14: /// </summary>
15: /// <param name="bVk"></param>
16: /// <param name="bScan"></param>
17: /// <param name="dwFlags"></param>
18: /// <param name="dwExtraInfo"></param>
19: [DllImport("user32.dll")]
20: static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
由于键盘上的Caps Lock和Scroll Lock和Num Lock类型是一样的,就把其它两个键一起说了,省了用到了再去乱找。三个键的常量声明:
1: //按键状态
2: const int KEYEVENTF_EXTENDEDKEY = 0x1;
3: const int KEYEVENTF_KEYUP = 0x2;
4:
5: //键值
6: const int VK_NUMLOCK = 0x90;
7: const int VK_CAPITAL=0x14;
8: const int VK_SCROLL = 0x91;
获取键状态和设置键状态:
1: //获取键状态
2: bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;
3: bool NumLock = (((ushort)GetKeyState(0x90)) & 0xffff) != 0;
4: bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;
1: //设置NUM Lock
2: private void btnNum_Click(object sender, EventArgs e)
3: {
4: keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
5: keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
6:
7: Application.DoEvents();
8: ViewKeyState();
9:
10: }
11:
12: //设置Caps Lock
13: private void btnCaps_Click(object sender, EventArgs e)
14: {
15:
16: keybd_event(VK_CAPITAL, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
17: keybd_event(VK_CAPITAL, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
18:
19:
20: Application.DoEvents();
21: ViewKeyState();
22: }
23:
24: //设置Scroll Lock
25: private void btnScroll_Click(object sender, EventArgs e)
26: {
27:
28: keybd_event(VK_SCROLL, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
29: keybd_event(VK_SCROLL, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
30:
31: Application.DoEvents();
32: ViewKeyState();
33: }
ViewKeyState方法是刷新三个灯的状态以及按键显示文字。
1: /// <summary>
2: /// 显示状态
3: /// </summary>
4: private void ViewKeyState()
5: {
6: //获取键状态
7: bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;
8: bool NumLock = (((ushort)GetKeyState(0x90)) & 0xffff) != 0;
9: bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;
10:
11: //根据键状态设置按钮文字
12: btnCaps.Text= CapsLock ? "关闭":"开启";
13: btnNum.Text = NumLock ? "关闭" : "开启";
14: btnScroll.Text = ScrollLock ? "关闭" : "开启";
15:
16: //显示状态
17: rdbCapsLock.Checked= CapsLock;
18: rdbNumLock.Checked= NumLock;
19: rdbScrollLock.Checked= ScrollLock;
20: }
可以把Windows自带的虚拟键盘调用出来看一下效果:
声明:由于是一年多前做的东西了,也忘了参考的那些网站,不能给出链接,在此表示歉意。