如何使用delphi 7将密钥发送到另一个应用程序? [英] How can I send keys to another application using delphi 7?

查看:103
本文介绍了如何使用delphi 7将密钥发送到另一个应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以几乎我正在尝试将字符串的击键和编辑框发送到活动窗口,然后再按Enter键.这里有人知道在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的以太,或者它不起作用.我尝试过TTouchKeyboard,但这仅适用于delphi 10及更高版本.

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 delp or it just doesn't work. I have tried TTouchKeyboard but that's only for delphi 10 and newer.

推荐答案

我已经使用它来将文本发送到没有界面的烦人的弹出式3G应用程序,这是一个hack,因为我们别无选择.

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;

希望有帮助

更新

经过修改,允许使用其他字符(非字母),即移位的数字!£$等.

Edited to allow other characters (non alpha ) ie shifted numerals !"£$ etc.

这篇关于如何使用delphi 7将密钥发送到另一个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆