点击按钮发送键盘虚拟值
点击按钮向一个位置的窗体发送键盘虚拟值!~
如按下Ctrl+T 如何用代码表示?
谢谢,如果有代码另外多加200!
[解决办法]
- Delphi(Pascal) code
var TheWindow : HWND; begin TheWindow := FindWindow(nil, '计算器'); if TheWindow <> 0 then PostMessage(TheWindow,WM_CLOSE,0,0); end; procedure EndProcess(AExeName: string); var lppe: TProcessEntry32; found : boolean; Hand : THandle; begin Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0); found := Process32First(Hand,lppe); while found do begin if UpperCase(StrPas(lppe.szExeFile)) = UpperCase(AExeName) then begin TerminateProcess(OpenProcess(PROCESS_TERMINATE, true, lppe.th32ProcessID), 0); Exit; end else found := Process32Next(Hand,lppe); end; end; const PROCESS_TERMINATE = $0001; var ProcessHandle : THandle; ProcessID: Integer; TheWindow : HWND; begin TheWindow := FindWindow(nil, '计算器'); GetWindowThreadProcessID(TheWindow, @ProcessID); ProcessHandle := OpenProcess(PROCESS_TERMINATE, FALSE, ProcessId); TerminateProcess(ProcessHandle,4); end;
[解决办法]
- Delphi(Pascal) code
procedure SendKey(const mKey: Word; mShiftState: TShiftState; mCount: Integer = 1); overload; const cExtended: set of Byte = [VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT, VK_HOME, VK_END, VK_PRIOR, VK_NEXT, VK_INSERT, VK_DELETE]; procedure pKeyboardEvent(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; { pKeyboardEvent } procedure pSendKeyDown(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 cExtended) then begin pKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY); if mGenUpMsg then pKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP) end else begin pKeyboardEvent(mKey, vScanCode, 0); if mGenUpMsg then pKeyboardEvent(mKey, vScanCode, KEYEVENTF_KEYUP); end; end; { pSendKeyDown } procedure pSendKeyUp(mKey: Word); var vScanCode: Byte; begin vScanCode := Lo(MapVirtualKey(mKey, 0)); if mKey in cExtended then pKeyboardEvent(mKey, vScanCode, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP) else pKeyboardEvent(mKey, vScanCode, KEYEVENTF_KEYUP); end; { pSendKeyUp } var I: Integer; begin for I := 1 to mCount do begin if ssShift in mShiftState then pSendKeyDown(VK_SHIFT, False); if ssCtrl in mShiftState then pSendKeyDown(VK_CONTROL, False); if ssAlt in mShiftState then pSendKeyDown(VK_MENU, False); pSendKeyDown(mKey, True); if ssShift in mShiftState then pSendKeyUp(VK_SHIFT); if ssCtrl in mShiftState then pSendKeyUp(VK_CONTROL); if ssAlt in mShiftState then pSendKeyUp(VK_MENU); end; end; { SendKey } //demo SendKey(VK_DOWN, [ssAlt]);