2009-06-20 10 views

Répondre

1

La fonction SendInput accepte un tableau de structures INPUT. Les structures INPUT peuvent être un événement souris ou clavier. Le keyboard event structure a un membre appelé wVk qui peut être n'importe quelle touche du clavier. Le fichier d'en-tête Winuser.h fournit des définitions de macro (VK_ *) pour chaque clé.

5
INPUT input; 
WORD vkey = VK_F12; // see link below 
input.type = INPUT_KEYBOARD; 
input.ki.wScan = MapVirtualKey(vkey, MAPVK_VK_TO_VSC); 
input.ki.time = 0; 
input.ki.dwExtraInfo = 0; 
input.ki.wVk = vkey; 
input.ki.dwFlags = 0; // there is no KEYEVENTF_KEYDOWN 
SendInput(1, &input, sizeof(INPUT)); 

input.ki.dwFlags = KEYEVENTF_KEYUP; 
SendInput(1, &input, sizeof(INPUT)); 

List of virtual key codes .....

+2

Utiliser 'VkKeyScanEx (char, KeyboardLayout)' 'mettre votre chars' "commune" dans cet exemple :) ... 'input.ki.wVk = VkKeyScanEx ('a', kbl);' en ce qui concerne KeyboardLayout, le plus simple est de charger le fichier keyboardLayout de la fenêtre courante: 'HKL kbl = GetKeyboardLayout (0);' –

+0

Merci et @ jave.web pour la réponse. J'ai fait mon code pour saisir le caractère dans la réponse ci-dessous (désolé je ne peux pas trouver comment insérer le code dans la section de commentaire). –

0

je fait une modification après avoir lu @ code de Nathan , this reference et combiné avec la suggestion de @ jave.web. Ce code peut être utilisé pour saisir des caractères (majuscules et minuscules).

#define WINVER 0x0500 
#include<windows.h> 
void pressKeyB(char mK) 
{ 
    HKL kbl = GetKeyboardLayout(0); 
    INPUT ip; 
    ip.type = INPUT_KEYBOARD; 
    ip.ki.time = 0; 
    ip.ki.dwFlags = KEYEVENTF_UNICODE; 
    if ((int)mK<65 && (int)mK>90) //for lowercase 
    { 
     ip.ki.wScan = 0; 
     ip.ki.wVk = VkKeyScanEx(mK, kbl); 
    } 
    else //for uppercase 
    { 
     ip.ki.wScan = mK; 
     ip.ki.wVk = 0; 

    } 
    ip.ki.dwExtraInfo = 0; 
    SendInput(1, &ip, sizeof(INPUT)); 
} 

est inférieure à la fonction pour appuyer sur la touche de retour:

void pressEnter() 
{ 
    INPUT ip; 
    ip.type = INPUT_KEYBOARD; 
    ip.ki.time = 0; 
    ip.ki.dwFlags = KEYEVENTF_UNICODE; 
    ip.ki.wScan = VK_RETURN; //VK_RETURN is the code of Return key 
    ip.ki.wVk = 0; 

    ip.ki.dwExtraInfo = 0; 
    SendInput(1, &ip, sizeof(INPUT)); 

} 
Questions connexes