Bonjour à tous,

Voici un problème sur lequel je sèche depuis quelques jours.

J'ai d'abord fait plusieurs recherches afin de savoir comment simuler des clics de souris et des pressions de touches de clavier.

J'ai finalement trouvé tout ce dont j'avais besoin et tout semble bien fonctionner. Dans mon application, la simulation de la touche Escape fonctionne bien, elle réagit correctement, mais un bug survient alors une fois que je reviens sous Windows : je ne peux plus déplacer les icônes de mon bureau ou de l'Explorer. Je suis obligé d'appuyer sur la touche Escape du clavier pour que je puisse à nouveau déplacer mes icônes.

Autrement dit, il semble que la simulation de pression de la touche Escape rende Windows "instable" d'une certaine manière, comme si l'événement ne se terminait pas bien.

Voici le code que j'utilise :

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
 
    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function SendInput(ByVal nInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer
    End Function
 
    ' ne fonctionne pas ?
    '<DllImport("coredll.dll", SetLastError:=True)> _
    'Private Shared Sub keybd_event(ByVal bVk As Integer, ByVal bScan As Integer, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    'End Sub
 
    ''' <summary>
    ''' Constantes utiles
    ''' </summary>
    ''' <remarks></remarks>
    Private Const INPUT_MOUSE As Integer = 0                ' input type souris
    Private Const INPUT_KEYBOARD As Integer = 1             ' input type clavier
    Private Const INPUT_HARDWARE As Integer = 2             ' input type hardware
    Private Const MOUSEEVENTF_LEFTDOWN As UInt32 = &H2      ' événement clic gauche bas
    Private Const MOUSEEVENTF_LEFTUP As UInt32 = &H4        ' événement clic gauche haut
    Private Const KEYEVENTF_KEYUP As UInt32 = &H2
    Private Const KEYEVENTF_KEYDOWN As UInt32 = &H0
    Private Const VK_ESCAPE As UInt32 = &H1B                ' événement touche escape
    Private Const VK_BACK As UInt32 = &H8
 
    ''' <summary>
    ''' Structure input générique.
    ''' </summary>
    ''' <remarks></remarks>
    Private Structure INPUT
        Dim dwType As Integer                       ' type d'input : souris/clavier/hardware
        Dim mkhi As MOUSEKEYBDHARDWAREINPUT         ' structure définissant l'input
    End Structure
 
    ''' <summary>
    ''' Regroupement de structures souris/clavier/hardware.
    ''' </summary>
    ''' <remarks></remarks>
    <StructLayout(LayoutKind.Explicit)> _
    Private Structure MOUSEKEYBDHARDWAREINPUT
        <FieldOffset(0)> Public mi As MOUSEINPUT    ' input souris
        <FieldOffset(0)> Public ki As KEYBDINPUT    ' input clavier
        <FieldOffset(0)> Public hi As HARDWAREINPUT ' input hardware
    End Structure
 
    ''' <summary>
    ''' Input type souris.
    ''' </summary>
    ''' <remarks></remarks>
    Private Structure MOUSEINPUT
        Public dx As Integer                        ' position x de la souris
        Public dy As Integer                        ' position y de la souris
        Public mouseData As Integer                 ' ?
        Public dwFlags As Integer                   ' type d'événement
        Public time As Integer                      ' ?
        Public dwExtraInfo As Integer               ' ?
    End Structure
 
    ''' <summary>
    ''' Input type clavier.
    ''' </summary>
    ''' <remarks></remarks>
    Private Structure KEYBDINPUT
        Public wVk As Integer                       ' Virtual Keycode of keys. E.g., VK_RETURN, VK_TAB
        Public wScan As Integer                     ' Scan Code value of keys. E.g., 0xb8 for �Left Alt� key.
        Public dwFlags As Integer                   ' Flag that is set for key state. E.g., KEYEVENTF_KEYUP. 
        Public time As Integer                      ' ?
        Public dwExtraInfo As Integer               ' 32-bit extra information about keystroke. 
    End Structure
 
    ''' <summary>
    ''' Input type hardware.
    ''' </summary>
    ''' <remarks></remarks>
    Private Structure HARDWAREINPUT
        Public uMsg As Integer                      ' ?
        Public wParamL As Short                     ' ?
        Public wParamH As Short                     ' ?
    End Structure
 
    ''' <summary>
    ''' Fonction de simulation de clic gauche de la souris.
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub LeftClick()
        Dim input As INPUT = New INPUT()
 
        input.dwType = INPUT_MOUSE
        input.mkhi.mi.dwFlags = MOUSEEVENTF_LEFTDOWN
        input.mkhi.mi.dwExtraInfo = 0
        input.mkhi.mi.mouseData = 0
        input.mkhi.mi.time = 0
        SendInput(1, input, Marshal.SizeOf(input))
        input.mkhi.mi.dwFlags = MOUSEEVENTF_LEFTUP
        SendInput(1, input, Marshal.SizeOf(input))
    End Sub
 
    ''' <summary>
    ''' Fonction de simulation de la touche escape.
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub Esc()
        Dim input As INPUT = New INPUT()
 
        input.dwType = INPUT_KEYBOARD
        input.mkhi.ki.wVk = VK_ESCAPE
        input.mkhi.ki.wScan = 0
        input.mkhi.ki.dwFlags = KEYEVENTF_KEYDOWN
        input.mkhi.ki.time = 0
        input.mkhi.ki.dwExtraInfo = 0
        SendInput(1, input, Marshal.SizeOf(input))
        input.mkhi.ki.dwFlags = KEYEVENTF_KEYUP
        SendInput(1, input, Marshal.SizeOf(input))
        'keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYDOWN, 0)
        'keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0)
    End Sub
Vous remarquerez dans ma fonction "Esc" que j'ai essayé d'utiliser la version "keybd_event", mais celle-ci ne fonctionne pas et je suis donc resté sur "SendInput".

Au début, je ne faisais que l'événement KEYEVENTF_KEYDOWN et je pensais que le problème venait de là, mais même en ajoutant l'événement KEYEVENTF_KEYUP juste après, cela ne change rien...

Je travaille sous Windows XP Pro SP3 et Visual Studio 2008 Pro.

Merci d'avance