C# 實現螢幕鍵盤 (ScreenKeyboard)
要實現一個螢幕鍵盤,需要監聽所有鍵盤事件,無論窗體是否被啟用。因此需要一個全域性的鉤子,也就
是系統範圍的鉤子。
什麼是鉤子(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 呼叫封裝起來以便呼叫。
1// NativeMethods.cs
2using System;
3using System.Runtime.InteropServices;
4using System.Drawing;
5
6namespace 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函式。
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 }
使用完鉤子後,要進行解除安裝,這個可以寫在解構函式中。
1
2 public void Stop() {
3 this.Stop(true, true, true);
4 }
5
6 public void Stop(bool uninstallMouseHook, bool uninstallKeyboardHook,
7 bool throwExceptions) {
8 // if mouse hook set and must be uninstalled
9 if (hMouseHook != IntPtr.Zero && uninstallMouseHook) {
10 // uninstall hook
11 bool retMouse = UnhookWindowsHookEx(hMouseHook);
12 // reset invalid handle
13 hMouseHook = IntPtr.Zero;
14 // if failed and exception must be thrown
15 if (retMouse == false && throwExceptions) {
16 // Returns the error code returned by the last unmanaged function
17 // called using platform invoke that has the DllImportAttribute.
18 // SetLastError flag set.
19 int errorCode = Marshal.GetLastWin32Error();
20 // Initializes and throws a new instance of the Win32Exception class
21 // with the specified error.
22 throw new Win32Exception(errorCode);
23 }
24 }
25
26 // if keyboard hook set and must be uninstalled
27 if (hKeyboardHook != IntPtr.Zero && uninstallKeyboardHook) {
28 // uninstall hook
29 bool retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
30 // reset invalid handle
31 hKeyboardHook = IntPtr.Zero;
32 // if failed and exception must be thrown
33 if (retKeyboard == false && throwExceptions) {
34 // Returns the error code returned by the last unmanaged function
35 // called using platform invoke that has the DllImportAttribute.
36 // SetLastError flag set.
37 int errorCode = Marshal.GetLastWin32Error();
38 // Initializes and throws a new instance of the Win32Exception class
39 // with the specified error.
40 throw new Win32Exception(errorCode);
41 }
42 }
43 }
44
將這個檔案編譯成一個dll,即可在應用程式中呼叫。通過它提供的事件,便可監聽所有的鍵盤事件。
但是,這隻能監聽鍵盤事件,沒有鍵盤的情況下,怎麼會有鍵盤事件?其實很簡單,通過SendInput
API函式提供虛擬鍵盤程式碼的呼叫即可模擬鍵盤輸入。下面的程式碼模擬一個 KeyDown 和 KeyUp 過程,
把他們連線起來就是一次按鍵過程。
1 private void SendKeyDown(short key) {
2 Input[] input = new Input[1];
3 input[0].type = INPUT.KEYBOARD;
4 input[0].ki.wVk = key;
5 input[0].ki.time = NativeMethods.GetTickCount();
6
7 if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]))
8 < input.Length) {
9 throw new Win32Exception(Marshal.GetLastWin32Error());
10 }
11 }
12
13 private void SendKeyUp(short key) {
14 Input[] input = new Input[1];
15 input[0].type = INPUT.KEYBOARD;
16 input[0].ki.wVk = key;
17 input[0].ki.dwFlags = KeyboardConstaint.KEYEVENTF_KEYUP;
18 input[0].ki.time = NativeMethods.GetTickCount();
19
20 if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]))
21 < input.Length) {
22 throw new Win32Exception(Marshal.GetLastWin32Error());
23 }
24 }
自己實現一個 KeyBoardButton 控制元件用作按鈕,用 Visual Studio 或者 SharpDevelop 為螢幕鍵盤設計 UI,然後
在這些 Button 的 Click 事件裡面模擬一個按鍵過程。
1
2 private void ButtonOnClick(object sender, EventArgs e) {
3 KeyboardButton btnKey = sender as KeyboardButton;
4 if (btnKey == null) {
5 return;
6 }
7
8 SendKeyCommand(btnKey);
9 }
10
11 private void SendKeyCommand(KeyboardButton keyButton) {
12 short key = keyButton.VKCode;
13 if (combinationVKButtonsMap.ContainsKey(key)) {
14 if (keyButton.Checked) {
15 SendKeyUp(key);
16 } else {
17 SendKeyDown(key);
18 }
19 } else {
20 SendKeyDown(key);
21 SendKeyUp(key);
22 }
23 }
其中 combinationVKButtonsMap 是一個 IDictionary>, key 儲存的是
VK_SHIFT, VK_CONTROL 等組合鍵的鍵盤碼。左右兩個按鈕對應同一個鍵盤碼,因此需要放在一個 List 裡。
標準鍵盤上的每一個鍵都有虛擬鍵碼( VK_CODE)與之對應。還有一些其他的常量,
把它寫在一個靜態 class 裡吧。
1 // KeyboardConstaint.cs
2 internal static class KeyboardConstaint {
3 internal static readonly short VK_F1 = 0x70;
4 internal static readonly short VK_F2 = 0x71;
5 internal static readonly short VK_F3 = 0x72;
6 internal static readonly short VK_F4 = 0x73;
7 internal static readonly short VK_F5 = 0x74;
8 internal static readonly short VK_F6 = 0x75;
9 internal static readonly short VK_F7 = 0x76;
10 internal static readonly short VK_F8 = 0x77;
11 internal static readonly short VK_F9 = 0x78;
12 internal static readonly short VK_F10 = 0x79;
13 internal static readonly short VK_F11 = 0x7A;
14 internal static readonly short VK_F12 = 0x7B;
15
16 internal static readonly short VK_LEFT = 0x25;
17 internal static readonly short VK_UP = 0x26;
18 internal static readonly short VK_RIGHT = 0x27;
19 internal static readonly short VK_DOWN = 0x28;
20
21 internal static readonly short VK_NONE = 0x00;
22 internal static readonly short VK_ESCAPE = 0x1B;
23 internal static readonly short VK_EXECUTE = 0x2B;
24 internal static readonly short VK_CANCEL = 0x03;
25 internal static readonly short VK_RETURN = 0x0D;
26 internal static readonly short VK_ACCEPT = 0x1E;
27 internal static readonly short VK_BACK = 0x08;
28 internal static readonly short VK_TAB = 0x09;
29 internal static readonly short VK_DELETE = 0x2E;
30 internal static readonly short VK_CAPITAL = 0x14;
31 internal static readonly short VK_NUMLOCK = 0x90;
32 internal static readonly short VK_SPACE = 0x20;
33 internal static readonly short VK_DECIMAL = 0x6E;
34 internal static readonly short VK_SUBTRACT = 0x6D;
35
36 internal static readonly short VK_ADD = 0x6B;
37 internal static readonly short VK_DIVIDE = 0x6F;
38 internal static readonly short VK_MULTIPLY = 0x6A;
39 internal static readonly short VK_INSERT = 0x2D;
40
41 internal static readonly short VK_OEM_1 = 0xBA; // ';:' for US
42 internal static readonly short VK_OEM_PLUS = 0xBB; // '+' any country
43
44 internal static readonly short VK_OEM_MINUS = 0xBD; // '-' any country
45
46 internal static readonly short VK_OEM_2 = 0xBF; // '/?' for US
47 internal static readonly short VK_OEM_3 = 0xC0; // '`~' for US
48 internal static readonly short VK_OEM_4 = 0xDB; // '[{' for US
49 internal static readonly short VK_OEM_5 = 0xDC; // '|' for US
50 internal static readonly short VK_OEM_6 = 0xDD; // ']}' for US
51 internal static readonly short VK_OEM_7 = 0xDE; // ''"' for US
52 internal static readonly short VK_OEM_PERIOD = 0xBE; // '.>' any country
53 internal static readonly short VK_OEM_COMMA = 0xBC; // ', 54 internal static readonly short VK_SHIFT = 0x10;
55 internal static readonly short VK_CONTROL = 0x11;
56 internal static readonly short VK_MENU = 0x12;
57 internal static readonly short VK_LWIN = 0x5B;
58 internal static readonly short VK_RWIN = 0x5C;
59 internal static readonly short VK_APPS = 0x5D;
60
61 internal static readonly short VK_LSHIFT = 0xA0;
62 internal static readonly short VK_RSHIFT = 0xA1;
63 internal static readonly short VK_LCONTROL = 0xA2;
64 internal static readonly short VK_RCONTROL = 0xA3;
65 internal static readonly short VK_LMENU = 0xA4;
66 internal static readonly short VK_RMENU = 0xA5;
67
68 internal static readonly short VK_SNAPSHOT = 0x2C;
69 internal static readonly short VK_SCROLL = 0x91;
70 internal static readonly short VK_PAUSE = 0x13;
71 internal static readonly short VK_HOME = 0x24;
72
73 internal static readonly short VK_NEXT = 0x22;
74 internal static readonly short VK_PRIOR = 0x21;
75 internal static readonly short VK_END = 0x23;
76
77 internal static readonly short VK_NUMPAD0 = 0x60;
78 internal static readonly short VK_NUMPAD1 = 0x61;
79 internal static readonly short VK_NUMPAD2 = 0x62;
80 internal static readonly short VK_NUMPAD3 = 0x63;
81 internal static readonly short VK_NUMPAD4 = 0x64;
82 internal static readonly short VK_NUMPAD5 = 0x65;
83 internal static readonly short VK_NUMPAD5NOTHING = 0x0C;
84 internal static readonly short VK_NUMPAD6 = 0x66;
85 internal static readonly short VK_NUMPAD7 = 0x67;
86 internal static readonly short VK_NUMPAD8 = 0x68;
87 internal static readonly short VK_NUMPAD9 = 0x69;
88
89 internal static readonly short KEYEVENTF_EXTENDEDKEY = 0x0001;
90 internal static readonly short KEYEVENTF_KEYUP = 0x0002;
91
92 internal static readonly int GWL_EXSTYLE = -20;
93 internal static readonly int WS_DISABLED = 0X8000000;
94 internal static readonly int WM_SETFOCUS = 0X0007;
95 }
螢幕鍵盤必須是一個不能獲得輸入焦點的窗體,在這個窗體的建構函式裡,可以安裝
一個全域性滑鼠鉤子,再通過呼叫 SetWindowLong API 函式完成。
1UserActivityHook hook = new UserActivityHook(true, true);
2hook.MouseActivity += HookOnMouseActivity;
3
4private 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 等鍵盤燈的模擬,這裡就不講了,如果有興趣,可以下載完整的程式碼。最後我們的螢幕鍵盤程式執行的效果如
下圖:
ScreenKeyboard.PNG
是系統範圍的鉤子。
什麼是鉤子(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 呼叫封裝起來以便呼叫。
1// NativeMethods.cs
2using System;
3using System.Runtime.InteropServices;
4using System.Drawing;
5
6namespace 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函式。
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 }
使用完鉤子後,要進行解除安裝,這個可以寫在解構函式中。
1
2 public void Stop() {
3 this.Stop(true, true, true);
4 }
5
6 public void Stop(bool uninstallMouseHook, bool uninstallKeyboardHook,
7 bool throwExceptions) {
8 // if mouse hook set and must be uninstalled
9 if (hMouseHook != IntPtr.Zero && uninstallMouseHook) {
10 // uninstall hook
11 bool retMouse = UnhookWindowsHookEx(hMouseHook);
12 // reset invalid handle
13 hMouseHook = IntPtr.Zero;
14 // if failed and exception must be thrown
15 if (retMouse == false && throwExceptions) {
16 // Returns the error code returned by the last unmanaged function
17 // called using platform invoke that has the DllImportAttribute.
18 // SetLastError flag set.
19 int errorCode = Marshal.GetLastWin32Error();
20 // Initializes and throws a new instance of the Win32Exception class
21 // with the specified error.
22 throw new Win32Exception(errorCode);
23 }
24 }
25
26 // if keyboard hook set and must be uninstalled
27 if (hKeyboardHook != IntPtr.Zero && uninstallKeyboardHook) {
28 // uninstall hook
29 bool retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
30 // reset invalid handle
31 hKeyboardHook = IntPtr.Zero;
32 // if failed and exception must be thrown
33 if (retKeyboard == false && throwExceptions) {
34 // Returns the error code returned by the last unmanaged function
35 // called using platform invoke that has the DllImportAttribute.
36 // SetLastError flag set.
37 int errorCode = Marshal.GetLastWin32Error();
38 // Initializes and throws a new instance of the Win32Exception class
39 // with the specified error.
40 throw new Win32Exception(errorCode);
41 }
42 }
43 }
44
將這個檔案編譯成一個dll,即可在應用程式中呼叫。通過它提供的事件,便可監聽所有的鍵盤事件。
但是,這隻能監聽鍵盤事件,沒有鍵盤的情況下,怎麼會有鍵盤事件?其實很簡單,通過SendInput
API函式提供虛擬鍵盤程式碼的呼叫即可模擬鍵盤輸入。下面的程式碼模擬一個 KeyDown 和 KeyUp 過程,
把他們連線起來就是一次按鍵過程。
1 private void SendKeyDown(short key) {
2 Input[] input = new Input[1];
3 input[0].type = INPUT.KEYBOARD;
4 input[0].ki.wVk = key;
5 input[0].ki.time = NativeMethods.GetTickCount();
6
7 if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]))
8 < input.Length) {
9 throw new Win32Exception(Marshal.GetLastWin32Error());
10 }
11 }
12
13 private void SendKeyUp(short key) {
14 Input[] input = new Input[1];
15 input[0].type = INPUT.KEYBOARD;
16 input[0].ki.wVk = key;
17 input[0].ki.dwFlags = KeyboardConstaint.KEYEVENTF_KEYUP;
18 input[0].ki.time = NativeMethods.GetTickCount();
19
20 if (NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]))
21 < input.Length) {
22 throw new Win32Exception(Marshal.GetLastWin32Error());
23 }
24 }
自己實現一個 KeyBoardButton 控制元件用作按鈕,用 Visual Studio 或者 SharpDevelop 為螢幕鍵盤設計 UI,然後
在這些 Button 的 Click 事件裡面模擬一個按鍵過程。
1
2 private void ButtonOnClick(object sender, EventArgs e) {
3 KeyboardButton btnKey = sender as KeyboardButton;
4 if (btnKey == null) {
5 return;
6 }
7
8 SendKeyCommand(btnKey);
9 }
10
11 private void SendKeyCommand(KeyboardButton keyButton) {
12 short key = keyButton.VKCode;
13 if (combinationVKButtonsMap.ContainsKey(key)) {
14 if (keyButton.Checked) {
15 SendKeyUp(key);
16 } else {
17 SendKeyDown(key);
18 }
19 } else {
20 SendKeyDown(key);
21 SendKeyUp(key);
22 }
23 }
其中 combinationVKButtonsMap 是一個 IDictionary
VK_SHIFT, VK_CONTROL 等組合鍵的鍵盤碼。左右兩個按鈕對應同一個鍵盤碼,因此需要放在一個 List 裡。
標準鍵盤上的每一個鍵都有虛擬鍵碼( VK_CODE)與之對應。還有一些其他的常量,
把它寫在一個靜態 class 裡吧。
1 // KeyboardConstaint.cs
2 internal static class KeyboardConstaint {
3 internal static readonly short VK_F1 = 0x70;
4 internal static readonly short VK_F2 = 0x71;
5 internal static readonly short VK_F3 = 0x72;
6 internal static readonly short VK_F4 = 0x73;
7 internal static readonly short VK_F5 = 0x74;
8 internal static readonly short VK_F6 = 0x75;
9 internal static readonly short VK_F7 = 0x76;
10 internal static readonly short VK_F8 = 0x77;
11 internal static readonly short VK_F9 = 0x78;
12 internal static readonly short VK_F10 = 0x79;
13 internal static readonly short VK_F11 = 0x7A;
14 internal static readonly short VK_F12 = 0x7B;
15
16 internal static readonly short VK_LEFT = 0x25;
17 internal static readonly short VK_UP = 0x26;
18 internal static readonly short VK_RIGHT = 0x27;
19 internal static readonly short VK_DOWN = 0x28;
20
21 internal static readonly short VK_NONE = 0x00;
22 internal static readonly short VK_ESCAPE = 0x1B;
23 internal static readonly short VK_EXECUTE = 0x2B;
24 internal static readonly short VK_CANCEL = 0x03;
25 internal static readonly short VK_RETURN = 0x0D;
26 internal static readonly short VK_ACCEPT = 0x1E;
27 internal static readonly short VK_BACK = 0x08;
28 internal static readonly short VK_TAB = 0x09;
29 internal static readonly short VK_DELETE = 0x2E;
30 internal static readonly short VK_CAPITAL = 0x14;
31 internal static readonly short VK_NUMLOCK = 0x90;
32 internal static readonly short VK_SPACE = 0x20;
33 internal static readonly short VK_DECIMAL = 0x6E;
34 internal static readonly short VK_SUBTRACT = 0x6D;
35
36 internal static readonly short VK_ADD = 0x6B;
37 internal static readonly short VK_DIVIDE = 0x6F;
38 internal static readonly short VK_MULTIPLY = 0x6A;
39 internal static readonly short VK_INSERT = 0x2D;
40
41 internal static readonly short VK_OEM_1 = 0xBA; // ';:' for US
42 internal static readonly short VK_OEM_PLUS = 0xBB; // '+' any country
43
44 internal static readonly short VK_OEM_MINUS = 0xBD; // '-' any country
45
46 internal static readonly short VK_OEM_2 = 0xBF; // '/?' for US
47 internal static readonly short VK_OEM_3 = 0xC0; // '`~' for US
48 internal static readonly short VK_OEM_4 = 0xDB; // '[{' for US
49 internal static readonly short VK_OEM_5 = 0xDC; // '|' for US
50 internal static readonly short VK_OEM_6 = 0xDD; // ']}' for US
51 internal static readonly short VK_OEM_7 = 0xDE; // ''"' for US
52 internal static readonly short VK_OEM_PERIOD = 0xBE; // '.>' any country
53 internal static readonly short VK_OEM_COMMA = 0xBC; // ', 54 internal static readonly short VK_SHIFT = 0x10;
55 internal static readonly short VK_CONTROL = 0x11;
56 internal static readonly short VK_MENU = 0x12;
57 internal static readonly short VK_LWIN = 0x5B;
58 internal static readonly short VK_RWIN = 0x5C;
59 internal static readonly short VK_APPS = 0x5D;
60
61 internal static readonly short VK_LSHIFT = 0xA0;
62 internal static readonly short VK_RSHIFT = 0xA1;
63 internal static readonly short VK_LCONTROL = 0xA2;
64 internal static readonly short VK_RCONTROL = 0xA3;
65 internal static readonly short VK_LMENU = 0xA4;
66 internal static readonly short VK_RMENU = 0xA5;
67
68 internal static readonly short VK_SNAPSHOT = 0x2C;
69 internal static readonly short VK_SCROLL = 0x91;
70 internal static readonly short VK_PAUSE = 0x13;
71 internal static readonly short VK_HOME = 0x24;
72
73 internal static readonly short VK_NEXT = 0x22;
74 internal static readonly short VK_PRIOR = 0x21;
75 internal static readonly short VK_END = 0x23;
76
77 internal static readonly short VK_NUMPAD0 = 0x60;
78 internal static readonly short VK_NUMPAD1 = 0x61;
79 internal static readonly short VK_NUMPAD2 = 0x62;
80 internal static readonly short VK_NUMPAD3 = 0x63;
81 internal static readonly short VK_NUMPAD4 = 0x64;
82 internal static readonly short VK_NUMPAD5 = 0x65;
83 internal static readonly short VK_NUMPAD5NOTHING = 0x0C;
84 internal static readonly short VK_NUMPAD6 = 0x66;
85 internal static readonly short VK_NUMPAD7 = 0x67;
86 internal static readonly short VK_NUMPAD8 = 0x68;
87 internal static readonly short VK_NUMPAD9 = 0x69;
88
89 internal static readonly short KEYEVENTF_EXTENDEDKEY = 0x0001;
90 internal static readonly short KEYEVENTF_KEYUP = 0x0002;
91
92 internal static readonly int GWL_EXSTYLE = -20;
93 internal static readonly int WS_DISABLED = 0X8000000;
94 internal static readonly int WM_SETFOCUS = 0X0007;
95 }
螢幕鍵盤必須是一個不能獲得輸入焦點的窗體,在這個窗體的建構函式裡,可以安裝
一個全域性滑鼠鉤子,再通過呼叫 SetWindowLong API 函式完成。
1UserActivityHook hook = new UserActivityHook(true, true);
2hook.MouseActivity += HookOnMouseActivity;
3
4private 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 等鍵盤燈的模擬,這裡就不講了,如果有興趣,可以下載完整的程式碼。最後我們的螢幕鍵盤程式執行的效果如
下圖:
ScreenKeyboard.PNG
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12921506/viewspace-277852/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- win10開啟螢幕鍵盤快捷鍵 螢幕鍵盤怎麼調出來Win10
- win10怎麼關閉螢幕鍵盤_win10如何關閉螢幕鍵盤Win10
- C#實現滑鼠、鍵盤鉤子C#
- 推薦1個jQuery螢幕鍵盤外掛jQuery
- C#實現的三種方式實現模擬鍵盤按鍵C#
- iOS 螢幕錄製實現iOS
- 螢幕取詞技術實現原理與關鍵原始碼原始碼
- win10螢幕鍵盤怎麼開啟 調出虛擬鍵盤的四種方法Win10
- selenium實現螢幕截圖
- QT擷取螢幕的實現QT
- Win10怎麼開啟螢幕鍵盤 win10開啟鍵盤的3種方法Win10
- 如何實現共享螢幕標註功能?
- android 螢幕圓角實現方法Android
- VC實現螢幕變暗效果 (轉)
- mac蘋果螢幕截圖快捷鍵Mac蘋果
- Android -- 工具類(七):[ScreenUtil] (截圖,獲取螢幕寬高,顯示、隱藏虛擬鍵盤,調節螢幕亮度)Android
- C# 螢幕操作錄製與回放C#
- 如何遠端投屏實現螢幕共享
- Android 5.0+ 螢幕錄製實現Android
- 用Delphi實現遠端螢幕抓取 (轉)
- 用VB實現螢幕陰暗操作 (轉)
- win10系統登入介面如何開啟螢幕軟鍵盤Win10
- win10系統螢幕鍵盤尺寸修改不了的解決方法Win10
- android自適應滑動鍵盤產生的螢幕尺寸變化Android
- 按鍵精靈+螢幕錄影專家實現資料抓包錄製
- Ubuntu螢幕截圖快捷鍵知多少Ubuntu
- win10螢幕亮度快捷鍵是什麼_win10怎麼調節螢幕亮度快捷鍵Win10
- Win10系統下螢幕虛擬鍵盤過大的解決方法Win10
- 短視訊系統原始碼,點選螢幕空白處鍵盤不自動收起原始碼
- Android Lollipop (5.0) 螢幕錄製實現Android
- HTML5實現螢幕手勢解鎖HTML
- 12.2 實現鍵盤模擬按鍵
- js實現阻止指定鍵盤按鍵效果JS
- C#軟體開發例項.私人訂製自己的螢幕截圖工具(三)托盤圖示及選單的實現C#
- windows10系統開機總自動開啟螢幕鍵盤如何關閉Windows
- 用JS實現簡單的螢幕錄影機JS
- Windows螢幕解鎖服務原理及實現(1)Windows
- 如何實現監控手機螢幕?(附原始碼)原始碼