Delphi 发送按键

const
ExtendedVKeys : set of byte =
      [VK_Up,VK_Down,VK_Left,VK_Right,VK_Home,VK_End,VK_Prior, {Pg Up} VK_Next, {PgDn}VK_Insert,VK_Delete];
//////////////////////发送按键
procedure SendCardNo(CardNo:String);
 var
  i :integer;
begin
 for i:=1 to length(CardNo) do
 begin
    SendKeyDown(vkKeyScan(CardNo[i]),1, False);
 end;


end;
procedure SendKeysBack(Key:char);
begin

  SendKeyDown(vkKeyScan(Key),1, False);

end;


Procedure SendKeyDown(VKey: Byte;NumTimes : Word;GenUpMsg : Boolean);
 var
   Cnt : Word;
   ScanCode : Byte;
   NumState : Boolean;
   KeyBoardState : TKeyboardState;
  begin
  If (VKey=VK_NUMLOCK) then
    begin
     NumState:=ByteBool(GetKeyState(VK_NUMLOCK) and 1);
     GetKeyBoardState(KeyBoardState);
   If NumState then
     KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] and not 1)
   else
     KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] or 1);
     SetKeyBoardState(KeyBoardState);
     exit;
   end;
  ScanCode:=Lo(MapVirtualKey(VKey,0));
  For Cnt:=1 to NumTimes do
    If (VKey in ExtendedVKeys)then
    begin
      KeyboardEvent( VKey, ScanCode, KEYEVENTF_EXTENDEDKEY);
      If (GenUpMsg ) then
       KeyboardEvent( VKey, ScanCode, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP)
    end
    else
    begin
      KeyboardEvent(VKey, ScanCode, 0) ;
      If (GenUpMsg) then
        KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP);
    End;
 end;
 Procedure KeyboardEvent(VKey, ScanCode : Byte;Flags : Longint);
 var
   KeyboardMsg : TMsg;
   Wait :boolean;
 begin
 wait :=true;
  keybd_event(VKey, ScanCode, Flags,0);
  If (Wait) then While (
      PeekMessage(KeyboardMsg,0,WM_KEYFIRST, WM_KEYLAST, PM_REMOVE)) do
        begin
         TranslateMessage(KeyboardMsg);
         DispatchMessage( KeyboardMsg);
        end;
  end;
procedure SetCardLength(value: integer);
begin
CardLength:=Value;
end;

procedure SetSendKey(value: boolean);
begin
SendKey:=Value;
end;

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lostcity1/archive/2006/03/23/636077.aspx

原文地址:https://www.cnblogs.com/MaxWoods/p/1612247.html