Visual-Vincent / InputHelper

A .NET friendly library for simulating mouse and keyboard input.
BSD 3-Clause "New" or "Revised" License
14 stars 6 forks source link

how to terminate the hook's action to continue? #3

Closed yongzhi444 closed 4 years ago

yongzhi444 commented 4 years ago

example, i hook the keydown, if "ctrl + c" is pressed, i want to stop "copy" action. how i do? thanks a lot!

Visual-Vincent commented 4 years ago

Hi there, sorry for my late reply. To block a single or a combination of keystrokes simply set e.Block = True in the hook event handler when the input satisfies your needs.

Private Sub KeyboardHook_KeyDown(sender As System.Object, e As InputHelper.EventArgs.KeyboardHookEventArgs) Handles KeyboardHook.KeyDown
    If e.Modifiers = InputHelper.ModifierKeys.Control AndAlso e.KeyCode = Keys.C Then
        e.Block = True 'Prevents CTRL + C from reaching any other applications or from executing standard Windows actions.
    End If
End Sub

Note that this will block all use of CTRL + C which, in some cases, are used for other shortcuts than just "copy". For instance in CMD it is used to terminate the currently running command/script.

Please take a look at Event Args on the Wiki, if you haven't already, to get an overview of what information is made available through the event.

Hope this helped!

yongzhi444 commented 4 years ago

thanks a lot!