-1

I want to create a program which simulates key presses or just write a text to a specific application window. This is what I've tried but it doesn't seem to work.

void MainWindow::on_startButton_clicked()
{
    HWND windowname = FindWindow(NULL, L"<mywindowname>");
    SendMessage(windowname, WM_SETTEXT, NULL, (LPARAM)"Window");
}

Any ideas?

Taylor Brandstetter
  • 3,523
  • 15
  • 24
Davlog
  • 2,162
  • 8
  • 36
  • 60
  • You'll probably want that text to be a wide string, but unless that's your own window, you're probably better off with `SetWindowText`. – chris Nov 21 '13 at 17:45
  • @chris does this change the window title? because thats not what i want – Davlog Nov 21 '13 at 17:46
  • 1
    @Davlog: `WM_SETTEXT` is mostly the same... if you want to simulate keypresses you have to use `SendInput` or individual `WM_KEYDOWN`/`WM_KEYUP` (but if the application bypasses the normal key messages to get input they won't work). – Matteo Italia Nov 21 '13 at 17:55
  • See my answer here, which can easily be converted to c++: http://stackoverflow.com/a/41886193/1599699 – Andrew Jan 27 '17 at 02:26

1 Answers1

0

So I found out today that SendMessage will only send a single character (at least it's the solution for my problem). So for a string I have to do the following for each character in it.

QString string = "Hello";
foreach(QChar c, string){
  SendMessage(windowname, WM_KEYDOWN, (int)c-0x20, 0);
  SendMessage(windowname, WM_CHAR, (int)c-0x20, 0);
  SendMessage(windowname, WM_KEYUP, (int)c-0x20, 0);
}

For more information about this function and deeper details on WM_KEY's etc. click here

Davlog
  • 2,162
  • 8
  • 36
  • 60