473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Oddity with WH_KEYBOARD_LL in VB.NET 2005

I have been working a program that uses a keyboard hook to trigger several
different actions based on the key that is pressed. The code that I use
(simplified and provided below) works perfectly in VB.Net 2003, but in VB.Net
2005 Pro, my keyboardhandle always equals 0, so the hook never gets set.
Would anyone know why on the same computer, the same working code in 2003
would not work in 2005? Thanks in advance for any advice you might have.

Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading

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

Public Function IsHooked( _
ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean

Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)

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 & " HookedState")
End Sub

Public Function KeyboardCallback(ByVal Code As Integer, _
ByVal wParam As Integer, _
ByRef lParam As KBDLLHOOKSTRUCT) As Integer

Debug.WriteLine("Calling IsHooked")

If (IsHooked(lParam)) Then
Return 1
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 CheckHooked")
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
Private Sub initLoad(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Call HookKeyboard()
End Sub

Private Sub closeOnExit(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Call UnhookKeyboard()
End Sub
Nov 23 '05 #1
1 4115
Hello "Draiko",

i had the same problem...but after some Experiments i've found a Solution:
If you disable the "Enable the Visual Studio hosting process"-Option in your
Debug-Project-Settings you get some "real" Keyboardhandles from
SetWindowsHookEx. :)

Regards Lars
Dec 2 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
2
by: Ray Cassick \(Home\) | last post by:
I have been beating myself up over trying to locate what I thought was a memory leak in MY code but based upon what I have seen I can't be sure that it IS my fault or something strange with DOTNET....
5
by: amber | last post by:
I'm having some trouble with a listbox on a Windows form. Initially it populates fine. I have another listbox (listbox1) and when it's index changes, listbox2 is repopulated. I have a textbox...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
1
by: twd | last post by:
I'm seeing some new and unexpected behaviour with tkinter + python2.4, in a gnome+linux environment. The code below used to work (and continues to work under windows). The intended behaviour is...
5
by: jmdocherty | last post by:
All, I've been trying to set up a CSS layout and all seems well in Firefox but in Safari it just seems to be plain weird! I hope someone can help tell me whether this is a problem with my code...
43
by: michael.f.ellis | last post by:
The following script puzzles me. It creates two nested lists that compare identically. After identical element assignments, the lists are different. In one case, a single element is replaced. In...
4
by: sonet192 | last post by:
When I go to publish my web site with Visual Studio 2005, it would delete the destination files then claim the entire process was successful. When I check to see if all the files and folders were...
4
by: Feanor | last post by:
I am running this in a small loop: char charStock = {recBuff, recBuff, recBuff}; for(i=0; i<3; i++) printf("%c", charStock); record.numInStock = atoi(charStock); printf(" %d\n",...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.