2010-01-21 25 views

risposta

7

utilizzando SendMessage per inserire il testo nel buffer di modifica (che suona come volete):

HWND notepad = FindWindow(_T("Notepad"), NULL); 
HWND edit = FindWindowEx(notepad, NULL, _T("Edit"), NULL); 
SendMessage(edit, WM_SETTEXT, NULL, (LPARAM)_T("hello")); 

se avete bisogno di keycode e le battiture arbitrari, è possibile utilizzare SendInput() (disponibile in 2K/XP e preferito) o keybd_event() `(che finirà per chiamare SendInput in nuovi microprocessori) alcuni esempi qui:

http://www.codeguru.com/forum/showthread.php?t=377393

c'è anche WM_SYSCOMMAND/WM_KEYDOWN/WM_KEYUP/WM_CHAR eventi per Sen dMessaggio a cui potresti essere interessato.

+0

Come viene inviato a una finestra? – H4cKL0rD

+0

Non è così. Devi passare un handle di finestra nel parametro 'hWnd'. Inoltre, l'handle della finestra è il * primo * parametro, non il terzo. –

+0

Non è possibile utilizzare SendMessage() per inviare sequenze di tasti. Non puoi controllare lo stato della tastiera. In particolare i tasti Shift, Control e Alt. –

5

keybd_event() è stato sostituito con SendInput(), quindi è meglio utilizzarlo. Ecco alcuni esempi di codice per fare ciò che hai chiesto. È possibile modificare direttamente il controllo di modifica della finestra del Blocco note utilizzando SendMessage(), oppure è possibile utilizzare SendInput() per sintetizzare le sequenze di tasti da inviare alla finestra.

Utilizzando SendInput():

int SendKeystrokesToNotepad(const TCHAR *const text) 
{ 
    INPUT *keystroke; 
    UINT i, character_count, keystrokes_to_send, keystrokes_sent; 
    HWND notepad; 

    assert(text != NULL); 

    //Get the handle of the Notepad window. 
    notepad = FindWindow(_T("Notepad"), NULL); 
    if(notepad == NULL) 
     return 0; 

    //Bring the Notepad window to the front. 
    if(!SetForegroundWindow(notepad)) 
     return 0; 

    //Fill in the array of keystrokes to send. 
    character_count = _tcslen(text); 
    keystrokes_to_send = character_count * 2; 
    keystroke = new INPUT[ keystrokes_to_send ]; 
    for(i = 0; i < character_count; ++i) 
    { 
     keystroke[ i * 2 ].type = INPUT_KEYBOARD; 
     keystroke[ i * 2 ].ki.wVk = 0; 
     keystroke[ i * 2 ].ki.wScan = text[ i ]; 
     keystroke[ i * 2 ].ki.dwFlags = KEYEVENTF_UNICODE; 
     keystroke[ i * 2 ].ki.time = 0; 
     keystroke[ i * 2 ].ki.dwExtraInfo = GetMessageExtraInfo(); 

     keystroke[ i * 2 + 1 ].type = INPUT_KEYBOARD; 
     keystroke[ i * 2 + 1 ].ki.wVk = 0; 
     keystroke[ i * 2 + 1 ].ki.wScan = text[ i ]; 
     keystroke[ i * 2 + 1 ].ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP; 
     keystroke[ i * 2 + 1 ].ki.time = 0; 
     keystroke[ i * 2 + 1 ].ki.dwExtraInfo = GetMessageExtraInfo(); 
    } 

    //Send the keystrokes. 
    keystrokes_sent = SendInput((UINT)keystrokes_to_send, keystroke, sizeof(*keystroke)); 
    delete [] keystroke; 

    return keystrokes_sent == keystrokes_to_send; 
} 

Utilizzando SendMessage():

int SendKeystrokesToNotepad(const TCHAR *const text) 
{ 
    HWND notepad, edit; 

    assert(text != NULL); 

    //Get the handle of the Notepad window. 
    notepad = FindWindow(_T("Notepad"), NULL); 
    if(notepad == NULL) 
     return 0; 

    //Get the handle of the Notepad window's edit control. 
    edit = FindWindowEx(notepad, NULL, _T("Edit"), NULL); 
    if(edit == NULL) 
     return 0; 

    SendMessage(edit, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)text); 
    return 1; 
} 

Mi auguro che aiuta.

+0

Nel file SendInput() exmaple, il KEYEVENTF_UNICODE si preoccupa del tipo di testo, che può essere anche un carattere? Non vedo KEYEVENTF_SCANCODE ma metti il ​​tuo personaggio in wScan. È necessario GetMessageExtraInfo()? – ManuelSchneid3r

+1

@DevNoob: No, questo dovrebbe funzionare sia per le build Unicode che per quelle non Unicode. (L'ho appena testato.) Sì, GetMessageExtraInfo() _appears_ è necessario perché la documentazione specifica che è necessario. A proposito, le risposte alle tue domande possono anche essere trovate leggendo la documentazione MSDN per la funzione e i tipi corrispondenti usando il link nella mia risposta. – Sam

+0

@DevNoob, non sono completamente sicuro di cosa intendi per "KEYEVENTF_KEYUP" lasciato fuori. Inoltre, non sono sicuro di cosa intendi per la parte della documentazione che hai citato. Se ti stai chiedendo perché sono necessari entrambi gli eventi tastiera key-down e key-up, è perché questi vengono utilizzati per generare i corrispondenti messaggi WM_KEYDOWN e 'WM_KEYUP' per la finestra di destinazione. Stiamo lavorando a un livello basso e non penso che l'API di Windows fornisca un messaggio 'WM_KEYPRESS' per rappresentare una sola chiave _press_. – Sam

Problemi correlati