开发者

Virtual Key Press in window C#

Hey, I am trying to type a message into notepad without having to have it as my focus window (Foregrou开发者_JS百科nd 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜