tags:

views:

61

answers:

1

Hey, I am trying to type a message into notepad without having to have it as my focus window (Foreground Window).

This is what I have so far:

    const UInt32 WM_KEYDOWN = 0x0100;
    const int VK_F5 = 0x74;

    [DllImport("user32.dll")]
    static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
    [STAThread]

    private void button2_Click(object sender, EventArgs e)
    {
            Process[] processes = Process.GetProcessesByName("Notepad");

            foreach (Process proc in processes)
                PostMessage(proc.MainWindowHandle, VK_TAB, VK_S, 0);

    }

But this does not type S into notepad. When I use VK_F5, it displays the date/time and when I use VK_F1 that displays the help window but it does not type s.

I have thought about using SendKey but that requires me to be targeting Notepad.

Any help would be appreciated.

Thanks.

+1  A: 

if when you use VK_F5 it inserts the date, that means the send message procedure is working.

the problem is the destination of the message, you are sending the messages to the notepad window handle,

if you want keys to be written in the text area, you need to get the handle of the text area control of the notepad application and send the message to it

camilin87
Ah I see, do you know how I would do this?
Crazyd22
in these articles, they manage to get the handle of practically anything, take a dive into themhttp://www.codeproject.com/KB/dotnet/objectspy.aspxhttp://www.codeproject.com/KB/threads/winspy.aspx
camilin87
Brilliant, ill have a look at them, thanks!
Crazyd22