我正在尝试禁用Windows密钥。在运行应用程序时,我收到错误"检测到CallbackOnCollectedDelegate"以下是我的代码
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, ByVal hMod As IntPtr, ByVal dwThreadId As Integer) As IntPtr
Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" (ByVal hHook As IntPtr) As Boolean
Declare Function CallNextHookEx Lib "user32" Alias "CallNextHookEx" (ByVal hHook As IntPtr, ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
Const WH_KEYBOARD_LL As Integer = 13
Structure KBDLLHOOKSTRUCT
Dim vkCode As Integer
Dim scanCode As Integer
Dim flags As Integer
Dim time As Integer
Dim dwExtraInfo As Integer
End Structure
Dim intLLKey As IntPtr
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, IntPtr.Zero, 0)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
UnhookWindowsHookEx(intLLKey)
End Sub
Private Function LowLevelKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
Dim blnEat As Boolean = False
Select Case wParam
Case 256, 257, 260, 261
'Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
blnEat = ((lParam.vkCode = 9) AndAlso (lParam.flags = 32)) Or
((lParam.vkCode = 27) AndAlso (lParam.flags = 32)) Or
((lParam.vkCode = 27) AndAlso (lParam.flags = 0)) Or
((lParam.vkCode = 91) AndAlso (lParam.flags = 1)) Or
((lParam.vkCode = 92) AndAlso (lParam.flags = 1))
End Select
If blnEat = True Then
Return 1
Else
Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)
End If
End Function
非常感谢任何帮助
答案 0 :(得分:1)
问题与this post相同。不同之处在于他们正在使用C#。但是,VB正在做同样的事情;为你生成一个代表。作为参考,这是反编译代码在表单的Load事件中的样子。
Private Sub Form1_Load(sender As Object, e As EventArgs)
Me.intLLKey = Form1.SetWindowsHookEx(13, New Form1.LowLevelKeyboardProcDelegate(Me.LowLevelKeyboardProc), IntPtr.Zero, 0)
End Sub
请注意,它正在为您创建一个LowLevelKeyboardProcDelegate委托。我不会在这里彻底改变@HansPassant's answer,因为他在描述问题和解决方案方面表现出色;只说你需要存储自己对LowLevelKeyboardProcDelegate委托的引用。