1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| Option Explicit
Private Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As LongPtr, ByVal hMod As LongPtr, ByVal dwThreadId As Long) As LongPtr
Private Declare PtrSafe Function CallNextHookEx Lib "user32" (ByVal hHook As LongPtr, ByVal nCode As Long, ByVal wParam As LongPtr, lParam As LongPtr) As LongPtr
Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As LongPtr) As Long
Private Declare PtrSafe Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Const WH_KEYBOARD = 2
Private Const VK_LCONTROL = 162 ' Ctrl gauche
Private fHook As LongPtr
Private Function KeyboardProc(ByVal nCode As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
KeyboardProc = 0
If (nCode = 0) And ((lParam And &HC0000000) = 0) Then
On Error Resume Next
If wParam = 13 Then ' touche entrée
If GetKeyState(VK_LCONTROL) < 0 Then ' controle gauche appuyé
KeyboardProc = 1 ' intercepter la touche
MsgBox "Ctrl + entrée intercepté"
End If
End If
End If
If (nCode < 0) Or (KeyboardProc = 0) Then: KeyboardProc = CallNextHookEx(fHook, nCode, wParam, lParam)
End Function
Public Sub EndHook()
If fHook <> 0 Then
UnhookWindowsHookEx fHook
fHook = 0
End If
End Sub
Public Sub StartHook()
EndHook
fHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, 0, GetCurrentThreadId)
End Sub |
Partager