我来了!
并送模拟操作系统按键函数const
ExtendedKeys: set of Byte = [VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT, VK_HOME,
VK_END, VK_PRIOR, VK_NEXT, VK_INSERT, VK_DELETE];procedure ZsKeyboardEvent(mKey, mScanCode: Byte; mFlags: Longint);
{ 向系统发送按键事件 }
var
vKeyboardMsg: TMsg;
begin
keybd_event(mKey, mScanCode, mFlags, 0);
while PeekMessage(vKeyboardMsg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE) do
begin
TranslateMessage(vKeyboardMsg);
DispatchMessage(vKeyboardMsg);
end;
end; { ZsKeyboardEvent }procedure ZsSendKeyDown(mKey: Word; mGenUpMsg: Boolean); { 模拟键盘按下 }
var
vScanCode: Byte;
vNumState: Boolean;
vKeyBoardState: TKeyboardState;
begin
if (mKey = VK_NUMLOCK) then begin
vNumState := ByteBool(GetKeyState(VK_NUMLOCK) and 1);
GetKeyBoardState(vKeyBoardState);
if vNumState then
vKeyBoardState[VK_NUMLOCK] := (vKeyBoardState[VK_NUMLOCK] and not 1)
else vKeyBoardState[VK_NUMLOCK] := (vKeyBoardState[VK_NUMLOCK] or 1);
SetKeyBoardState(vKeyBoardState);
Exit;
end; vScanCode := Lo(MapVirtualKey(mKey, 0));
if (mKey in ExtendedKeys) then begin
ZsKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY);
if mGenUpMsg then
ZsKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP)
end else begin
ZsKeyboardEvent(mKey, vScanCode, 0);
if mGenUpMsg then ZsKeyboardEvent(mKey, vScanCode, KEYEVENTF_KEYUP);
end;
end; { ZsSendKeyDown }procedure ZsSendKeyUp(mKey: Word); { 模拟放开键盘 }
var
vScanCode: Byte;
begin
vScanCode := Lo(MapVirtualKey(mKey, 0));
if mKey in ExtendedKeys then
ZsKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP)
else ZsKeyboardEvent(mKey, vScanCode, KEYEVENTF_KEYUP);
end; { ZsSendKeyUp }procedure ZsSendKey(mKey: Word; mShiftState: TShiftState); { 模拟按键 }
begin
if ssShift in mShiftState then ZsSendKeyDown(VK_SHIFT, False);
if ssCtrl in mShiftState then ZsSendKeyDown(VK_CONTROL, False);
if ssAlt in mShiftState then ZsSendKeyDown(VK_MENU, False);
ZsSendKeyDown(mKey, True);
if ssShift in mShiftState then ZsSendKeyUp(VK_SHIFT);
if ssCtrl in mShiftState then ZsSendKeyUp(VK_CONTROL);
if ssAlt in mShiftState then ZsSendKeyUp(VK_MENU);
end; { ZsSendKeyUp }procedure ZsSendKeys(mKeyList: string); { 模拟一系列按键 }
var
I: Integer;
vShortCut: TShortCut;
vKey: Word;
vShiftState: TShiftState;
begin
with TStringList.Create do try
Text := mKeyList;
for I := 0 to Pred(Count) do begin
vShortCut := TextToShortCut(Strings[I]);
if vShortCut <> 0 then begin
ShortCutToKey(vShortCut, vKey, vShiftState);
ZsSendKey(vKey, vShiftState)
end;
end;
finally
Free;
end;
end; { ZsSendKeys }
并送模拟操作系统按键函数const
ExtendedKeys: set of Byte = [VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT, VK_HOME,
VK_END, VK_PRIOR, VK_NEXT, VK_INSERT, VK_DELETE];procedure ZsKeyboardEvent(mKey, mScanCode: Byte; mFlags: Longint);
{ 向系统发送按键事件 }
var
vKeyboardMsg: TMsg;
begin
keybd_event(mKey, mScanCode, mFlags, 0);
while PeekMessage(vKeyboardMsg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE) do
begin
TranslateMessage(vKeyboardMsg);
DispatchMessage(vKeyboardMsg);
end;
end; { ZsKeyboardEvent }procedure ZsSendKeyDown(mKey: Word; mGenUpMsg: Boolean); { 模拟键盘按下 }
var
vScanCode: Byte;
vNumState: Boolean;
vKeyBoardState: TKeyboardState;
begin
if (mKey = VK_NUMLOCK) then begin
vNumState := ByteBool(GetKeyState(VK_NUMLOCK) and 1);
GetKeyBoardState(vKeyBoardState);
if vNumState then
vKeyBoardState[VK_NUMLOCK] := (vKeyBoardState[VK_NUMLOCK] and not 1)
else vKeyBoardState[VK_NUMLOCK] := (vKeyBoardState[VK_NUMLOCK] or 1);
SetKeyBoardState(vKeyBoardState);
Exit;
end; vScanCode := Lo(MapVirtualKey(mKey, 0));
if (mKey in ExtendedKeys) then begin
ZsKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY);
if mGenUpMsg then
ZsKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP)
end else begin
ZsKeyboardEvent(mKey, vScanCode, 0);
if mGenUpMsg then ZsKeyboardEvent(mKey, vScanCode, KEYEVENTF_KEYUP);
end;
end; { ZsSendKeyDown }procedure ZsSendKeyUp(mKey: Word); { 模拟放开键盘 }
var
vScanCode: Byte;
begin
vScanCode := Lo(MapVirtualKey(mKey, 0));
if mKey in ExtendedKeys then
ZsKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP)
else ZsKeyboardEvent(mKey, vScanCode, KEYEVENTF_KEYUP);
end; { ZsSendKeyUp }procedure ZsSendKey(mKey: Word; mShiftState: TShiftState); { 模拟按键 }
begin
if ssShift in mShiftState then ZsSendKeyDown(VK_SHIFT, False);
if ssCtrl in mShiftState then ZsSendKeyDown(VK_CONTROL, False);
if ssAlt in mShiftState then ZsSendKeyDown(VK_MENU, False);
ZsSendKeyDown(mKey, True);
if ssShift in mShiftState then ZsSendKeyUp(VK_SHIFT);
if ssCtrl in mShiftState then ZsSendKeyUp(VK_CONTROL);
if ssAlt in mShiftState then ZsSendKeyUp(VK_MENU);
end; { ZsSendKeyUp }procedure ZsSendKeys(mKeyList: string); { 模拟一系列按键 }
var
I: Integer;
vShortCut: TShortCut;
vKey: Word;
vShiftState: TShiftState;
begin
with TStringList.Create do try
Text := mKeyList;
for I := 0 to Pred(Count) do begin
vShortCut := TextToShortCut(Strings[I]);
if vShortCut <> 0 then begin
ShortCutToKey(vShortCut, vKey, vShiftState);
ZsSendKey(vKey, vShiftState)
end;
end;
finally
Free;
end;
end; { ZsSendKeys }
begin
ZsSendKey(VK_F4, [ssAlt]); //你该知道它要做什么吧!
end;