Bonjour,

J'apprécierais de l'aide sur le problème suivant.

Mon but final est que l'appui sur la touche F12 entraîne l'impression de la date (par SendKeys) dans l'application qui a le focus (que ce soit Outlook, Word, Excel, etc...). J'ai donc mis en place un "global keyboard hook" à l'échelle du système, qui fonctionne bien (code ci-dessous).

Mon seul souci est que je souhaiterais neutraliser toute interception ultérieure par le système du même F12. Par exemple, l'appui sur F12 sous Word imprime bien la date, mais lance en même temps la boîte de dialogue ("Enregistrer sous..."), car F12 est la raccourci clavier naturel pour cette boîte de dialogue sous Word (hook local). La date s'imprime en fait au sein d'un champ de cette boîte de dialogue.

En d'autres termes, je souhaiterais améliorer le hook pour que l'appui sur F12 fasse ce que je souhaite (c'est déjà le cas), mais rien d'autre. C'est-à-dire faire en sorte que, hormis mon traitement, le système "oublie" que F12 a été pressé.

Je débute en VB.net : je compte sur votre indulgence.

Merci d'avance pour votre aide,
Cordialement,

Nicolas

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
' Option Strict On
Option Explicit On
 
' inspired by:
' http://jo0ls-dotnet-stuff.blogspot.com/2008/12/vbnet-global-keyboard-hook-to-detect.html
 
Imports System.Runtime.InteropServices
 
Public Class Form1
 
    Private Const WH_KEYBOARD_LL As Integer = 13
    Private Const WM_KEYUP As Integer = &H101
    Private Const WM_SYSKEYUP As Integer = &H105
    Private proc As LowLevelKeyboardProcDelegate = AddressOf HookCallback
    Private hookID As IntPtr
 
    Private Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As IntPtr
 
    <DllImport("user32")> _
    Private Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, _
        ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
    End Function
 
    <DllImport("user32.dll")> _
    Private Shared Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
 
    <DllImport("user32.dll")> _
    Private Shared Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As IntPtr
    End Function
 
    <DllImport("kernel32.dll", CharSet:=CharSet.Unicode)> _
    Private Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
    End Function
 
    Sub New()
        InitializeComponent()
        Text = "KeyboardPlus 1.01"
        hookID = SetHook(proc)
    End Sub
 
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
        UnhookWindowsHookEx(hookID)
    End Sub
 
    Private Function SetHook(ByVal proc As LowLevelKeyboardProcDelegate) As IntPtr
        Using curProcess As Process = Process.GetCurrentProcess()
            Using curModule As ProcessModule = curProcess.MainModule
                Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0)
            End Using
        End Using
    End Function
 
    Private Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        ' "The WM_KEYUP message is posted to the window with the keyboard focus
        ' when a nonsystem key is released. A nonsystem key is a key that is pressed when the ALT key is not pressed,
        ' or a keyboard key that is pressed when a window has the keyboard focus." 
        If nCode >= 0 AndAlso (wParam.ToInt32 = WM_KEYUP OrElse wParam.ToInt32 = WM_SYSKEYUP) Then
            Dim vkCode As Integer = Marshal.ReadInt32(lParam)
            If vkCode = Keys.F12 Then
                Dim dateOfTheDay As Char() = DateAndTime.Now.ToString.ToCharArray
                If Console.CapsLock Then
                    SendKeys.Send("{CAPSLOCK}")
                    For index As Integer = 0 To 9
                        SendKeys.Send(dateOfTheDay(index))
                    Next
                    SendKeys.Send("{CAPSLOCK}")
                Else
                    For index As Integer = 0 To 9
                        SendKeys.Send(dateOfTheDay(index))
                    Next
                End If
            End If
        End If
        Return CallNextHookEx(hookID, nCode, wParam, lParam)
    End Function
 
End Class