Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2006
    Messaggi
    103

    Problema Hook Tastiera

    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:
    codice:
    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
    Questo è l'evento associato alla pressione del tasto
    codice:
       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
    Questa è la dichiarazione dell'hook
    codice:
    Private WithEvents kbHook As New KeyboardHook
    Sottolieneo che il codice dell'hook non l'ho scritto io ma l'ho trovato in rete.

  2. #2
    Utente di HTML.it L'avatar di alpynet
    Registrato dal
    Mar 2010
    Messaggi
    123
    Ciao, che linguaggio? Ma non fai prima a catturare l'evento, del tasto premuto, direttamente dal form attivo?

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2006
    Messaggi
    103
    Ciao si scusami mi sono scordato di specificare il linguaggio, comunque è vb.net.

    Catturare l'evento del form è una cosa complicata (almeno credo) perchè per esempio se metto l'evento nella classe form se poi ho il focus in un pulsante o textbox per esempio non funziona. Ma dovrei aggiungere l'evento in tutti i controlli del form. Mi sbaglio io?

  4. #4
    Utente di HTML.it L'avatar di alpynet
    Registrato dal
    Mar 2010
    Messaggi
    123
    se imposti la proprietà, del form, KeyPreview=True, ogni evento puo' essere processato anche da form.
    Ad esempio, se metti il seguente codice nell'evento KeyDown del form, il tasto f3 viene processato sempre indipendentemente da dove è il focus.
    codice:
    If e.KeyCode = Keys.F3 Then
    .....tuo codice
    End If

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2006
    Messaggi
    103
    Wow grazie mille, hai risolto pienamente il mio problema. Ho cercato molto su google e le uniche soluzioni che ero riuscito a trovare erano tutte a base di Hook.

    Ti sono grato

  6. #6
    Utente di HTML.it L'avatar di alpynet
    Registrato dal
    Mar 2010
    Messaggi
    123
    Mi fa piacere. E' questo lo spirito del forum..

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.