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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值