Intercept keyboard input

xiaoxiao2021-03-06  93

This is an example of using Keyboard Hook, which is explained, please check VB5 Call WinAPI skills or hook's profile 'below .basoption Explicit

Declare function setWindowshookex lib "user32" alias "setwindowshookexa" (byval IDHOKEXA "(Byval LPFN AS"

Long, ByVal hmod As Long, ByVal dwThreadId As Long) As LongDeclare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As LongDeclare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long,

LPARAM As Any) As Long

Public HNEXTHOKPROC AS Longpublic const hc_action = 0public const wh_keyboard = 2

Public Sub UnHookKBD () If hnexthookproc <> 0 ThenUnhookWindowsHookEx hnexthookprochnexthookproc = 0End IfEnd SubPublic Function EnableKBDHook () If hnexthookproc <> 0 ThenExit FunctionEnd Ifhnexthookproc = SetWindowsHookEx (WH_KEYBOARD, AddressOf _MyKBHFunc, App.hInstance, 0) If hnexthookproc <> 0 ThenEnableKBDHook = hnexthookprocEnd Ifnd functionpublic function mykbhfunc (byval icode as long, byval wparam as long) These three parameters are fixed, can not move, and mykbhfunc name as long as the name after addressof () in 'setWindowsHookex () It can be, not necessarily what 'wparam is incoming, which Key's Virtual-Key Code is

'If you put the following two lines unmark, all keyboards have no effect' mykbhfunc = 1 'Eat information' exit function

MyKBHFunc = 0 'information to be processed If iCode <0 ThenMyKBHFunc = CallNextHookEx (hnexthookproc, iCode, wParam, lParam) Exit FunctionEnd IfIf wParam = vbKeySnapshot Then' detected to have no press PrintScreen key MyKBHFunc = 1 'in this Hook this will eat Information Debug.Print "Haha" elsecall CallNexthookex (HNEXTHOOKPROC, ICODE, WPARAM, LPARAM) end ingnd function

'The following in FormPrivate Sub Form_Load () Call Enablekbdhookend Sub

转载请注明原文地址:https://www.9cbs.com/read-102936.html

New Post(0)