问题
Ok, so Pretty much i am trying to send keystrokes of a string from and edit box to the active window and the enter key after. does anyone here know a working method of doing this in delphi 7?
I have been searching for about an hour and a half for this now and i cant seem to find anything and the stuff I have found is ether for newer versions of delphi, or it just doesn't work. I have tried TTouchKeyboard but that's only for delphi 10 and newer.
回答1:
I've used this to send text to an annoying popup 3G application with no interface, its a hack be we wern't left with any option.
procedure TForm1.TypeMessage(Msg: string);
var
CapsOn: boolean;
i: integer;
ch: char;
shift: boolean;
key: short;
begin
CapsOn := (GetKeyState( VK_CAPITAL ) and $1) <> 0;
for i:=1 to length(Msg) do
begin
ch := Msg[i];
ch := UpCase(ch);
if ch <> Msg[i] then
begin
if CapsOn then
begin
keybd_event( VK_SHIFT, 0, 0, 0 );
end;
keybd_event( ord(ch), 0, 0, 0 );
keybd_event( ord(ch), 0, KEYEVENTF_KEYUP, 0 );
if CapsOn then
begin
keybd_event( VK_SHIFT, 0, KEYEVENTF_KEYUP, 0 );
end;
end
else
begin
key := VKKeyScan( ch );
// UpperCase
if ((not CapsOn) and (ch>='A') and (ch <= 'Z')) or
((key and $100) > 0) then
begin
keybd_event( VK_SHIFT, 0, 0, 0 );
end;
keybd_event( key, 0, 0, 0 );
keybd_event( key, 0, KEYEVENTF_KEYUP, 0 );
if ((not CapsOn) and (ch>='A') and (ch <= 'Z')) or
((key and $100) > 0) then
begin
keybd_event( VK_SHIFT, 0, KEYEVENTF_KEYUP, 0 );
end;
end;
end;
end;
hope that helps
UPDATE
Edited to allow other characters (non alpha ) ie shifted numerals !"£$ etc.
回答2:
See keybd_event function. You will need to perform translation between chars and keyboard scan codes, but internet is full of information on this.
Unless you need to emulate typing, it makes sense to send WM_SETTEXT to the edit box window and then send Enter as a keyboard. This will let you avoid dealing with scancodes.
回答3:
Use SendKeys() from the unit SNDKEY32.PAS on the Delphi 7 installation CD. In case you cannot find your CD, look here. Works fine for me (Delphi7 on Windows 7).
来源:https://stackoverflow.com/questions/9673442/how-can-i-send-keys-to-another-application-using-delphi-7