C# 实现屏幕键盘 (ScreenKeyboard)

摘要:
系统范围的钩子将捕捉系统中所有进程将发生的事件消息。Hook类型Windows共有14种Hooks,每一种类型的Hook可以使应用程序能够监视不同类型的系统消息处理机制。1namespaceCnBlogs.Youzai.ScreenKeyboard{7[StructLayout]8internalstructMOUSEINPUT{9publicintdx;10publicintdy;11publicintmouseData;12publicintdwFlags;13publicinttime;14publicIntPtrdwExtraInfo;15}1617[StructLayout]18internalstructKEYBDINPUT{19publicshortwVk;20publicshortwScan;21publicintdwFlags;22publicinttime;23publicIntPtrdwExtraInfo;24}2526[StructLayout]27internalstructInput{28[FieldOffset]29publicinttype;30[FieldOffset]31publicMOUSEINPUTmi;32[FieldOffset]33publicKEYBDINPUTki;34[FieldOffset]35publicHARDWAREINPUThi;36}3738[StructLayout]39internalstructHARDWAREINPUT{40publicintuMsg;41publicshortwParamL;42publicshortwParamH;43}4445internalclassINPUT{46publicconstintMOUSE=0;47publicconstintKEYBOARD=1;48publicconstintHARDWARE=2;49}5051internalstaticclassNativeMethods{52[DllImport]53internalstaticexternIntPtrGetWindowLong;5455[DllImport]56internalstaticexternIntPtrSetWindowLong;5758[DllImport]59internalstaticexternUInt32SendInput;6061[DllImport]62internalstaticexternintGetTickCount();6364[DllImport]65internalstaticexternshortGetKeyState;6667[DllImport]68internalstaticexternIntPtrSendMessage;69}70}安装钩子使用SetWindowsHookEx函数,指定一个Hook类型、自己的Hook过程是全局还是局部Hook,同时给出Hook过程的进入点,就可以轻松的安装自己的Hook过程。对于某些类型的Hook,系统将向该类的所有Hook函数发送消息,这时,Hook函数中的CallNextHookEx语句将被忽略。

原文地址:http://www.cnblogs.com/youzai/archive/2008/05/19/1202732.html

要实现一个屏幕键盘,需要监听所有键盘事件,无论窗体是否被激活。因此需要一个全局的钩子,也就
是系统范围的钩子。

什么是钩子(Hook)

钩子(Hook)是Windows提供的一种消息处理机制平台,是指在程序正常运行中接受信息之前预先
启动的函数,用来检查和修改传给该程序的信息,(钩子)实际上是一个处理消息的程序段,通
过系统调用,把它挂入系统。每当特定的消息发出,在没有到达目的窗口前,钩子程序就先捕获
该消息,亦即钩子函数先得到控制权。这时钩子函数即可以加工处理(改变)该消息,也可以不
作处理而继续传递该消息,还可以强制结束消息的传递。注意:安装钩子函数将会影响系统的性
能。监测“系统范围事件”的系统钩子特别明显。因为系统在处理所有的相关事件时都将调用您的
钩子函数,这样您的系统将会明显的减慢。所以应谨慎使用,用完后立即卸载。还有,由于您可
以预先截获其它进程的消息,所以一旦您的钩子函数出了问题的话必将影响其它的进程。

钩子的作用范围
一共有两种范围(类型)的钩子,局部的和远程的。局部钩子仅钩挂自己进程的事件。远程的钩
子还可以将钩挂其它进程发生的事件。远程的钩子又有两种: 基于线程的钩子将捕获其它进程中
某一特定线程的事件。简言之,就是可以用来观察其它进程中的某一特定线程将发生的事件。 系
统范围的钩子将捕捉系统中所有进程将发生的事件消息。

Hook 类型
Windows共有14种Hooks,每一种类型的Hook可以使应用程序能够监视不同类型的系统消息处理机
制。下面描述所有可以利用的Hook类型的发生时机。详细内容可以查阅MSDN,这里只介绍我们将要
用到的两种类型的钩子。
(1)WH_KEYBOARD_LL Hook
WH_KEYBOARD_LL Hook监视输入到线程消息队列中的键盘消息。

(2)WH_MOUSE_LL Hook
WH_MOUSE_LL Hook监视输入到线程消息队列中的鼠标消息。

下面的 class 把 API 调用封装起来以便调用。

1namespaceCnBlogs.Youzai.ScreenKeyboard{
7C# 实现屏幕键盘 (ScreenKeyboard)第1张[StructLayout(LayoutKind.Sequential)]
8C# 实现屏幕键盘 (ScreenKeyboard)第2张internalstructMOUSEINPUT{
9C# 实现屏幕键盘 (ScreenKeyboard)第1张publicintdx;
10C# 实现屏幕键盘 (ScreenKeyboard)第1张publicintdy;
11C# 实现屏幕键盘 (ScreenKeyboard)第1张publicintmouseData;
12C# 实现屏幕键盘 (ScreenKeyboard)第1张publicintdwFlags;
13C# 实现屏幕键盘 (ScreenKeyboard)第1张publicinttime;
14C# 实现屏幕键盘 (ScreenKeyboard)第1张publicIntPtrdwExtraInfo;
15C# 实现屏幕键盘 (ScreenKeyboard)第9张}
16C# 实现屏幕键盘 (ScreenKeyboard)第1张
17C# 实现屏幕键盘 (ScreenKeyboard)第1张[StructLayout(LayoutKind.Sequential)]
18C# 实现屏幕键盘 (ScreenKeyboard)第2张internalstructKEYBDINPUT{
19C# 实现屏幕键盘 (ScreenKeyboard)第1张publicshortwVk;
20C# 实现屏幕键盘 (ScreenKeyboard)第1张publicshortwScan;
21C# 实现屏幕键盘 (ScreenKeyboard)第1张publicintdwFlags;
22C# 实现屏幕键盘 (ScreenKeyboard)第1张publicinttime;
23C# 实现屏幕键盘 (ScreenKeyboard)第1张publicIntPtrdwExtraInfo;
24C# 实现屏幕键盘 (ScreenKeyboard)第9张}
25C# 实现屏幕键盘 (ScreenKeyboard)第1张
26C# 实现屏幕键盘 (ScreenKeyboard)第1张[StructLayout(LayoutKind.Explicit)]
27C# 实现屏幕键盘 (ScreenKeyboard)第2张internalstructInput{
28C# 实现屏幕键盘 (ScreenKeyboard)第1张[FieldOffset(0)]
29C# 实现屏幕键盘 (ScreenKeyboard)第1张publicinttype;
30C# 实现屏幕键盘 (ScreenKeyboard)第1张[FieldOffset(4)]
31C# 实现屏幕键盘 (ScreenKeyboard)第1张publicMOUSEINPUTmi;
32C# 实现屏幕键盘 (ScreenKeyboard)第1张[FieldOffset(4)]
33C# 实现屏幕键盘 (ScreenKeyboard)第1张publicKEYBDINPUTki;
34C# 实现屏幕键盘 (ScreenKeyboard)第1张[FieldOffset(4)]
35C# 实现屏幕键盘 (ScreenKeyboard)第1张publicHARDWAREINPUThi;
36C# 实现屏幕键盘 (ScreenKeyboard)第9张}
37C# 实现屏幕键盘 (ScreenKeyboard)第1张
38C# 实现屏幕键盘 (ScreenKeyboard)第1张[StructLayout(LayoutKind.Sequential)]
39C# 实现屏幕键盘 (ScreenKeyboard)第2张internalstructHARDWAREINPUT{
40C# 实现屏幕键盘 (ScreenKeyboard)第1张publicintuMsg;
41C# 实现屏幕键盘 (ScreenKeyboard)第1张publicshortwParamL;
42C# 实现屏幕键盘 (ScreenKeyboard)第1张publicshortwParamH;
43C# 实现屏幕键盘 (ScreenKeyboard)第9张}
44C# 实现屏幕键盘 (ScreenKeyboard)第1张
45C# 实现屏幕键盘 (ScreenKeyboard)第2张internalclassINPUT{
46C# 实现屏幕键盘 (ScreenKeyboard)第1张publicconstintMOUSE=0;
47C# 实现屏幕键盘 (ScreenKeyboard)第1张publicconstintKEYBOARD=1;
48C# 实现屏幕键盘 (ScreenKeyboard)第1张publicconstintHARDWARE=2;
49C# 实现屏幕键盘 (ScreenKeyboard)第9张}
50C# 实现屏幕键盘 (ScreenKeyboard)第1张
51C# 实现屏幕键盘 (ScreenKeyboard)第2张internalstaticclassNativeMethods{
52C# 实现屏幕键盘 (ScreenKeyboard)第1张[DllImport("User32.dll",CharSet=CharSet.Auto,SetLastError=false)]
53C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticexternIntPtrGetWindowLong(IntPtrhWnd,intnIndex);
54C# 实现屏幕键盘 (ScreenKeyboard)第1张
55C# 实现屏幕键盘 (ScreenKeyboard)第1张[DllImport("User32.dll",CharSet=CharSet.Auto,SetLastError=false)]
56C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticexternIntPtrSetWindowLong(IntPtrhWnd,intnIndex,intdwNewLong);
57C# 实现屏幕键盘 (ScreenKeyboard)第1张
58C# 实现屏幕键盘 (ScreenKeyboard)第1张[DllImport("User32.dll",EntryPoint="SendInput",CharSet=CharSet.Auto)]
59C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticexternUInt32SendInput(UInt32nInputs,Input[]pInputs,Int32cbSize);
60C# 实现屏幕键盘 (ScreenKeyboard)第1张
61C# 实现屏幕键盘 (ScreenKeyboard)第1张[DllImport("Kernel32.dll",EntryPoint="GetTickCount",CharSet=CharSet.Auto)]
62C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticexternintGetTickCount();
63C# 实现屏幕键盘 (ScreenKeyboard)第1张
64C# 实现屏幕键盘 (ScreenKeyboard)第1张[DllImport("User32.dll",EntryPoint="GetKeyState",CharSet=CharSet.Auto)]
65C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticexternshortGetKeyState(intnVirtKey);
66C# 实现屏幕键盘 (ScreenKeyboard)第1张
67C# 实现屏幕键盘 (ScreenKeyboard)第1张[DllImport("User32.dll",EntryPoint="SendMessage",CharSet=CharSet.Auto)]
68C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticexternIntPtrSendMessage(IntPtrhWnd,intmsg,IntPtrwParam,IntPtrlParam);
69C# 实现屏幕键盘 (ScreenKeyboard)第9张}
70C# 实现屏幕键盘 (ScreenKeyboard)第64张}

安装钩子
使用SetWindowsHookEx函数(API函数),指定一个Hook类型、自己的Hook过程是全局还是局部Hook,
同时给出Hook过程的进入点,就可以轻松的安装自己的Hook过程。SetWindowsHookEx总是将你的Hook函
数放置在Hook链的顶端。你可以使用CallNextHookEx函数将系统消息传递给Hook链中的下一个函数。
对于某些类型的Hook,系统将向该类的所有Hook函数发送消息,这时,
Hook函数中的CallNextHookEx语句将被忽略。全局(远程钩子)Hook函数可以拦截系统中所有线程的某
个特定的消息,为了安装一个全局Hook过程,必须在应用程序外建立一个DLL并将该Hook函数封装到其中,
应用程序在安装全局Hook过程时必须先得到该DLL模块的句柄。将Dll名传递给LoadLibrary 函数,就会得
到该DLL模块的句柄;得到该句柄 后,使用GetProcAddress函数可以得到Hook过程的地址。最后,使用
SetWindowsHookEx将 Hook过程的首址嵌入相应的Hook链中,SetWindowsHookEx传递一个模块句柄,它为
Hook过程的进入点,线程标识符置为0,该Hook过程同系统中的所有线程关联。如果是安装局部Hook此时
该Hook函数可以放置在DLL中,也可以放置在应用程序的模块段。在C#中通过平台调用(前文已经介绍过)
来调用API函数。

1publicvoidStart(boolinstallMouseHook,boolinstallKeyboardHook){
2C# 实现屏幕键盘 (ScreenKeyboard)第2张if(hMouseHook==IntPtr.Zero&&installMouseHook){
3C# 实现屏幕键盘 (ScreenKeyboard)第1张MouseHookProcedure=newHookProc(MouseHookProc);
4C# 实现屏幕键盘 (ScreenKeyboard)第1张hMouseHook=SetWindowsHookEx(
5C# 实现屏幕键盘 (ScreenKeyboard)第1张WH_MOUSE_LL,
6C# 实现屏幕键盘 (ScreenKeyboard)第1张MouseHookProcedure,
7C# 实现屏幕键盘 (ScreenKeyboard)第1张Marshal.GetHINSTANCE(
8C# 实现屏幕键盘 (ScreenKeyboard)第1张Assembly.GetExecutingAssembly().GetModules()[0]),
9C# 实现屏幕键盘 (ScreenKeyboard)第1张0
10C# 实现屏幕键盘 (ScreenKeyboard)第1张);
11C# 实现屏幕键盘 (ScreenKeyboard)第1张
12C# 实现屏幕键盘 (ScreenKeyboard)第2张if(hMouseHook==IntPtr.Zero){
13C# 实现屏幕键盘 (ScreenKeyboard)第1张interrorCode=Marshal.GetLastWin32Error();
14C# 实现屏幕键盘 (ScreenKeyboard)第1张Stop(true,false,false);
15C# 实现屏幕键盘 (ScreenKeyboard)第1张
16C# 实现屏幕键盘 (ScreenKeyboard)第1张thrownewWin32Exception(errorCode);
17C# 实现屏幕键盘 (ScreenKeyboard)第9张}
18C# 实现屏幕键盘 (ScreenKeyboard)第9张}
19C# 实现屏幕键盘 (ScreenKeyboard)第1张
20C# 实现屏幕键盘 (ScreenKeyboard)第2张if(hKeyboardHook==IntPtr.Zero&&installKeyboardHook){
21C# 实现屏幕键盘 (ScreenKeyboard)第1张KeyboardHookProcedure=newHookProc(KeyboardHookProc);
22C# 实现屏幕键盘 (ScreenKeyboard)第1张//installhook
23C# 实现屏幕键盘 (ScreenKeyboard)第1张hKeyboardHook=SetWindowsHookEx(
24C# 实现屏幕键盘 (ScreenKeyboard)第1张WH_KEYBOARD_LL,
25C# 实现屏幕键盘 (ScreenKeyboard)第1张KeyboardHookProcedure,
26C# 实现屏幕键盘 (ScreenKeyboard)第1张Marshal.GetHINSTANCE(
27C# 实现屏幕键盘 (ScreenKeyboard)第1张Assembly.GetExecutingAssembly().GetModules()[0]),
28C# 实现屏幕键盘 (ScreenKeyboard)第1张0);
29C# 实现屏幕键盘 (ScreenKeyboard)第1张//IfSetWindowsHookExfails.
30C# 实现屏幕键盘 (ScreenKeyboard)第2张if(hKeyboardHook==IntPtr.Zero){
31C# 实现屏幕键盘 (ScreenKeyboard)第1张//Returnstheerrorcodereturnedbythelast
32C# 实现屏幕键盘 (ScreenKeyboard)第1张//unmanagedfunctioncalledusingplatforminvoke
33C# 实现屏幕键盘 (ScreenKeyboard)第1张//thathastheDllImportAttribute.SetLastErrorflagset.
34C# 实现屏幕键盘 (ScreenKeyboard)第1张interrorCode=Marshal.GetLastWin32Error();
35C# 实现屏幕键盘 (ScreenKeyboard)第1张//docleanup
36C# 实现屏幕键盘 (ScreenKeyboard)第1张Stop(false,true,false);
37C# 实现屏幕键盘 (ScreenKeyboard)第1张//Initializesandthrowsanewinstanceofthe
38C# 实现屏幕键盘 (ScreenKeyboard)第1张//Win32Exceptionclasswiththespecifiederror.
39C# 实现屏幕键盘 (ScreenKeyboard)第1张thrownewWin32Exception(errorCode);
40C# 实现屏幕键盘 (ScreenKeyboard)第9张}
41C# 实现屏幕键盘 (ScreenKeyboard)第9张}
42C# 实现屏幕键盘 (ScreenKeyboard)第64张}

使用完钩子后,要进行卸载,这个可以写在析构函数中。

1publicvoidStop(){
3boolthrowExceptions){
8C# 实现屏幕键盘 (ScreenKeyboard)第1张//ifmousehooksetandmustbeuninstalled
9C# 实现屏幕键盘 (ScreenKeyboard)第2张if(hMouseHook!=IntPtr.Zero&&uninstallMouseHook){
10C# 实现屏幕键盘 (ScreenKeyboard)第1张//uninstallhook
11C# 实现屏幕键盘 (ScreenKeyboard)第1张boolretMouse=UnhookWindowsHookEx(hMouseHook);
12C# 实现屏幕键盘 (ScreenKeyboard)第1张//resetinvalidhandle
13C# 实现屏幕键盘 (ScreenKeyboard)第1张hMouseHook=IntPtr.Zero;
14C# 实现屏幕键盘 (ScreenKeyboard)第1张//iffailedandexceptionmustbethrown
15C# 实现屏幕键盘 (ScreenKeyboard)第2张if(retMouse==false&&throwExceptions){
16C# 实现屏幕键盘 (ScreenKeyboard)第1张//Returnstheerrorcodereturnedbythelastunmanagedfunction
17C# 实现屏幕键盘 (ScreenKeyboard)第1张//calledusingplatforminvokethathastheDllImportAttribute.
18C# 实现屏幕键盘 (ScreenKeyboard)第1张//SetLastErrorflagset.
19C# 实现屏幕键盘 (ScreenKeyboard)第1张interrorCode=Marshal.GetLastWin32Error();
20C# 实现屏幕键盘 (ScreenKeyboard)第1张//InitializesandthrowsanewinstanceoftheWin32Exceptionclass
21C# 实现屏幕键盘 (ScreenKeyboard)第1张//withthespecifiederror.
22C# 实现屏幕键盘 (ScreenKeyboard)第1张thrownewWin32Exception(errorCode);
23C# 实现屏幕键盘 (ScreenKeyboard)第9张}
24C# 实现屏幕键盘 (ScreenKeyboard)第9张}
25C# 实现屏幕键盘 (ScreenKeyboard)第1张
26C# 实现屏幕键盘 (ScreenKeyboard)第1张//ifkeyboardhooksetandmustbeuninstalled
27C# 实现屏幕键盘 (ScreenKeyboard)第2张if(hKeyboardHook!=IntPtr.Zero&&uninstallKeyboardHook){
28C# 实现屏幕键盘 (ScreenKeyboard)第1张//uninstallhook
29C# 实现屏幕键盘 (ScreenKeyboard)第1张boolretKeyboard=UnhookWindowsHookEx(hKeyboardHook);
30C# 实现屏幕键盘 (ScreenKeyboard)第1张//resetinvalidhandle
31C# 实现屏幕键盘 (ScreenKeyboard)第1张hKeyboardHook=IntPtr.Zero;
32C# 实现屏幕键盘 (ScreenKeyboard)第1张//iffailedandexceptionmustbethrown
33C# 实现屏幕键盘 (ScreenKeyboard)第2张if(retKeyboard==false&&throwExceptions){
34C# 实现屏幕键盘 (ScreenKeyboard)第1张//Returnstheerrorcodereturnedbythelastunmanagedfunction
35C# 实现屏幕键盘 (ScreenKeyboard)第1张//calledusingplatforminvokethathastheDllImportAttribute.
36C# 实现屏幕键盘 (ScreenKeyboard)第1张//SetLastErrorflagset.
37C# 实现屏幕键盘 (ScreenKeyboard)第1张interrorCode=Marshal.GetLastWin32Error();
38C# 实现屏幕键盘 (ScreenKeyboard)第1张//InitializesandthrowsanewinstanceoftheWin32Exceptionclass
39C# 实现屏幕键盘 (ScreenKeyboard)第1张//withthespecifiederror.
40C# 实现屏幕键盘 (ScreenKeyboard)第1张thrownewWin32Exception(errorCode);
41C# 实现屏幕键盘 (ScreenKeyboard)第9张}
42C# 实现屏幕键盘 (ScreenKeyboard)第9张}
43C# 实现屏幕键盘 (ScreenKeyboard)第64张}
44C# 实现屏幕键盘 (ScreenKeyboard)第142张

将这个文件编译成一个dll,即可在应用程序中调用。通过它提供的事件,便可监听所有的键盘事件。
但是,这只能监听键盘事件,没有键盘的情况下,怎么会有键盘事件?其实很简单,通过SendInput
API函数提供虚拟键盘代码的调用即可模拟键盘输入。下面的代码模拟一个 KeyDown 和 KeyUp 过程,
把他们连接起来就是一次按键过程。

1privatevoidSendKeyDown(shortkey){
2privatevoidSendKeyUp(shortkey){
14C# 实现屏幕键盘 (ScreenKeyboard)第1张Input[]input=newInput[1];
15C# 实现屏幕键盘 (ScreenKeyboard)第1张input[0].type=INPUT.KEYBOARD;
16C# 实现屏幕键盘 (ScreenKeyboard)第1张input[0].ki.wVk=key;
17C# 实现屏幕键盘 (ScreenKeyboard)第1张input[0].ki.dwFlags=KeyboardConstaint.KEYEVENTF_KEYUP;
18C# 实现屏幕键盘 (ScreenKeyboard)第1张input[0].ki.time=NativeMethods.GetTickCount();
19C# 实现屏幕键盘 (ScreenKeyboard)第1张
20C# 实现屏幕键盘 (ScreenKeyboard)第1张if(NativeMethods.SendInput((uint)input.Length,input,Marshal.SizeOf(input[0]))
21C# 实现屏幕键盘 (ScreenKeyboard)第2张<input.Length){
22C# 实现屏幕键盘 (ScreenKeyboard)第1张thrownewWin32Exception(Marshal.GetLastWin32Error());
23C# 实现屏幕键盘 (ScreenKeyboard)第9张}
24C# 实现屏幕键盘 (ScreenKeyboard)第64张}

自己实现一个 KeyBoardButton 控件用作按钮,用 Visual Studio 或者 SharpDevelop 为屏幕键盘设计 UI,然后
在这些 Button 的 Click 事件里面模拟一个按键过程。

1privatevoidButtonOnClick(objectsender,EventArgse){
3privatevoidSendKeyCommand(KeyboardButtonkeyButton){
12C# 实现屏幕键盘 (ScreenKeyboard)第1张shortkey=keyButton.VKCode;
13C# 实现屏幕键盘 (ScreenKeyboard)第2张if(combinationVKButtonsMap.ContainsKey(key)){
14C# 实现屏幕键盘 (ScreenKeyboard)第2张if(keyButton.Checked){
15C# 实现屏幕键盘 (ScreenKeyboard)第1张SendKeyUp(key);
16C# 实现屏幕键盘 (ScreenKeyboard)第2张}else{
17C# 实现屏幕键盘 (ScreenKeyboard)第1张SendKeyDown(key);
18C# 实现屏幕键盘 (ScreenKeyboard)第9张}
19C# 实现屏幕键盘 (ScreenKeyboard)第2张}else{
20C# 实现屏幕键盘 (ScreenKeyboard)第1张SendKeyDown(key);
21C# 实现屏幕键盘 (ScreenKeyboard)第1张SendKeyUp(key);
22C# 实现屏幕键盘 (ScreenKeyboard)第9张}
23C# 实现屏幕键盘 (ScreenKeyboard)第64张}

其中 combinationVKButtonsMap 是一个 IDictionary<short, IList<KeyboardButton>>, key 存储的是
VK_SHIFT, VK_CONTROL 等组合键的键盘码。左右两个按钮对应同一个键盘码,因此需要放在一个 List 里。
标准键盘上的每一个键都有虚拟键码( VK_CODE)与之对应。还有一些其他的常量,
把它写在一个静态 class 里吧。

1internalstaticclassKeyboardConstaint{
3C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F1=0x70;
4C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F2=0x71;
5C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F3=0x72;
6C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F4=0x73;
7C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F5=0x74;
8C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F6=0x75;
9C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F7=0x76;
10C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F8=0x77;
11C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F9=0x78;
12C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F10=0x79;
13C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F11=0x7A;
14C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_F12=0x7B;
15C# 实现屏幕键盘 (ScreenKeyboard)第1张
16C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_LEFT=0x25;
17C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_UP=0x26;
18C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_RIGHT=0x27;
19C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_DOWN=0x28;
20C# 实现屏幕键盘 (ScreenKeyboard)第1张
21C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NONE=0x00;
22C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_ESCAPE=0x1B;
23C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_EXECUTE=0x2B;
24C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_CANCEL=0x03;
25C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_RETURN=0x0D;
26C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_ACCEPT=0x1E;
27C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_BACK=0x08;
28C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_TAB=0x09;
29C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_DELETE=0x2E;
30C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_CAPITAL=0x14;
31C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMLOCK=0x90;
32C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_SPACE=0x20;
33C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_DECIMAL=0x6E;
34C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_SUBTRACT=0x6D;
35C# 实现屏幕键盘 (ScreenKeyboard)第1张
36C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_ADD=0x6B;
37C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_DIVIDE=0x6F;
38C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_MULTIPLY=0x6A;
39C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_INSERT=0x2D;
40C# 实现屏幕键盘 (ScreenKeyboard)第1张
41C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_OEM_1=0xBA;//';:'forUS
42C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_OEM_PLUS=0xBB;//'+'anycountry
43C# 实现屏幕键盘 (ScreenKeyboard)第1张
44C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_OEM_MINUS=0xBD;//'-'anycountry
45C# 实现屏幕键盘 (ScreenKeyboard)第1张
46C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_OEM_2=0xBF;//'/?'forUS
47C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_OEM_3=0xC0;//'`~'forUS
48C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_OEM_4=0xDB;//'[{'forUS
49C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_OEM_5=0xDC;//'|'forUS
50C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_OEM_6=0xDD;//']}'forUS
51C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_OEM_7=0xDE;//''"'forUS
52C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_OEM_PERIOD=0xBE;//'.>'anycountry
53C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_OEM_COMMA=0xBC;//',<'anycountry
54C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_SHIFT=0x10;
55C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_CONTROL=0x11;
56C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_MENU=0x12;
57C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_LWIN=0x5B;
58C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_RWIN=0x5C;
59C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_APPS=0x5D;
60C# 实现屏幕键盘 (ScreenKeyboard)第1张
61C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_LSHIFT=0xA0;
62C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_RSHIFT=0xA1;
63C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_LCONTROL=0xA2;
64C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_RCONTROL=0xA3;
65C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_LMENU=0xA4;
66C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_RMENU=0xA5;
67C# 实现屏幕键盘 (ScreenKeyboard)第1张
68C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_SNAPSHOT=0x2C;
69C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_SCROLL=0x91;
70C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_PAUSE=0x13;
71C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_HOME=0x24;
72C# 实现屏幕键盘 (ScreenKeyboard)第1张
73C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NEXT=0x22;
74C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_PRIOR=0x21;
75C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_END=0x23;
76C# 实现屏幕键盘 (ScreenKeyboard)第1张
77C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMPAD0=0x60;
78C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMPAD1=0x61;
79C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMPAD2=0x62;
80C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMPAD3=0x63;
81C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMPAD4=0x64;
82C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMPAD5=0x65;
83C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMPAD5NOTHING=0x0C;
84C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMPAD6=0x66;
85C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMPAD7=0x67;
86C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMPAD8=0x68;
87C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortVK_NUMPAD9=0x69;
88C# 实现屏幕键盘 (ScreenKeyboard)第1张
89C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortKEYEVENTF_EXTENDEDKEY=0x0001;
90C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyshortKEYEVENTF_KEYUP=0x0002;
91C# 实现屏幕键盘 (ScreenKeyboard)第1张
92C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyintGWL_EXSTYLE=-20;
93C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyintWS_DISABLED=0X8000000;
94C# 实现屏幕键盘 (ScreenKeyboard)第1张internalstaticreadonlyintWM_SETFOCUS=0X0007;
95C# 实现屏幕键盘 (ScreenKeyboard)第64张}

屏幕键盘必须是一个不能获得输入焦点的窗体,在这个窗体的构造函数里,可以安装
一个全局鼠标钩子,再通过调用 SetWindowLong API 函数完成。

1privatevoidHookOnMouseActivity(objectsener,HookEx.MouseExEventArgse){
5C# 实现屏幕键盘 (ScreenKeyboard)第1张Pointlocation=e.Location;
6C# 实现屏幕键盘 (ScreenKeyboard)第1张
7C# 实现屏幕键盘 (ScreenKeyboard)第2张if(e.Button==MouseButtons.Left){
8C# 实现屏幕键盘 (ScreenKeyboard)第1张RectanglecaptionRect=newRectangle(this.Location,newSize(this.Width,
9C# 实现屏幕键盘 (ScreenKeyboard)第1张SystemInformation.CaptionHeight));
10C# 实现屏幕键盘 (ScreenKeyboard)第2张if(captionRect.Contains(location)){
11C# 实现屏幕键盘 (ScreenKeyboard)第1张NativeMethods.SetWindowLong(this.Handle,KeyboardConstaint.GWL_EXSTYLE,
12C# 实现屏幕键盘 (ScreenKeyboard)第1张(int)NativeMethods.GetWindowLong(this.Handle,KeyboardConstaint.GWL_EXSTYLE)
13C# 实现屏幕键盘 (ScreenKeyboard)第1张&(~KeyboardConstaint.WS_DISABLED));
14C# 实现屏幕键盘 (ScreenKeyboard)第1张NativeMethods.SendMessage(this.Handle,KeyboardConstaint.WM_SETFOCUS,IntPtr.Zero,IntPtr.Zero);
15C# 实现屏幕键盘 (ScreenKeyboard)第2张}else{
16C# 实现屏幕键盘 (ScreenKeyboard)第1张NativeMethods.SetWindowLong(this.Handle,KeyboardConstaint.GWL_EXSTYLE,
17C# 实现屏幕键盘 (ScreenKeyboard)第1张(int)NativeMethods.GetWindowLong(this.Handle,KeyboardConstaint.GWL_EXSTYLE)|
18C# 实现屏幕键盘 (ScreenKeyboard)第1张KeyboardConstaint.WS_DISABLED);
19C# 实现屏幕键盘 (ScreenKeyboard)第9张}
20C# 实现屏幕键盘 (ScreenKeyboard)第9张}
21C# 实现屏幕键盘 (ScreenKeyboard)第64张}

鼠标单击标题栏,让屏幕键盘可以接收焦点,并激活,单击其他部分则不激活窗体(如果激活了,其他程序必然取消激活,
输入就无法进行了),这样才可以进行输入,并且保证了可以拖动窗体到其他位置。

至此,一个屏幕键盘程序差不多完成了,能够实现与实际键盘完全同步。至于窗体,按键重绘,以及 Num Lock, Caps Lock,
Scroll Lock 等键盘灯的模拟,这里就不讲了,如果有兴趣,可以下载完整的代码。最后我们的屏幕键盘程序运行的效果如
下图:

C# 实现屏幕键盘 (ScreenKeyboard)第276张

点击下载完整源代码

免责声明:文章转载自《C# 实现屏幕键盘 (ScreenKeyboard)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇mysqldump常用操作zabbix监控交换机下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

MYSQL连接字符串参数详细解析(大全参考)

阅读目录 Connector/Net Connection String Options Reference 回到目录 Connector/Net Connection String Options Reference Database=dbname;Data Source=192.168.1.1;Port=3306;User Id=root;Pa...

winform窗体 小程序【移动窗体和阴影】

窗体无边框设置后无法移动,引用API 使其获得功能 移动 //窗体移动API [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPt...

ckeditor如何能实现直接粘贴把图片上传到服务器中

如何做到 ueditor批量上传word图片? 1、前端引用代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="ht...

java汉字乱码解决办法

自从接触Java和JSP以来,就不断与Java的中文乱码问题打交道,现在终于得到了彻底的解决,现将我们的解决心得与大家共享。一、Java中文问题的由来Java的内核和class文件是基于unicode的,这使Java程序具有良好的跨平台性,但也带来了一些中文乱码问题的麻烦。原因主要有两方面,Java和JSP文件本身编译时产生的乱码问题和Java程序于其...

javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp

公司新做了一个项目,需要用到邮件发送功能。因为以前同事在其他项目上做过邮件模块,所以直接移植过来了。 本以为很简单的功能,直接copy多好。然而命运多舛。 调用发送邮件的代码时候报错。 主要代码: Properties props = new Properties(); props.put("mail.smtp.host", host); props.pu...

mysql数据库编码问题

一:插入数据乱码 ①:数据库的字符集不对,需要修改成utf-8;如果解决不了走第二步 ②: var cmd = new MySqlCommand(“set names utf8”);如果解决不了走第三步 ③:把“Charset=utf8”加载数据库连接字符串后边即可。在解决不了没辙了,换数据库吧!  二:获取有乱码问题  Incorrect strin...