原文地址:http://www.cnblogs.com/youzai/archive/2008/05/19/1202732.htmlhtml
要实现一个屏幕键盘,须要监听全部键盘事件,不管窗体是否被激活。所以须要一个全局的钩子,也就
是系统范围的钩子。函数
什么是钩子(Hook)ui
钩子(Hook)是Windows提供的一种消息处理机制平台,是指在程序正常运行中接受信息以前预先
启动的函数,用来检查和修改传给该程序的信息,(钩子)其实是一个处理消息的程序段,通
过系统调用,把它挂入系统。每当特定的消息发出,在没有到达目的窗口前,钩子程序就先捕获
该消息,亦即钩子函数先获得控制权。这时钩子函数便可以加工处理(改变)该消息,也能够不
做处理而继续传递该消息,还能够强制结束消息的传递。注意:安装钩子函数将会影响系统的性
能。监测“系统范围事件”的系统钩子特别明显。由于系统在处理全部的相关事件时都将调用您的
钩子函数,这样您的系统将会明显的减慢。因此应谨慎使用,用完后当即卸载。还有,因为您可
以预先截获其它进程的消息,因此一旦您的钩子函数出了问题的话必将影响其它的进程。this
钩子的做用范围
一共有两种范围(类型)的钩子,局部的和远程的。局部钩子仅钩挂本身进程的事件。远程的钩
子还能够将钩挂其它进程发生的事件。远程的钩子又有两种: 基于线程的钩子将捕获其它进程中
某一特定线程的事件。简言之,就是能够用来观察其它进程中的某一特定线程将发生的事件。 系
统范围的钩子将捕捉系统中全部进程将发生的事件消息。 spa
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 调用封装起来以便调用。
code
1

// NativeMethods.cs
2

using System;
3

using System.Runtime.InteropServices;
4

using System.Drawing;
5
6

namespace CnBlogs.Youzai.ScreenKeyboard
{
7
[StructLayout(LayoutKind.Sequential)]
8
internal struct MOUSEINPUT {
9
public int dx;
10
public int dy;
11
public int mouseData;
12
public int dwFlags;
13
public int time;
14
public IntPtr dwExtraInfo;
15
}
16
17
[StructLayout(LayoutKind.Sequential)]
18
internal struct KEYBDINPUT {
19
public short wVk;
20
public short wScan;
21
public int dwFlags;
22
public int time;
23
public IntPtr dwExtraInfo;
24
}
25
26
[StructLayout(LayoutKind.Explicit)]
27
internal struct Input {
28
[FieldOffset(0)]
29
public int type;
30
[FieldOffset(4)]
31
public MOUSEINPUT mi;
32
[FieldOffset(4)]
33
public KEYBDINPUT ki;
34
[FieldOffset(4)]
35
public HARDWAREINPUT hi;
36
}
37
38
[StructLayout(LayoutKind.Sequential)]
39
internal struct HARDWAREINPUT {
40
public int uMsg;
41
public short wParamL;
42
public short wParamH;
43
}
44
45
internal class INPUT {
46
public const int MOUSE = 0;
47
public const int KEYBOARD = 1;
48
public const int HARDWARE = 2;
49
}
50
51
internal static class NativeMethods {
52
[DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
53
internal static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
54
55
[DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
56
internal static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
57
58
[DllImport("User32.dll", EntryPoint = "SendInput", CharSet = CharSet.Auto)]
59
internal static extern UInt32 SendInput(UInt32 nInputs, Input[] pInputs, Int32 cbSize);
60
61
[DllImport("Kernel32.dll", EntryPoint = "GetTickCount", CharSet = CharSet.Auto)]
62
internal static extern int GetTickCount();
63
64
[DllImport("User32.dll", EntryPoint = "GetKeyState", CharSet = CharSet.Auto)]
65
internal static extern short GetKeyState(int nVirtKey);
66
67
[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
68
internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
69
}
70
}
安装钩子
使用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函数。orm
1

public void Start(bool installMouseHook, bool installKeyboardHook)
{
2
if (hMouseHook == IntPtr.Zero && installMouseHook) {
3
MouseHookProcedure = new HookProc(MouseHookProc);
4
hMouseHook = SetWindowsHookEx(
5
WH_MOUSE_LL,
6
MouseHookProcedure,
7
Marshal.GetHINSTANCE(
8
Assembly.GetExecutingAssembly().GetModules()[0]),
9
0
10
);
11
12
if (hMouseHook == IntPtr.Zero) {
13
int errorCode = Marshal.GetLastWin32Error();
14
Stop(true, false, false);
15
16
throw new Win32Exception(errorCode);
17
}
18
}
19
20
if (hKeyboardHook == IntPtr.Zero && installKeyboardHook) {
21
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
22
//install hook
23
hKeyboardHook = SetWindowsHookEx(
24
WH_KEYBOARD_LL,
25
KeyboardHookProcedure,
26
Marshal.GetHINSTANCE(
27
Assembly.GetExecutingAssembly().GetModules()[0]),
28
0);
29
// If SetWindowsHookEx fails.
30
if (hKeyboardHook == IntPtr.Zero) {
31
// Returns the error code returned by the last
32
// unmanaged function called using platform invoke
33
// that has the DllImportAttribute.SetLastError flag set.
34
int errorCode = Marshal.GetLastWin32Error();
35
//do cleanup
36
Stop(false, true, false);
37
//Initializes and throws a new instance of the
38
// Win32Exception class with the specified error.
39
throw new Win32Exception(errorCode);
40
}
41
}
42
}
使用完钩子后,要进行卸载,这个能够写在析构函数中。htm
将这个文件编译成一个dll,便可在应用程序中调用。经过它提供的事件,即可监听全部的键盘事件。
可是,这只能监听键盘事件,没有键盘的状况下,怎么会有键盘事件?其实很简单,经过SendInput
API函数提供虚拟键盘代码的调用便可模拟键盘输入。下面的代码模拟一个 KeyDown 和 KeyUp 过程,
把他们链接起来就是一次按键过程。
本身实现一个 KeyBoardButton 控件用做按钮,用 Visual Studio 或者 SharpDevelop 为屏幕键盘设计 UI,而后
在这些 Button 的 Click 事件里面模拟一个按键过程。
其中 combinationVKButtonsMap 是一个 IDictionary<short, IList<KeyboardButton>>, key 存储的是
VK_SHIFT, VK_CONTROL 等组合键的键盘码。左右两个按钮对应同一个键盘码,所以须要放在一个 List 里。
标准键盘上的每个键都有虚拟键码( VK_CODE)与之对应。还有一些其余的常量,
把它写在一个静态 class 里吧。
屏幕键盘必须是一个不能得到输入焦点的窗体,在这个窗体的构造函数里,能够安装
一个全局鼠标钩子,再经过调用 SetWindowLong API 函数完成。
1

UserActivityHook hook = new UserActivityHook(true, true);
2

hook.MouseActivity += HookOnMouseActivity;
3
4

private void HookOnMouseActivity(object sener, HookEx.MouseExEventArgs e)
{
5
Point location = e.Location;
6
7
if (e.Button == MouseButtons.Left) {
8
Rectangle captionRect = new Rectangle(this.Location, new Size(this.Width,
9
SystemInformation.CaptionHeight));
10
if (captionRect.Contains(location)) {
11
NativeMethods.SetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE,
12
(int)NativeMethods.GetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE)
13
& (~KeyboardConstaint.WS_DISABLED));
14
NativeMethods.SendMessage(this.Handle, KeyboardConstaint.WM_SETFOCUS, IntPtr.Zero, IntPtr.Zero);
15
} else {
16
NativeMethods.SetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE,
17
(int)NativeMethods.GetWindowLong(this.Handle, KeyboardConstaint.GWL_EXSTYLE) |
18
KeyboardConstaint.WS_DISABLED);
19
}
20
}
21
}
鼠标单击标题栏,让屏幕键盘能够接收焦点,并激活,单击其余部分则不激活窗体(若是激活了,其余程序必然取消激活,
输入就没法进行了),这样才能够进行输入,而且保证了能够拖动窗体到其余位置。
至此,一个屏幕键盘程序差很少完成了,可以实现与实际键盘彻底同步。至于窗体,按键重绘,以及 Num Lock, Caps Lock,
Scroll Lock 等键盘灯的模拟,这里就不讲了,若是有兴趣,能够下载完整的代码。最后咱们的屏幕键盘程序运行的效果如
下图:

点击下载完整源代码
