请教组合模拟按键问题(100)

  • 主题发起人 主题发起人 网络无限
  • 开始时间 开始时间

网络无限

Unregistered / Unconfirmed
GUEST, unregistred user!
各位,我想把一个剪贴板内容粘贴到指定窗口里面,现在遇到的问题是粘贴出来的东西最后多了两个字母 vv ,因为我是后台模拟按键ctrl+v模拟ctrl-v的代码如下,因为是后台,所以不能用keybd_event postMessage(form_hw,WM_keydown,17,0);//CTRL 按下 postMessage(form_hw,WM_keydown,86,0);//V 按下 postMessage(form_hw,WM_keyup,86,0);//释放V postMessage(form_hw,WM_keyup,17,0);//释放 CTRL 请问代码错在什么地方,谢谢!
 
procedure StrokeKey(vkKey: Byte; KeyUp: Boolean);var P: TInput; KeyboardMsg: TMsg;begin P.Itype := 1; P.ki.wVk := vkKey; P.ki.wScan := vkKey; if KeyUp then P.ki.dwFlags := KEYEVENTF_KEYUP else P.ki.dwFlags := 0; if (vkKey in [VK_Up, VK_Down, VK_Left, VK_Right, VK_Home, VK_End, VK_Prior, VK_Next, VK_Insert, VK_Delete]) then P.ki.dwFlags := P.ki.dwFlags or KEYEVENTF_EXTENDEDKEY; P.ki.time := 0; P.ki.dwExtraInfo := GetMessageExtraInfo; SendInput(1, P, SizeOf(P)); while (PeekMessage(KeyboardMsg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE)) do begin TranslateMessage(KeyboardMsg); DispatchMessage(KeyboardMsg); end;end;procedure TForm1.Button1Click(Sender: TObject);begin StrokeKey(VK_CONTROL, false); postMessage(Memo1.Handle, WM_keyUp, 86, 0); StrokeKey(VK_CONTROL, True);end;
 
感谢你的代码,但还是不行,我是开始发送回车获得外部程序的发言栏焦点,然后模拟按ctrl-v把剪贴板的内容复制进去,再按回车发送出去,代码如下postMessage(Myhwnd,WM_KEYDOWN,VK_RETURN,0); ///回车postMessage(Myhwnd,WM_KEYup,VK_RETURN,0);sleep(100);postMessage(Myhwnd,WM_keydown,17,0);postMessage(Myhwnd,WM_keydown,86,0);postMessage(Myhwnd,WM_keyup,86,0);postMessage(Myhwnd,WM_keyup,17,0);sleep(100);postMessage(Myhwnd,WM_KEYDOWN,VK_RETURN,0); ///回车postMessage(Myhwnd,WM_KEYup,VK_RETURN,0);这样发出去的话就有两个V跟在后面,而我手动操作按键就不会出现,我用你的代码代替我的4行postMessage代码(ctrl-v)出来结果只有一个V,连剪贴板的都复制不进去,我把你的修改为如下替换,还是出不来剪贴板,只出来两个V StrokeKey(VK_CONTROL, True); postMessage(Myhwnd,WM_keydown, 86, 0); postMessage(Myhwnd, WM_keyUp, 86, 0); StrokeKey(VK_CONTROL, false);另外说明一下,我程序的目的是在聊天窗口自动发言功能,但用keybd_event代替那4行代码可以实现,但无法后台。
 
我已解决,我把那4行代码postMessage(Myhwnd,WM_keydown,17,0);postMessage(Myhwnd,WM_keydown,86,0);postMessage(Myhwnd,WM_keyup,86,0);postMessage(Myhwnd,WM_keyup,17,0);换为:sendMessage(Myhwnd,WM_keydown,17,0);sendMessage(Myhwnd,WM_keydown,86,0);sendMessage(Myhwnd,WM_keyup,86,0);sendMessage(Myhwnd,WM_keyup,17,0);就可以了。
 

Similar threads

I
回复
0
查看
759
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部