J'utilise l'appel Windows API SendInput() pour simuler des événements de clavier. L'extrait suivant (détails non pertinents omis) fonctionne parfaitement pour envoyer une séquence de caractères:Comment spécifier les modificateurs kbd avec SendInput()?
wchar_t txt = ...;
INPUT *input = ...;
size_t nInput = 0;
for (unsigned int j = 0; j < length; j++) {
input[nInput].ki.wVk = 0;
input[nInput].ki.wScan = txt[j];
input[nInput].ki.dwFlags = KEYEVENTF_UNICODE;
nInput++;
input[nInput].ki.wVk = 0;
input[nInput].ki.wScan = txt[j];
input[nInput].ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
nInput++;
}
SendInput(nInput, input, sizeoF(INPUT));
Maintenant, je suis en train d'envoyer des touches individuelles, avec des modificateurs. J'ai essayé le code suivant:
bool control, alt shift;
wchar_t chr;
if (control) {
input[nInput].ki.wVk = VK_CONTROL;
input[nInput].ki.dwFlags = 0;
nInput++;
}
if (alt) {
input[nInput].ki.wVk = VK_MENU;
input[nInput].ki.dwFlags = 0;
nInput++;
}
if (shift) {
input[nInput].ki.wVk = VK_SHIFT;
input[nInput].ki.dwFlags = 0;
nInput++;
}
input[nInput].ki.wVk = 0;
input[nInput].ki.wScan = chr;
input[nInput].ki.dwFlags = KEYEVENTF_UNICODE;
nInput++;
input[nInput].ki.wVk = 0;
input[nInput].ki.wScan = chr;
input[nInput].ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
nInput++;
if (shift) {
input[nInput].ki.wVk = VK_SHIFT;
input[nInput].ki.dwFlags = KEYEVENTF_KEYUP;
nInput++;
}
if (alt) {
input[nInput].ki.wVk = VK_MENU;
input[nInput].ki.dwFlags = KEYEVENTF_KEYUP;
nInput++;
}
if (control) {
input[nInput].ki.wVk = VK_CONTROL;
input[nInput].ki.dwFlags = KEYEVENTF_KEYUP;
nInput++;
}
SendInput(nInput, input, sizeof(INPUT));
Cependant, les modificateurs ne semblent pas passer à travers, à savoir, même si, disent control
est à true, la séquence d'événements est reçue comme une simple pression de touche.
@David, vous devriez probablement accepter cette réponse sinon SO continuera à vous déranger avec "avez-vous pensé à accepter une réponse à cette question". – paxdiablo
@Pax: bien sûr, je vais le faire, mais d'abord je voulais donner aux autres une chance d'améliorer ma réponse ou de signaler mes erreurs. –