HOWTO: use input in vb.net

A forum to store posts deemed exceptionally wise and useful
Post Reply
Guest

HOWTO: use input in vb.net

Post by Guest »

here is a new revision...

this will hook both key presses and mouse clicks. I really want to stress that you unhook both of them on application exit.


Code: Select all

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

Module Keyboard 
    Private Declare Function UnhookWindowsHookEx Lib "user32" _ 
      (ByVal hHook As Integer) As Integer 

    Private 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 the escape key is pressed, run the CleaUP sub routine. 
        'the CleanUP sub removes the garbage and frees resources, then 
        'exits the application. 
        If (Hookstruct.vkCode = VK_ESCAPE) Then 
            CleanUP() 
            Return True 
        End If 

        Return False 
    End Function 

    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 

Module Mouse 

    Public Structure POINTAPI 
        Public x As Long 
        Public y As Long 
    End Structure 

    Public Structure MSLLHOOKSTRUCT 
        Public pt As POINTAPI 
        Public MouseData As Long 
        Public Flags As Long 
        Public Time As Long 
        Public dwExtraInfo As Long 
    End Structure 

    'Constants 
    Public Const WM_RBUTTONDOWN = &H204 
    Public Const WM_RBUTTONUP = &H205 
    Public Const WM_LBUTTONDOWN = &H201 
    Private Const WH_MOUSE_LL = 14          'Hook Flag 
    Private Const HC_ACTION = 0 

    Private mHandle As Integer 

    Private Declare Function UnhookWindowsHookEx Lib "user32" _ 
     (ByVal hHook As Integer) As Integer 

    Private Declare Function SetWindowsHookEx Lib "user32" _ 
      Alias "SetWindowsHookExA" (ByVal idHook As Integer, _ 
      ByVal lpfn As MouseHookDelegate, 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 MSLLHOOKSTRUCT) As Integer 

    Public Function MouseCallback(ByVal Code As Integer, _ 
      ByVal wParam As Integer, _ 
      ByRef lParam As MSLLHOOKSTRUCT) As Integer 


        If (Code = HC_ACTION) Then 
            Debug.WriteLine(wParam.ToString()) 
            Select Case wParam 
                'Case WM_RBUTTONDOWN, WM_RBUTTONUP 
                'Debug.WriteLine(wParam.ToString()) 
                'MsgBox("test") 
            Case WM_LBUTTONDOWN 
                    MsgBox("test") 
                    Return 1 
            End Select 
        End If 


        Return CallNextHookEx(mHandle, _ 
          Code, wParam, lParam) 
    End Function 

    Public Delegate Function MouseHookDelegate( _ 
      ByVal Code As Integer, _ 
      ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT) _ 
                   As Integer 

    <MarshalAs(UnmanagedType.FunctionPtr)> _ 
    Private callback As MouseHookDelegate 

    Public Sub HookMouse() 
        callback = New MouseHookDelegate(AddressOf MouseCallback) 


        mHandle = SetWindowsHookEx( _ 
          WH_MOUSE_LL, callback, _ 
          Marshal.GetHINSTANCE( _ 
           [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0) 
    End Sub 

    Public Sub UnHookMouse() 
        Call UnhookWindowsHookEx(mHandle) 
    End Sub 

End Module
WalterWzK
Posts: 72
Joined: Wed Jan 12, 2005 2:57 pm
Location: Netherlands
Contact:

Post by WalterWzK »

Well it seems like a nice copy of a low-level hook tutorial. But I've been here before and also this one is not complete because your hooking the main-module and not the irrlicht device so there is no input captured when the device window is selected.

I suggest we all (vb.net users) wait for the end of march. By that time version 0.8 will be released with more .NET capability's

Dont get me wrong, nice work.. but sorry mate, I've tested it also a couple of weeks ago when i was trying to catch the input and it wont work properly.
Current Project: Don Salvatore's Mafia
Genre: 3D Shooter \ RTS
Programming Language: VB .NET
Engine: Irrlicht 11 [.NET Wrapper]
Guest

Post by Guest »

yeah, when i first posted the thread. I stated that I didnt write it. Seems to work for me, I am capturing mouse and keyboard input.
xycos

Post by xycos »

How would this be done in C#?
Post Reply