Thema: Delphi Tastatureingabe

Einzelnen Beitrag anzeigen

Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#4

Re: Tastatureingabe

  Alt 13. Nov 2004, 17:15
Zitat:
Dies sollte am besten durch eingabe des textes als string gehn
Hi,

Hier eine kompakte Funktion:

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 }
Thomas
  Mit Zitat antworten Zitat