Hi, How can I send keystrokes(like Ctrl + V) to a minimised Notepad ?

To send some text I use something like this:

HWND ahwnd;
char text[100] = "it works !";

ahwnd = FindWindow("Notepad",NULL)
SendMessage(FindWindowEx(ahwnd, NULL, "Edit", NULL), WM_SETTEXT, 0, (LPARAM)text);

Give me an example please.

Here is an article that explains how to do it.

creat an empty file (result.txt) in the folder where you are working with coding then use the keyword of File ans save your keystrocks in the .txt file.
here is an example

#include<conio.h>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;


int main()
{
            ifstream f("result.txt");                                    //to read frm file
            char c;
            while(!f.eof())
            {
                f.get(c);
                cout<<c;

            }

            cout<<"or you can enter my result and my program will save it  in my result file"<<endl;
            string s;
            cin>>s;
            cout<<endl;
            File<<s<<endl;
                        f.close();
return 0;


}

Well, that's kind of what I want to do Ancient Dragon, but I would like to have the process minimised when I send the keystrokes. I'm sure there's a way to use SendMessage() with WM_KEYUP and WM_KEYDOWN and send keystrokes to a process but I need a working example.

Oh and this isn't a keylogger, this is sending keystrokes to a minimised process, not storing keystrokes, Notepad was just an example.

PostMessage(FindWindowEx(ahwnd, NULL, "Edit", NULL), WM_KEYDOWN, 0x11, 0);//0x11 = Ctrl
PostMessage(FindWindowEx(ahwnd, NULL, "Edit", NULL), WM_KEYDOWN, 0x56, 0);//0x56 = v
PostMessage(FindWindowEx(ahwnd, NULL, "Edit", NULL), WM_KEYUP, 0x56, 0);
PostMessage(FindWindowEx(ahwnd, NULL, "Edit", NULL), WM_KEYUP, 0x11, 0);

It's quite amazing that this works better than SendMessage.

So I run these but instead of Ctrl + v (Paste) I get only v.
Is it because I'm sending these to "Edit" ?

SendMessage() only sends events to the current thread, so it won't work with other threads/procsses. PostThreadMessage() may or may not work, depending on what the thread is doing at the time (see this link for more info about that).

>>Is it because I'm sending these to "Edit" ?
Probably not. You are wasting a huge amount of time calling FindWindow() so often. Call it only once and store the result in a variable.

Well, I gave up on Notepad, I don't know where I have to send those keystrokes to make a paste.

Using FindWindow() I need to know the class name of the window to get a handle but what if I only know the window name or process name ? How would I get a handle for the window ?

Example:

#include <iostream>
#include <string>
#include <Windows.h>
using std::cout;



BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
{
    char title[500] = {0};

    GetWindowText(hWnd, title, sizeof(title));
    if(strstr(title, "Notepad"))
    {
        HWND* windowHandle = (HWND*)lParam;
        *windowHandle = hWnd;
        cout << title << '\n';
        return FALSE;
    }
    return TRUE;
}

int main()
{
    HWND windowHandle = 0;
    EnumWindows(MyEnumProc, (LPARAM)&windowHandle);
}
commented: Great example ! +1

Super ! Works like a charm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.