CJC Delphi 技巧馆's Archiver

cjc Posted at 2018-9-25 22:15

模拟键盘按键 send keys (cjc,2018.9.26)

模拟键盘按键 send keys
整理by CJC.

// [url]https://community.embarcadero.com/article/technical-articles/149-tools/12933-does-delphi-have-an-equivalent-to-the-visual-basic-sendkeys-function[/url]


procedure SimulateKeyDown(Key : byte);
begin
  keybd_event(Key, 0, 0, 0);
end;

procedure SimulateKeyUp(Key : byte);
begin
  keybd_event(Key, 0, KEYEVENTF_KEYUP, 0);
end;

procedure SimulateKeystroke(Key : byte;
                            extra : DWORD);
begin
  keybd_event(Key,
              extra,
              0,
              0);
  keybd_event(Key,
              extra,
              KEYEVENTF_KEYUP,
              0);
end;

procedure SendKeys(s : string);
var
  i : integer;
  flag : bool;
  w : word;
begin
{Get the state of the caps lock key}
  flag := not GetKeyState(VK_CAPITAL) and 1 = 0;
{If the caps lock key is on then turn it off}
  if flag then
    SimulateKeystroke(VK_CAPITAL, 0);
  for i := 1 to Length(s) do begin
    w := VkKeyScan(s[i]);
   {If there is not an error in the key translation}
    if ((HiByte(w) <> $FF) and
        (LoByte(w) <> $FF)) then begin
     {If the key requires the shift key down - hold it down}
      if HiByte(w) and 1 = 1 then
        SimulateKeyDown(VK_SHIFT);
     {Send the VK_KEY}
      SimulateKeystroke(LoByte(w), 0);
     {If the key required the shift key down - release it}
      if HiByte(w) and 1 = 1 then
        SimulateKeyUp(VK_SHIFT);
    end;
  end;
{if the caps lock key was on at start, turn it back on}
  if flag then
    SimulateKeystroke(VK_CAPITAL, 0);
end;




procedure TForm1.Button2Click(Sender: TObject);
begin
{Capture the entire screen to the clipboard}
{by simulating pressing the PrintScreen key}
  SimulateKeystroke(VK_SNAPSHOT, 0);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
{Capture the active window to the clipboard}
{by simulating pressing the PrintScreen key}
  SimulateKeystroke(VK_SNAPSHOT, 1);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
{Set the focus to a window (edit control) and send it a string}
  Application.ProcessMessages;
  Edit1.SetFocus;
  SendKeys('Delphi Is RAD!');
end;



procedure TForm1.Button5Click(Sender: TObject);
begin
   {Toggle the cap lock}
   SimulateKeyDown(VK_LMENU); // VK_CONTROL
   // SimulateKeystroke(VK_ESCAPE,0); // VK_CAPITAL, 0);      // VK_SNAPSHOT
   SimulateKeystroke(VK_TAB, 0); // VK_CAPITAL, 0);      // VK_SNAPSHOT

   delay(500);
   SimulateKeyUp(VK_LMENU);

    {Toggle the cap lock}
   //  SimulateKeystroke(VK_CAPITAL, 0);
end;



// ********************************************************************
//支持中文版(需Delphi XE以上编译):


// 支持中文 unicode
procedure SendKeys2(const S: String);

type  TArrayInput = array of TInput;
var
  InputEvents: TArrayInput;
  AInput: ^TInput;
  I, J: Integer;
begin
  if S = '' then Exit;
  SetLength(InputEvents, SizeOf(TInput) * (Length(S) * 2));
  AInput := @InputEvents[0];
  try
    J := 0;
    for I := 1 to Length(S) do
    begin
      InputEvents[J].Itype := INPUT_KEYBOARD;
      InputEvents[J].ki.wVk := 0;
      InputEvents[J].ki.wScan := Ord(S[I]);
      InputEvents[J].ki.dwFlags := KEYEVENTF_UNICODE;
      InputEvents[J].ki.time := 0;
      InputEvents[J].ki.dwExtraInfo := 0;
      Inc(J);
      InputEvents[J].Itype := INPUT_KEYBOARD;
      InputEvents[J].ki.wVk := 0;
      InputEvents[J].ki.wScan := Ord(S[I]);
      InputEvents[J].ki.dwFlags := KEYEVENTF_UNICODE or KEYEVENTF_KEYUP;
      InputEvents[J].ki.time := 0;
      InputEvents[J].ki.dwExtraInfo := 0;
      Inc(J);
    end;
    SendInput(J, AInput^, SizeOf(TInput));
  finally
    SetLength(InputEvents,0);
  end;
end;



procedure TForm2.Button1Click(Sender: TObject);
var
  Ahandle: THandle;
begin
  Application.ProcessMessages;
  Edit1.SetFocus;
  SendKeys2('好! ');

   Ahandle := FindWindow(nil, '无标题 - 记事本');
  // SetForegroundWindow(AHandle);
   SendKeys2('好的 Delphi Is RAD! 你们好');

end;

Page: [1]
CJC好玩背单词最新版本 - 是目前最好的背单词软件,游戏好玩,功能强大,词库众多,还有真人语音,免费下载,免费试用 !

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.