[RESOLVED] Keyboard hook giving error
Trying to convert my home app from vb6 to .net but have a problem. I am using the code in the tut by Paul Kimmel but it is returning Err.LastDllError = 126.
I am using this code as posted on Win7 and VS2010.
KeyboardHandle is retuning 0 but Im not sure which part of it is the problem. There is a lot of new stuff there for me.
Code:
Public Sub HookKeyboard()
callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
Call CheckHooked()
End Sub
Thanks for any help.
Re: Keyboard hook giving error
have you set your target CPU = x86?
Re: Keyboard hook giving error
also api declarations are different in vb6 than they are in vb.net
it would help if you posted your code
Re: Keyboard hook giving error
hey .paul.
Thanks for ya time.
Stting the cpu?....I have no idea what you mean about that. NEVER heard of a need to set my cpu. Sorry for my ignorance but thats why Im here.
As I said in my origional...Its a direct copy of Paul Kimmels ...but here it is.
Code:
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading
Module Keyboard
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 GetAsyncKeyState Lib "user32" (ByVal vKey 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 Structure KBDLLHOOKSTRUCT
Public vkCode As Integer
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
' Low-Level Keyboard Constants
Private Const HC_ACTION As Integer = 0
Private Const LLKHF_EXTENDED As Integer = &H1
Private Const LLKHF_INJECTED As Integer = &H10
Private Const LLKHF_ALTDOWN As Integer = &H20
Private Const LLKHF_UP As Integer = &H80
' Virtual Keys
Public Const VK_TAB = &H9
Public Const VK_CONTROL = &H11
Public Const VK_ESCAPE = &H1B
Public Const VK_DELETE = &H2E
Private Const WH_KEYBOARD_LL As Integer = 13&
Public KeyboardHandle As Integer
' Implement this function to block as many
' key combinations as you'd like
Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)
Debug.WriteLine(Hookstruct.vkCode = VK_ESCAPE)
Debug.WriteLine(Hookstruct.vkCode = VK_TAB)
If (Hookstruct.vkCode = VK_ESCAPE) And CBool(GetAsyncKeyState(VK_CONTROL) And &H8000) Then
Call HookedState("Ctrl + Esc blocked")
Return True
End If
If (Hookstruct.vkCode = VK_TAB) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) Then
Call HookedState("Alt + Tab blockd")
Return True
End If
If (Hookstruct.vkCode = VK_ESCAPE) And CBool(Hookstruct.flags And LLKHF_ALTDOWN) Then
Call HookedState("Alt + Escape blocked")
Return True
End If
Return False
End Function
Private Sub HookedState(ByVal Text As String)
Debug.WriteLine(Text)
End Sub
Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
If (Code = HC_ACTION) Then
Debug.WriteLine("Calling IsHooked")
If (IsHooked(lParam)) Then
Return 1
End If
End If
Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam)
End Function
Public Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
<MarshalAs(UnmanagedType.FunctionPtr)> Private callback As KeyboardHookDelegate
Public Sub HookKeyboard()
callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
Call CheckHooked()
End Sub
Public Sub CheckHooked()
If (Hooked()) Then
Debug.WriteLine("Keyboard hooked")
Else
Debug.WriteLine("Keyboard hook failed: " & Err.LastDllError)
End If
End Sub
Private Function Hooked()
Hooked = KeyboardHandle <> 0
End Function
Public Sub UnhookKeyboard()
If (Hooked()) Then
Call UnhookWindowsHookEx(KeyboardHandle)
End If
End Sub
End Module
Is setting the target cpu a 2010 specific issue? What are the other options for this setting?
Your help is appreciated.
Re: Keyboard hook giving error
ok. i tried it with x64 + with x86. it doesn't work.
try my globalInputHook class in my signature...
Re: Keyboard hook giving error
.paul.
Again thanks for your reply.
I know it doesnt work. Thats why I came here for help.
I want to know WHY it doesnt work. I have a lot of respect for what I have seen of Paul Kimmel and I believe his code should work.
Why doesnt it? What is causing the error. There are many alternatives I can find but I chose to come here for help with the option I am using.
Your signiture?....I dont see one.
Please feel free to post it here and I will try it.
Thanks .paul.
Re: Keyboard hook giving error
if i knew why it doesn't work, i would've told you:D
it's probably an error in transcription somewhere.
http://www.vbforums.com/showthread.php?t=637302
Re: Keyboard hook giving error
mmm....Or promote your own.. :bigyello:
Anyway....I had a look at your code and it looks very similar to what I am trying to get to work now. Although yours is much more comprehensive.
I have added it to my project but cant reference it.
What have I missed.
It is late here and I am tired so the simple things are boing missed :(
Your help is appreciated.
Re: Keyboard hook giving error
you just need to add the class to your project. after rebuilding, you'll find it (it's a component), at the top of your toolbox
also if you read the complete thread i gave the link for, you'll see what others have said about their experience with it...
Re: Keyboard hook giving error
Im not really concerned about others experience...Its only mine that counts :)
WOW...ya lost me....
I have no prob adding the file to my project but I cant reference it......If I continue to build it I will replace Londons gerkin.
It does not show up anywhere........and Im not able to reference it.
Grrrr.....
Not your fault Im sure....something sooo simple becomes such a big prob. :(
Re: Keyboard hook giving error
Strangely enough it was in my tools after reloading so its all good.
Its working now but I will need to spend a while to now try to understand some of the code and and what its doing. Vastly different to 6.
Thanks so much .paul. your persevering help has paid of for me.
Greatly appreciated.
Thanks.