Envoyer un code clavier à une appli directx

Résolu
survcopt Messages postés 224 Date d'inscription mardi 27 mai 2003 Statut Membre Dernière intervention 13 mai 2022 - 25 août 2009 à 16:16
survcopt Messages postés 224 Date d'inscription mardi 27 mai 2003 Statut Membre Dernière intervention 13 mai 2022 - 7 sept. 2009 à 15:04
Bonjour,
J'ai un jeu qui fonctionne avec directx, je voudrais lui envoyer un code clavier pour simuler un appui .
j'ai essayé les fonctions sendkeys sendinput et je sèche .
Je passe l'appli en premier plan je lui donne le focus mais la commande n'est pas réalisée lorsque le code clavier est envoyé .
quelqu'un aurait'il déja bosser la dessus?
Merci,


www.survey-copter.com

10 réponses

survcopt Messages postés 224 Date d'inscription mardi 27 mai 2003 Statut Membre Dernière intervention 13 mai 2022 3
7 sept. 2009 à 15:04
Il n'y a rien qui marche finalement j'ai fait un exe avec autohotkey et je lance le exe depuis mon programme et ça marche.


www.survey-copter.com
3
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
25 août 2009 à 20:05
Salut, dans le jeu les entrées du clavier sont sûrement géré par DirectInput, il faut simuler la frappe sur une touche avec SendInput.

J'avais répondu à cette question ici :

http://www.cppfrance.com/forum/sujet-SIMULER-SOURIS-DIRECTINPUT_781079.aspx
0
survcopt Messages postés 224 Date d'inscription mardi 27 mai 2003 Statut Membre Dernière intervention 13 mai 2022 3
26 août 2009 à 09:29
Je suis en train d'essayer sendinput sans succès pour valider mes essais j'utilise le sample directinpunt keyboard de directx SDK 9.0.
voici mon code

Déclarations
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();

[DllImport("user32.dll", SetLastError = true)]
public static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);

[DllImport("user32.dll")]
static extern short VkKeyScan(char ch);

[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;
}




Routine

private void ToggleLimiteur()
{
//IntPtr hWnd = FindWindow( null, "Sans titre - Bloc-notes" );

IntPtr HandleRfactor;
HandleRfactor = GetHandle();
SetFocus(new HandleRef(null, HandleRfactor));


if (HandleRfactor != IntPtr.Zero)
{

if (SetForegroundWindow(HandleRfactor))
{
Thread.Sleep(50);


INPUT input = new INPUT();
input.type = 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.L;

short key = VkKeyScan('L');
input.ki.wVk = (ushort)key;

SendInput(1, ref input, Marshal.SizeOf(input));


}
}
}


Avec le bloc note ça marche mais pas avec directx




www.survey-copter.com
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
26 août 2009 à 15:10
Essaye d'envoyer une fois la touche en position DOWN et une fois en position UP, pour simuler une vraie frappe.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
survcopt Messages postés 224 Date d'inscription mardi 27 mai 2003 Statut Membre Dernière intervention 13 mai 2022 3
26 août 2009 à 15:13
ça je ne sais pas faire désolé


www.survey-copter.com
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
26 août 2009 à 15:45
Dans la struct KEYBDINPUT le champ dwFlags doit être à KEYEVENTF_KEYUP . la doc est là :

http://msdn.microsoft.com/en-us/library/ms646271(VS.85).aspx
0
survcopt Messages postés 224 Date d'inscription mardi 27 mai 2003 Statut Membre Dernière intervention 13 mai 2022 3
26 août 2009 à 16:26
0
survcopt Messages postés 224 Date d'inscription mardi 27 mai 2003 Statut Membre Dernière intervention 13 mai 2022 3
26 août 2009 à 16:34
avec ce code ça ne marche pas sendinput me renvoie 2 , mais je n'arrive pas à savoir à quelle erreur ça correspond.


www.survey-copter.com
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
26 août 2009 à 23:00
Sous Vista je sais que les inputs peuvent être bloqués selon les privilèges de l'application, il peut être intéressant de tester avec une application éxecutée en tant qu'admin..

Sinon SendInput ne renvoi pas un code d'érreur mais le nombre d'inputs réussis.
0
survcopt Messages postés 224 Date d'inscription mardi 27 mai 2003 Statut Membre Dernière intervention 13 mai 2022 3
27 août 2009 à 10:19
En effet je suis sous Vista.

Sais-tu comment lancer un programme avec shellexecute avec les droits d'admin.
En fait j'ai un client sur un PC qui lance des commandes dont le lancement du jeu et l'envoi de commande clavier.
voici le code pour lancer le jeu comment lui passer en paramètres le fait de se lancer mode administrateur

StreamWriter SW;
SW = File.CreateText(Application.StartupPath + "\\1.bat");
SW.WriteLine('"' + "C:\\Program Files\\rFactor\\rFactor.exe" + '"');
SW.Close();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("1.bat");
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process monProcess;
monProcess = System.Diagnostics.Process.Start(psi);


Merci,

www.survey-copter.com
0
Rejoignez-nous