

In order to set a low level mouse and keyboard hook in VB.NET, we P/Invoke Windows API SetWindowsHookEx, CallNextHookEx, UnhookWindowsHookEx, and GetModuleHandle. The Windows API UnhookWindowsHookEx is used to remove a hook procedure installed in a hook chain by the SetWindowsHookEx function. We use the Windows API CallNextHookEx from the user32.dll to pass the hook information to the next hook procedure in the current hook chain. This function installs an application-defined hook procedure in the system hook chain associated with the certain hook type. For a detailed introduction on hooks, please refer to (VS.85).aspx To set a Windows hook, we call the Windows API SetWindowsHookEx from the user32.dll. This is where certain types of messages will be processed before they reach the target window procedure. A hook is a point in the system message-handling mechanism where an application can install a subroutine to monitor the message traffic in the system. Hi Lethal323, Welcome to VB.NET forums! Based on your post, I think that you can have a look at hook in this scenario to implement your requirement.

Public Class Form1 Private WithEvents Tmr As New Timer Private Declare Function GetKeyboardState Lib "user32" (ByVal keyState() As Byte) As Boolean Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Tmr.Start() End Sub Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick Dim keystat(0 To 255) As Byte GetKeyboardState(keystat) For count As Integer = 0 To 255 If (keystat(count) And &H80) = &H80 Then Debug.Print(Chr(count) & " is depressed") End If Next End Sub End Class There are other ways to test if a key is being pressed as Crazypennie has shown - the best way depends upon your application. If you want to investigate the function further try it with a timer like this: The Enter key can't toggle - that only applies to things like the Shift lock etc. If (keystat(keynumber) And &H80) = &H80 ThenĤ. To test if the topmost bit is set you should be using: Your loop will lock up your application.ģ. You don't appear to be calling GetKeyboardState anywhere.Ģ.


There are quite a few things wrong with your code.ġ.
