if Handle <> 0 then begin SetForegroundWindow(Handle); keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), 0, 0); keybd_event(Ord('S'), MapVirtualKey(Ord('S'), 0), 0, 0); keybd_event(Ord('S'), MapVirtualKey(Ord('S'), 0), KEYEVENTF_KEYUP, 0); keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_KEYUP, 0); end;
sorry,搞错了 应该是 if Handle <> 0 then begin SetForegroundWindow(Handle); keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0); keybd_event(Ord('S'), MapVirtualKey(Ord('S'), 0), 0, 0); keybd_event(Ord('S'), MapVirtualKey(Ord('S'), 0), KEYEVENTF_KEYUP, 0); keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0); end;另外你说用SendMessage来实现比较困难,稍后看看能不能解决
这样看看, SendMessage(hwnd, WM_SYSKEYDOWN, wParam, lParam);wParam,lParam说明见下。wParam Specifies the virtual-key code of the key being pressed. lParam Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table. Value Description 0–15 Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative. 16–23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM). 24 Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0. 25–28 Reserved; do not use. 29 Specifies the context code. The value is 1 if the ALT key is down while the key is pressed; it is 0 if the WM_SYSKEYDOWN message is posted to the active window because no window has the keyboard focus. 30 Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up. 31 Specifies the transition state. The value is always 0 for a WM_SYSKEYDOWN message.
if Handle <> 0 then
begin
SetForegroundWindow(Handle);
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), 0, 0);
keybd_event(Ord('S'), MapVirtualKey(Ord('S'), 0), 0, 0);
keybd_event(Ord('S'), MapVirtualKey(Ord('S'), 0), KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_KEYUP, 0);
end;
应该是 if Handle <> 0 then
begin
SetForegroundWindow(Handle);
keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);
keybd_event(Ord('S'), MapVirtualKey(Ord('S'), 0), 0, 0);
keybd_event(Ord('S'), MapVirtualKey(Ord('S'), 0), KEYEVENTF_KEYUP, 0);
keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);
end;另外你说用SendMessage来实现比较困难,稍后看看能不能解决
SendMessage(hwnd, WM_SYSKEYDOWN, wParam, lParam);wParam,lParam说明见下。wParam
Specifies the virtual-key code of the key being pressed.
lParam
Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table. Value Description
0–15 Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
16–23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM).
24 Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25–28 Reserved; do not use.
29 Specifies the context code. The value is 1 if the ALT key is down while the key is pressed; it is 0 if the WM_SYSKEYDOWN message is posted to the active window because no window has the keyboard focus.
30 Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.
31 Specifies the transition state. The value is always 0 for a WM_SYSKEYDOWN message.
帮助我也看了,总是试不成功
sendmessage(ahandle, WM_CHAR, ord('s'), $00380001);//$61000001);
sendmessage(ahandle, WM_KEYDOWN, vk_menu+ord('s'), $001f0008);//$61000001);
sendmessage(ahandle, WM_KEYDOWN, Ord('s'), $001f0008);
sendmessage(ahandle, WM_KEYUP, Ord('s'), $001f0008);
sendmessage(ahandle, WM_KEYUP, vk_menu, $001f0008);
分类:Object Pascal ( 版主:menxin, cAkk )
来自:kingkong, 时间:2003-1-13 2:09:00, ID:1575712 [显示:小字体 | 大字体]
老问题,最近做了一个自定义词组程序为的是自动向某个输入框发送字符串,其中
的核心函数是取得当前鼠标下的窗口句柄后向该窗口中拥有焦点的输入栏发送中文或
西文字符,在记事本下发送中文或西文均正常,但向IE,Word,Excel等发送
西文正常而中文却出现乱码,打开输入法后发送又正常了,不知是什么问题,
怎么解决?废话少说,不欢迎灌水!请发修改后的源程序,本人调试成功后立
即给分!!!!!!!--------------第一种方法------------
这种方法在中文输入法打开的情况下中西文都正常,
但我不想用这种费事的办法.
------------------------------------
//模拟按键函数
procedure TForm1.SendKeys(sSend:string);
var
i:integer;
Sstr:string;
focushld,windowhld:hwnd;
threadld:dword;
ch: byte;
begin
windowhld:=GetForegroundWindow;
threadld:=GetWindowThreadProcessId(Windowhld,nil);
AttachThreadInput(GetCurrentThreadId,threadld,true);
Focushld:=getfocus;
getcursorpos(p); //查鼠标坐标
Focushld:=WindowFromPoint(p); //返回句柄
AttachThreadInput(GetCurrentThreadId,threadld,false);
if (focushld=0) or
(focushld=Self.Memo1.Handle) or
(focushld=Self.Edit1.Handle) or
(focushld=Self.Edit2.Handle) or
(focushld=SpinEdit1.Handle) then
begin
Exit;
end;
i := 1;
while i <= Length(sSend) do
begin
ch := byte(sSend[i]);
if Windows.IsDBCSLeadByte(ch) then
begin
Inc(i);
SendMessage(focushld, WM_IME_CHAR, MakeWord(byte(sSend[i]), ch), 0);
end
else
begin
SendMessage(focushld, WM_IME_CHAR, word(ch), 0);
end;
Inc(i);
end;
SendMessage(focushld, WM_IME_CHAR, word(13), 0);
end;//定时器定时发送字符
procedure TForm1.Timer1Timer(Sender: TObject);
begin
SendKeys('ABCD1234大话西游');
end;--------------第二种方法------------
这种方法用拷贝-粘贴的方式,在记事本下正常,
但在WORD下毫无反应!
------------------------------------
procedure TGoodDictForm.SendKey();
var
i:integer;
focushld,windowhld:hwnd;
threadld:dword;
ch: byte;
begin
windowhld:=GetForegroundWindow;
threadld:=GetWindowThreadProcessId(Windowhld,nil);
AttachThreadInput(GetCurrentThreadId,threadld,true);
getcursorpos(p); //查鼠标坐标
Focushld:=WindowFromPoint(p); //返回句柄
AttachThreadInput(GetCurrentThreadId,threadld,false);
if (focushld=0) or (focushld=Self.Memo1.Handle) then
begin
Exit;
end;
SendMessage(focushld, WM_Paste, 0, 0);
end;//定时器定时发送字符
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Edit1.SelectAll;
Edit1.CopyToClipboard;
SendKeys();
end; To kingkong
要正確送出中文字符一定要依照微軟的IME機制, 單純用SendMessage
是不能夠的. 你可以把要送出的字符放在剪貼板中, 使用keybd_event
這個API送出 Ctrl 與 V 兩鍵的組合. 就能夠把中英文字串放到Word
等軟件中,
keybd_event(VK_CONTROL, 0, 0, 0);
keybd_event(Ord('v'), 0, 0, 0);
keybd_event(Ord('v'), 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);uses sendkey32
{+ = Shift
^ = Control
% = Alt}
SendKeys('^X',true);
发送 Control+X消息
uses sendkey32
{+ = Shift
^ = Control
% = Alt}
SendKeys('^X',true);
这样的方法,原因已经说过了。