模拟按钮(50分)

  • 主题发起人 主题发起人 D影子D
  • 开始时间 开始时间
D

D影子D

Unregistered / Unconfirmed
GUEST, unregistred user!
我想模拟按下某键,如a键
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure SendKey(const mKey: Word;
mShiftState: TShiftState;
mCount: Integer = 1);
{ 模拟系统按键;mCount指定按键次数 }
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 mCountdo
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 }
///////End Source
///////begin
Demo
procedure TForm1.Button1Click(Sender: TObject);
begin
SendKey(VK_F4, [ssAlt]);
end;
///////End Demo
end.
 
我要的是vc代码啊
 
//这个过程模拟在记事本中写一个"Hello world"
void CMessageTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
HWND wnd;
CString s;
wnd = ::FindWindow("notepad", NULL);
if(wnd!=0)
{
wnd = FindWindowEx(wnd,0,"Edit",NULL);
s = "Hello world.";
for(int i=0 i<s.GetLength();
i++)
{
::SendMessage(wnd, WM_CHAR, WORD(s), 0);
}
// 模拟返回键
::PostMessage(wnd, WM_KEYDOWN , VK_RETURN, 0);
// 模拟空格
::PostMessage(wnd, WM_KEYDOWN , VK_SPACE, 0);
}
}
//这个过程模拟按计算器中的第一个按钮
void CMessageTestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
HWND wnd;
wnd = ::FindWindow("SciCalc", NULL);
if(wnd!=0)
{
wnd = FindWindowEx(wnd,0,"Button",NULL);
// 模拟Mousedo
wn
::PostMessage(wnd, WM_LBUTTONDOWN , 0, 0);
// 模拟Mouse Up
::PostMessage(wnd, WM_LBUTTONUP , 0, 0);
}
}
 
同意zw84611的,我測試通過了,多謝!
 
還有問題,怎樣模擬按下組合鍵如CTRL+A
 
//这个过程模拟在记事本中按下Ctrl+V,既实现粘贴
void CMessageTestDlg::OnButton3()
{
// TODO: Add your control notification handler code here
HWND wnd;
CString s;
wnd = ::FindWindow("notepad", NULL);
if(wnd!=0)
{
DWORD dwVK, dwScan;
::SetForegroundWindow(wnd);
dwVK = VkKeyScan('v') &amp;
0xff;
char szOemchar[2];
// Get the OEM character - preinitialize the buffer
CharToOem( "v", szOemchar);
// Get the scan code for this key
dwScan = OemKeyScan(szOemchar[0]) &amp;
0xff;
// simulate a crtl-v
keybd_event((BYTE) VK_CONTROL, (BYTE) 0x01, 0, 0);
keybd_event((BYTE) dwVK, (BYTE) dwScan, 0, 0);
keybd_event((BYTE) dwVK, (BYTE) dwScan, KEYEVENTF_KEYUP, 0);
keybd_event((BYTE) VK_CONTROL, (BYTE) 0x01, KEYEVENTF_KEYUP, 0);

}

}
 
说的都很详细,
 
謝謝,我想給zw84611、老人家加上50(雖然舍不得,240個小時的收入啊)
請到:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1428727
領我的一點心意,別嫌少哦!
 
接受答案了.
 
后退
顶部