C++ 如何将按键发送到窗口?

C++ 如何将按键发送到窗口?,c++,windows,C++,Windows,im使用keybd_事件();我想使用SendMessage();要将击键发送到记事本,可以这样做吗 使用SendMessage将文本插入编辑缓冲区(听起来像是您想要的): 如果您需要键码和任意击键,您可以使用(2k/xp和首选)或`(在较新的OSs中最终将调用SendInput)以下示例: SendMessage还有您可能感兴趣的WM_SYSCOMMAND/WM_KEYDOWN/WM_KEYUP/WM_CHAR事件。keybd_event()已被SendInput()取代,因此最好使用它。下

im使用keybd_事件();我想使用SendMessage();要将击键发送到记事本,可以这样做吗

使用
SendMessage
将文本插入编辑缓冲区(听起来像是您想要的):

如果您需要键码和任意击键,您可以使用(2k/xp和首选)或`(在较新的OSs中最终将调用SendInput)以下示例:

SendMessage还有您可能感兴趣的WM_SYSCOMMAND/WM_KEYDOWN/WM_KEYUP/WM_CHAR事件。

keybd_event()已被SendInput()取代,因此最好使用它。下面是一些示例代码,用于执行您所要求的操作。您可以使用SendMessage()直接编辑记事本窗口的编辑控件,也可以使用SendInput()合成要发送到窗口的按键

使用:


我希望这会有帮助。

这会如何将它发送到窗口?它不会。您需要在
hWnd
参数中传递窗口句柄。此外,窗口句柄是第一个参数,而不是第三个参数。不能使用SendMessage()发送击键。您无法控制键盘状态。特别是Shift键、Control键和Alt键。WM_SETTEXT也不会发送击键。在任何情况下,您都不能像nobugz提到的那样可靠地使用SendMessage()。请看,您不需要发送按键来输入文本。OP已经知道
keybd\u事件
。问题是关于使用
SendMessage
在SendInput()exmaple中,KEYEVENTF\u UNICODE是否关心文本的类型,也可能是字符?我看不到KEYEVENTF_扫描码,但你把你的角色放在了wScan中。GetMessageExtraInfo()是否必要?@DevNoob:不,这应该适用于Unicode和非Unicode版本。(我刚刚测试过。)是的,GetMessageExtraInfo()似乎是必需的,因为文档指定它是必需的。顺便说一句,您的问题的答案也可以通过使用我答案中的链接阅读相应函数和类型的MSDN文档来找到。@DevNoob,我不完全确定您所说的被忽略的
KEYEVENTF\u KEYUP
是什么意思。我也不知道你所引用的那部分文档是什么意思。如果您想知道为什么同时需要按键向下和按键向上键盘事件,那是因为这些事件用于为目标窗口生成相应的WM_按键向下和
WM_按键向上
窗口消息。我们正在进行低级别的工作,我认为Windows API不提供表示单个按键的
WM\u KEYPRESS
消息。@DevNoob,使用
WM\u keypdown
WM\u keypup
实际上是一个很好的解决方案,因为它非常兼容。这是因为这些窗口消息几乎是所有Windows应用程序的标准消息。把它们分开也给了我们更多的控制权。我能想到的唯一不支持这种方法的Windows应用程序类型是那些使用DirectInput的应用程序。这样做的一个缺点是SendInput只将消息发送到当前关注的窗口,该窗口可能类似于菜单,而不是文本字段。很抱歉,复制粘贴错误。是的,但MS应该清楚地区分发送的字符和必须用作输入的字符。这就是我第一眼就误解的。我仍然不明白的是为什么KEYEVENTF\u扫描码被遗漏了。根据文档“…如果指定,wScan将识别密钥,wVk将被忽略。”来自
HWND notepad = FindWindow(_T("Notepad"), NULL);
HWND edit = FindWindowEx(notepad, NULL, _T("Edit"), NULL);
SendMessage(edit, WM_SETTEXT, NULL, (LPARAM)_T("hello"));
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;
}
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;
}