Delphi 发送按键

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:14   1212   0

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;

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP