gmamaladze / globalmousekeyhook

This library allows you to tap keyboard and mouse, detect and record their activity even when an application is inactive and runs in background.
MIT License
1.05k stars 257 forks source link

e.Handled = true does not suppress key actions #130

Closed tstaudte closed 3 years ago

tstaudte commented 3 years ago

I'm following the example in vb.net to suppress ALL keys globally, it logs the keys as they are pressed, but they are still sent to the app (e.g. notepad), although I set e.handled=true. Here's the main code:

` Imports System.ComponentModel Imports Gma.System.MouseKeyHook

Public Class frmLockScreen

Private m_GlobalHook As IKeyboardMouseEvents
Public Sub Subscribe()
    '  Note: for the application hook, use the Hook.AppEvents() instead
    m_GlobalHook = Hook.GlobalEvents()
    AddHandler m_GlobalHook.KeyPress, AddressOf GlobalHookKeyPress
End Sub

Public Sub Unsubscribe()
    If Not IsNothing(m_GlobalHook) Then
        RemoveHandler m_GlobalHook.KeyPress, AddressOf GlobalHookKeyPress
        m_GlobalHook.Dispose()
    End If
End Sub

Public Sub GlobalHookKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    Debug.Print("KeyPress: " & vbTab & "{0}", e.KeyChar)
    e.Handled = True
End Sub

Private Sub frmLockScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Subscribe()
End Sub

Private Sub frmLockScreen_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
    Unsubscribe()
End Sub

End Class `

Am I doing something wrong, or is this a bug?

essoperagma commented 3 years ago

Applications may still be responding to "KeyDown" and "KeyUp" events. If you want the keystrokes to be completely consumed, you need to subscribe to those events as well and run e.Handled = true.

tstaudte commented 3 years ago

Thank you, Essoperagma, that did the trick. Excellent!