--------------第一种方法------------
这种方法在中文输入法打开的情况下中西文都正常,
但我不想用这种费事的办法.
------------------------------------
//模拟按键函数
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;
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.