Salve a tutti, sto utilizzando questo hook di sistema per intercettare alcuni tasti in un gestionele del cliente.
Premetto che non si tratta di un keylogger e niente di inlegale
Uso l'hook per intercettare alcuni tasti standard che il cliente mi ha richiesto di configurare.
Per esempio in qualsiasi schermata del gestionale premendo il tasto F3 si apre la schermata "Nuovo" appropriata alla sezione.
L'hook funziona benissimo, il problema è che dopo un po' smette di funzionare senza restituire alcun errore.
Da cosa puo' dipendere?
C'e qualche modo di controllare periodicamente se l'hook è attivo e in caso contrario ripristinarlo?
Altra domanda, c'è eventualmente un'altro modo di implementare questa cosa?
Grazie a tutti in anticipo.
Questa è la classe:
Questo è l'evento associato alla pressione del tastocodice:Public Class KeyboardHook ''Constants Private Const HC_ACTION As Integer = 0 Private Const WH_KEYBOARD_LL As Integer = 13 Private Const WM_KEYDOWN = &H100 Private Const WM_KEYUP = &H101 Private Const WM_SYSKEYDOWN = &H104 Private Const WM_SYSKEYUP = &H105 ''Keypress Structure Private Structure KBDLLHOOKSTRUCT Public vkCode As Integer Public scancode As Integer Public flags As Integer Public time As Integer Public dwExtraInfo As Integer End Structure ''API Functions Private Declare Function SetWindowsHookEx Lib "user32" _ Alias "SetWindowsHookExA" _ (ByVal idHook As Integer, _ ByVal lpfn As KeyboardProcDelegate, _ ByVal hmod As Integer, _ ByVal dwThreadId 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 Private Declare Function UnhookWindowsHookEx Lib "user32" _ (ByVal hHook As Integer) As Integer ''Our Keyboard Delegate Private Delegate Function KeyboardProcDelegate _ (ByVal nCode As Integer, _ ByVal wParam As Integer, _ ByRef lParam As KBDLLHOOKSTRUCT) As Integer ''The KeyPress events Public Shared Event KeyDown(ByVal Key As Keys) Public Shared Event KeyUp(ByVal Key As Keys) ''The identifyer for our KeyHook Private Shared KeyHook As Integer ''KeyHookDelegate Private Shared KeyHookDelegate As KeyboardProcDelegate Public Sub New() ''Installs a Low Level Keyboard Hook KeyHookDelegate = New KeyboardProcDelegate(AddressOf KeyboardProc) KeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyHookDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0) End Sub Private Shared Function KeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer ''If it is a keypress If (nCode = HC_ACTION) Then Select Case wParam ''If it is a Keydown Event Case WM_KEYDOWN, WM_SYSKEYDOWN ''Activates the KeyDown event in Form 1 RaiseEvent KeyDown(CType(lParam.vkCode, Keys)) Case WM_KEYUP, WM_SYSKEYUP ''Activates the KeyUp event in Form 1 RaiseEvent KeyUp(CType(lParam.vkCode, Keys)) End Select End If ''Next Return CallNextHookEx(KeyHook, nCode, wParam, lParam) End Function Protected Overrides Sub Finalize() ''On close it UnHooks the Hook UnhookWindowsHookEx(KeyHook) MyBase.Finalize() End Sub End Class
Questa è la dichiarazione dell'hookcodice:Private Sub kbHook_KeyDown(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyDown Select Case Key Case Keys.F3 NuovoBtm_Click(New Object, New System.EventArgs) End Select 'Me.kbHook = Nothing 'Me.kbHook = New KeyboardHook End Sub
Sottolieneo che il codice dell'hook non l'ho scritto io ma l'ho trovato in rete.codice:Private WithEvents kbHook As New KeyboardHook

Rispondi quotando
