Imports System.Runtime.InteropServices
Public Class Form1
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardProc, ByVal hmod As IntPtr, ByVal dwThreadId As Integer) As IntPtr
Private Declare Function CallNextHookEx Lib "user32" (ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hhk As IntPtr) As Boolean
Private Delegate Function KeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Private Const WM_KEYDOWN As Integer = &H100
Private Const WH_KEYBOARD_LL As Integer = 13
Private hHook As IntPtr
Private Function KeyboardProcHandler(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
If nCode >= 0 AndAlso wParam = WM_KEYDOWN Then
Dim vkCode As Integer = Marshal.ReadInt32(lParam)
MessageBox.Show("Key Code: " & vkCode)
End If
Return CallNextHookEx(hHook, nCode, wParam, lParam)
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeyboardProcHandler, IntPtr.Zero, 0)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
UnhookWindowsHookEx(hHook)
End Sub
End Class
沒有留言:
張貼留言