[Interop][C#]SendInput � partir de TextBox
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:
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:
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:
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:
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.