C++ 如何将击键发送到窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2113950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send keystrokes to a window?
提问by H4cKL0rD
im using keybd_event(); and i want use SendMessage(); to send keystroke to notepad, can this be done?
我正在使用 keybd_event(); 我想使用 SendMessage(); 将击键发送到记事本,可以这样做吗?
采纳答案by jspcal
using SendMessage
to insert text into the edit buffer (which it sounds like you want):
使用SendMessage
插入文字编辑缓冲区(这听起来像你想要的):
HWND notepad = FindWindow(_T("Notepad"), NULL);
HWND edit = FindWindowEx(notepad, NULL, _T("Edit"), NULL);
SendMessage(edit, WM_SETTEXT, NULL, (LPARAM)_T("hello"));
if you need keycodes and arbitrary keystrokes, you can use SendInput()
(available in 2k/xp and preferred), or keybd_event()
` (which will end up calling SendInput in newer OSs) some examples here:
如果您需要键码和任意键击,您可以使用SendInput()
(在 2k/xp 和首选版本中可用)或keybd_event()
`(在较新的操作系统中最终会调用 SendInput)一些示例:
http://www.codeguru.com/forum/showthread.php?t=377393
http://www.codeguru.com/forum/showthread.php?t=377393
there's also WM_SYSCOMMAND/WM_KEYDOWN/WM_KEYUP/WM_CHAR events for SendMessage which you might be interested in.
还有您可能感兴趣的 SendMessage 的 WM_SYSCOMMAND/WM_KEYDOWN/WM_KEYUP/WM_CHAR 事件。
回答by Sam
keybd_event() has been superseded with SendInput(), so it's best to use that. Here is some example code to do what you have asked. You can either directly edit the Notepad window's edit control using SendMessage(), or you can use SendInput() to synthesise keystrokes to be sent to the window.
keybd_event() 已被 SendInput() 取代,因此最好使用它。这是一些示例代码来执行您的要求。您可以使用 SendMessage() 直接编辑记事本窗口的编辑控件,也可以使用 SendInput() 合成要发送到窗口的击键。
Using SendInput()
:
使用SendInput()
:
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;
}
Using SendMessage()
:
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;
}
I hope that helps.
我希望这有帮助。