Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi String an aktives Prog senden (https://www.delphipraxis.net/30147-string-aktives-prog-senden.html)

Tzuxy 21. Sep 2004 13:58


String an aktives Prog senden
 
Also,
mein Programm soll im Hintergrund laufen. Wenn man eine "bestimmte" Taste drückt wird ein if-then
anweisung gestartet. Die soll global eine zeichenkette ausgeben. wie ist das möglich.
also das die if-then anweisung gestartet wird, obwohl es inaktiv ist(im hintergrund) und das man tastaturanschläge global simulieren kann?

mumu 21. Sep 2004 14:01

Re: String an aktives Prog senden
 
tastatur hook, benutze mal die suche im forum Hier im Forum suchentastatur hook

toms 21. Sep 2004 14:06

Re: String an aktives Prog senden
 
Zitat:

und das man tastaturanschläge global simulieren kann?
Z.B mit Hier im Forum suchenkeybd_event()

Mit der Prozedur SendText() kannst du bequem eine Zeichenkette systemweit simulieren:

Code:
procedure SendText(S: string);


Delphi-Quellcode:
procedure PostKeyEx32(key: Word; const shift: TShiftState;
  specialkey: Boolean);
type
  TShiftKeyInfo = record
    shift: Byte;
    vkey: Byte;
  end;
  byteset = set of 0..7;
const
  shiftkeys: array [1..3] of TShiftKeyInfo =
    ((shift: Ord(ssCtrl); vkey: VK_CONTROL),
    (shift: Ord(ssShift); vkey: VK_SHIFT),
    (shift: Ord(ssAlt); vkey: VK_MENU));
var
  flag: DWORD;
  bShift: ByteSet absolute shift;
  i: Integer;
begin
  for i := 1 to 3 do
  begin
    if shiftkeys[i].shift in bShift then
      keybd_event(shiftkeys[i].vkey,
        MapVirtualKey(shiftkeys[i].vkey, 0),
        0, 0);
  end; { For }
  if specialkey then
    flag := KEYEVENTF_EXTENDEDKEY
  else
    flag := 0;

  keybd_event(key, MapvirtualKey(key, 0), flag, 0);
  flag := flag or KEYEVENTF_KEYUP;
  keybd_event(key, MapvirtualKey(key, 0), flag, 0);

  for i := 3 downto 1 do
  begin
    if shiftkeys[i].shift in bShift then
      keybd_event(shiftkeys[i].vkey,
        MapVirtualKey(shiftkeys[i].vkey, 0),
        KEYEVENTF_KEYUP, 0);
  end; { For }
end; { PostKeyEx32 }

procedure SendText(S: string);

  procedure SendRawCharacter(ch: Char);
  var
    i: Integer;
    numStr: string;
  begin
    numStr := Format('%4.4d', [Ord(ch)]);
    keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0),
      0, 0);
    for i := 1 to Length(numStr) do
      PostKeyEx32(VK_NUMPAD0 + Ord(numstr[i]) - Ord('0'), [], False);
    keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0),
      KEYEVENTF_KEYUP, 0);
  end;
 
var
  flags: TShiftState;
  vcode: Word;
  ret: Word;
  i, n: Integer;
  mask: Word;
begin { SendText }
  for i := 1 to Length(S) do
  begin
    ret := VkKeyScan(S[i]);
    if ret = $FFFF then
      SendRawCharacter(S[i])
    else
    begin
      vcode := Lobyte(ret);
      flags := [];
      mask := $100;
      for n := 1 to 3 do
      begin
        if (ret and mask) <> 0 then
        begin
          case mask of
            $100: Include(flags, ssShift);
            $200: Include(flags, ssCtrl);
            $400: Include(flags, ssAlt);
          end; { Case }
        end; { If }
        mask := mask shl 1;
      end; { For }
      PostKeyEx32(vcode, flags, False);
    end; { Else }
  end; { For }
end; { SendText }


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:13 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz