2010년도에 만든 키보드 후킹 VB.NET 버전
이벤트 생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Imports KeyboardHook_VB.KeyboardHook_VB Public Class frmMain Dim cKeyHook As CKeyHook Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load cKeyHook = New CKeyHook(Me) End Sub Private Sub btnHookStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHookStart.Click CKeyHook.InstallHook() End Sub Private Sub btnHookStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHookStop.Click cKeyHook.UnistallHook() End Sub End Class | cs |
테스트로 Enter, F1등 몇가지만 실험 사용하고 싶은 코드를 열거형으로 만들어 사용하면 된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | Imports System.IO Imports System.Runtime.InteropServices Imports System.Reflection Imports System.Text Imports System.Windows.Forms Namespace KeyboardHook_VB Public Class CKeyHook #Region "Keys" Private Const WH_KEYBOARD_LL As Integer = 13 Private Const GWL_HINSTANCE As Integer = -6 Private Const HCBT_ACTIVATE As Integer = 5 Private Const HCBT_CREATEWND As Integer = 3 Private Const Enter As Integer = 13 Private Const F1 As Integer = 112 Private Const A As Integer = 65 #End Region #Region "DLL" Private Shared hookHandle As IntPtr Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, ByVal hMod As Integer, ByVal dwThreadId As Integer) As Integer Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As KBDLLHOOKSTRUCT) As Integer Public Delegate Function KeyboardHookDelegate(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer Public Declare Function GetModuleHandle Lib "kernel32.dll" (ByVal lpModuleName As String) As IntPtr <MarshalAs(UnmanagedType.FunctionPtr)> Private hookFunction As KeyboardHookDelegate Public Structure KBDLLHOOKSTRUCT Public vkCode As Integer Public scanCode As Integer Public flags As Integer Public time As Integer Public dwExtraInfo As Integer End Structure Dim TargetForm As frmMain #End Region #Region "Method" Sub New(ByRef lTargetForm) TargetForm = lTargetForm End Sub Public Sub InstallHook() hookFunction = New KeyboardHookDelegate(AddressOf KeyboardCallback) Console.WriteLine("hooked") hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, hookFunction, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, IntPtr.Zero) End Sub Public Sub UnistallHook() Dim hookCheck As Boolean hookCheck = UnhookWindowsHookEx(hookHandle) If (True = hookCheck) Then Console.WriteLine("unhooked") Else Console.WriteLine("unhooked fail") End If End Sub Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer Try ' 특수 키보드 에 해당 flags 값이 1이면 KEY_PRESS 이벤트 129이면 KEY_UP 이벤트 ' 일반 키보드 에 해당 flags 값이 0이면 KEY_PRESS 이벤트 128이면 KEY_UP 이벤트 If (lParam.flags = 0) Then Select Case lParam.vkCode Case Enter TargetForm.lblKey.Text = "Enter" Case A TargetForm.lblKey.Text = "A" Case F1 TargetForm.lblKey.Text = "F1" Case Else TargetForm.lblKey.Text = lParam.vkCode End Select End If Catch ex As Exception End Try Dim inPtr As Integer Try inPtr = CallNextHookEx(hookHandle, Code, wParam, lParam) Catch ex As Exception Console.WriteLine("NextHook: '" & ex.Message) inPtr = 0 End Try Return inPtr End Function #End Region End Class End Namespace | cs |