简体   繁体   English

如何使用 delphi 7 将密钥发送到另一个应用程序?

[英]How can I send keys to another application using delphi 7?

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?这里有人知道在 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.我已经为此搜索了大约一个半小时,但我似乎找不到任何东西,我发现的东西是适用于较新版本的 delphi,或者它根本不起作用。 I have tried TTouchKeyboard but that's only for delphi 10 and newer.我试过 TTouchKeyboard 但这仅适用于 delphi 10 及更新版本。

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.我已经用它向一个烦人的没有界面的弹出式 3G 应用程序发送文本,这是一个黑客,因为我们没有任何选择。

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.编辑以允许其他字符(非字母),即移位数字 !"£$ 等。

See keybd_event function.参见keybd_event函数。 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.除非您需要模拟输入,否则将 WM_SETTEXT 发送到编辑框窗口然后将 Enter 作为键盘发送是有意义的。 This will let you avoid dealing with scancodes.这将使您避免处理扫描码。

Use SendKeys() from the unit SNDKEY32.PAS on the Delphi 7 installation CD.使用 Delphi 7 安装 CD 上 SNDKEY32.PAS 单元中的 SendKeys()。 In case you cannot find your CD, look here .如果找不到 CD,请查看此处 Works fine for me (Delphi7 on Windows 7).对我来说很好用(Windows 7 上的 Delphi7)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM