keybd_event 可以模拟输入一行字符串吗? Delphi / Windows SDK/APIhttp://www.delphi2007.net/DelphiAPI/html/delphi_20061206175530118.html
倒如VB用
WshShell.SendKeys "123456789"
这样就完成了
总不能每一个字符都用
keybd_event($75, MapVirtualKey($75, 0 ), 0 , 0 );
keybd_event(VK_F6, MapVirtualKey(VK_F6,0), KEYEVENTF_KEYUP,0);
这样吧.....
把字符串放入剪贴板,然后keybd_event一个CTRL+C不就行了吗?
说错了,CTRL+V
procedure TypeKeyString(s: string);
var
c: Char;
i: integer;
off: integer;
vkw: Word;
begin
for i := 1 to Length(s) do
begin
c := s[i];
if (c < #128) then
begin
vkw := VkKeyScan(c);
off := 0;
if vkw and $100 = $100 then
keybd_event(VK_SHIFT, 0, off, 0);
if vkw and $200 = $200 then
keybd_event(VK_CONTROL, 0, off, 0);
if vkw and $400 = $400 then
keybd_event(VK_MENU, 0, off, 0);
off := 0;
keybd_event(Byte(vkw), 0, off, 0);
//sleep(20);
off := off or KEYEVENTF_KEYUP;
keybd_event(Byte(vkw), 0, off, 0);
off := off or KEYEVENTF_KEYUP;
if vkw and $100 = $100 then
keybd_event(VK_SHIFT, 0, off, 0);
if vkw and $200 = $200 then
keybd_event(VK_CONTROL, 0, off, 0);
if vkw and $400 = $400 then
keybd_event(VK_MENU, 0, off, 0);
end;
end;
end;
CSDN真讨厌。为什么要把对齐用的空格删除?
谢谢强哥
但还有一个问题就是。
setForegroundWindow(FindWindow(nil,'无标题 - 记事本'));
这不能对最少化或是隐藏窗体起作用。。。。
如果这样的窗体应该怎么键入字符串呢?
SendMessage(FindWindow(nil,'无标题 - 记事本'),wm_syscommand,sc_RESTORE,0);
先后这个问题解决了。。。。。
楼上正解
记事本?
var
vHandle: THandle;
begin
vHandle := FindWindow('Notepad', nil);
if vHandle = 0 then Exit;
vHandle := FindWindowEx(vHandle, 0, 'Edit', nil);
if vHandle = 0 then Exit;
SendMessage(vHandle, EM_REPLACESEL, 0, Longint(PChar('Zswang路过...')));
end;