Bonjour,
J'ai une application Windows Forms avec un bouton "SendKeys" et un champ de texte.
Voici ce que j'essaie de faire :
Je lance mon application.
Je crée un processus "notepad" et j'en garde le Handle.
J'entre du texte dans le champ de texte.
J'appuie sur mon bouton.
Le texte entré s'affiche sur Notepad.
Je n'ai pas de problèmes pour lancer et référencer Notepad.
J'ai un problème pour envoyer du texte vers Notepad.
Quand j'ai utilisé Form.SendKeys, j'ai pu envoyer des touches la première fois, mais les fois suivantes où j'ai cliqué sur le bouton, rien ne se passe sur Notepad.
Alors, j'ai écarté la méthode Form.SendKeys. De toutes façons, je ne veux pas dépendre de Windows.Forms si je crée d'autres applications.
Alors, j'utilise SendInput pour envoyer des évènements clavier à Notepad. Cette méthode marche bien, mais je n'arrive pas à convertir les caractères dans mon champ de texte. Par exemple, si je tape le caractère 'a' dans mon champ de texte, Notepad reçoit NumPad1.
Voici des extraits du code que j'ai utilisé :
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 public const int INPUT_MOUSE = 0; public const int INPUT_KEYBOARD = 1; public const int INPUT_HARDWARE = 2; [DllImport( "user32.dll" )] public static extern bool SetForegroundWindow (IntPtr hWnd); [DllImport( "user32.dll" )] public static extern IntPtr GetForegroundWindow (); [DllImport( "user32.dll" )] public static extern IntPtr GetMessageExtraInfo ();
Code C# : 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 [StructLayout( LayoutKind.Explicit )] public struct INPUT { [FieldOffset( 0 )] public int type; [FieldOffset( 4 )] public MOUSEINPUT mi; [FieldOffset( 4 )] public KEYBDINPUT ki; [FieldOffset( 4 )] public HARDWAREINPUT hi; } [StructLayout( LayoutKind.Sequential )] public struct MOUSEINPUT { public int dx; public int dy; public uint mouseData; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; } [StructLayout( LayoutKind.Sequential )] public struct KEYBDINPUT { public ushort wVk; public ushort wScan; public uint dwFlags; public uint time; public IntPtr dwExtraInfo; } [StructLayout( LayoutKind.Sequential )] public struct HARDWAREINPUT { public uint uMsg; public ushort wParamL; public ushort wParamH; }
Ce code envoie bien le caractère 'W'
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 INPUT input = new INPUT(); input.type = Puppr.INPUT_KEYBOARD; input.ki = new KEYBDINPUT(); input.ki.dwExtraInfo = GetMessageExtraInfo(); input.ki.dwFlags = 0; input.ki.time = 0; input.ki.wScan = 0; input.ki.wVk = (ushort) System.Windows.Forms.Keys.W; SendInput( 1, ref input, Marshal.SizeOf( input ) );
Mais ce code n'envoie pas les caractères dans TextBox. Par exemple, si le texte dans le champ de texte est "abc", notepad reçoit "123".
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 foreach ( char ch in textBox1.Text ) { input.ki.wVk = (ushort) ch; SendInput( 1, ref input, Marshal.SizeOf( input ) ); }
Je voudrais savoir comment convertir le texte dans un TextBox en ushort pour SendInput.
Partager