Newsgroup: it.comp.lang.visual-basic
Titolo: Re: Hook tastiera vb2005

Grazie
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
Ciao
**********************
web: <a href="
http://www.programmatica.net
">
www.programmatica.net
</a>
blog: <a href="
http://blogs.ugidotnet.org/programmatica
">My blog</a> 

Stefano Benetti ha scritto:

Grazie era proprio quello che cercavo!

 >GrazieImports System.Runtime.InteropServices

si puo trovare qualcosa di simile per vb6?!?
mi serve qualcosa esente dal framework del .net

mi serve qualcosa esente dal framework del .net
Le dll di sistema a cui agganciarsi sono le stesse, quindi basta
trasformarlo in banale codice vb vecchio stile.
Ciao
**********************
web: 
www.programmatica.net
blog: 
blogs.ugidotnet.org/programmatica

Scusami, ho provato il codice e tutto ok, ma ai successivi test succede
questo:
Public Sub CheckHooked()
        If (Hooked()) Then
            Debug.WriteLine("Keyboard hooked")
        Else
            Debug.WriteLine("Keyboard hook failed: " &
Err.LastDllError) <<--Questa riga viene eseguita
        End If
    End Sub
viene eseguita quindi questa riga
Debug.WriteLine("Keyboard hook failed: " & Err.LastDllError)
e non credo sia un buon segno... come risolvo? Grazie

 e non credo sia un buon segno... come risolvo?
Se non ci dici di preciso che errore ti da...
Bye,
-- 
Carmine ( 
pikachu...@gmail.com
 (Togli la scritta NOSPAM) )
Webmaster di The Total Site: 
http://www.thetotalsite.it
My Blog: 
http://blog.thetotalsite.it
MariokartDS Codice Amico: 532639027909 - CarmineC in MarioKartDS
Se un circuito funziona al primo tentativo è perchè il problema è molto
più nascosto e subdolo di quanto tu possa pensare...
E apparirà nel momento in cui farà il massimo danno 


Il problema è che non succede proprio niente... Mi dice solo che
l'hook è fallito!
uso il codice seguente.. ho sbagliato qualcosa?
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading
Public Class Form1









        MsgBox("Hookstruct.vkCode: " & Hookstruct.vkCode)
        MsgBox(Hookstruct.vkCode = VK_ESCAPE)
        MsgBox(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)
        MsgBox(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
            MsgBox("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
            MsgBox("Keyboard hooked")
        Else
            MsgBox("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 Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        HookKeyboard()
    End Sub
End Class

 l'hook è fallito!
Sicuro?
Prova a mettere un blocco TryCatch e vedi che eccezione ti dà.
Cmq sul mio sito (
www.thetotalsite.it
) nella sezione "esempi programmazione" 
trovi un esempio completo di un logger di mouse e tastiera.


 Bye,
OK, grazie 1000, ho provato l'esempio ed è perfetto! grazie!