2009-06-20 8 views

risposta

1

La funzione SendInput accetta un array di strutture INPUT. Le strutture INPUT possono essere un evento mouse o tastiera. Lo keyboard event structure ha un membro chiamato wVk che può essere qualsiasi tasto sulla tastiera. Il file di intestazione Winuser.h fornisce definizioni di macro (VK_ *) per ogni chiave.

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

Usa 'VkKeyScanEx (char, KeyboardLayout)' per mettere il "comune" 'chars' in questo esempio :) ... 'input.ki.wVk = VkKeyScanEx ('a', kbl);' come per KeyboardLayout il modo più semplice è caricare il keyboardLayout della finestra corrente: 'HKL kbl = GetKeyboardLayout (0);' –

+0

Grazie e @ jave.web per la risposta. Ho inventato il mio codice per inserire il carattere nella risposta di seguito (mi dispiace non riesco a trovare come inserire il codice nella sezione commenti). –

0

Ho fatto una modifica dopo la lettura del codice di Nathan @ , this reference e combinato con il suggerimento di @ jave.web. Questo codice può essere utilizzato per inserire caratteri (sia maiuscoli che minuscoli).

#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)); 
} 

Di seguito è la funzione per premendo il tasto di ritorno:

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)); 

} 
Problemi correlati