如何用程序替代鼠标和键盘操作(100分)

  • 主题发起人 主题发起人 patriot
  • 开始时间 开始时间
P

patriot

Unregistered / Unconfirmed
GUEST, unregistred user!

如何用程序代替鼠标和键盘操作
我想做一个程序:如何每隔一段时间相当于鼠标左键点击一下屏幕,在每隔一段时间相当于
按下ALT+F4。请各位高手指点。。
 
自动移动鼠标位置和虚拟按键
 
1.鼠标用鼠标事件。
2.键盘用发消息。
 
最好能给点代码提示吗?
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ActnList;

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 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 }
///////End Source

///////Begin Demo
procedure TForm1.Button1Click(Sender: TObject);
begin
SendKey(VK_F4, [ssAlt]);
end;

end.
 
模拟鼠标单击
var
pt: TPoint;
begin
pt:= edit1.ClientToScreen( Point( 4,4 ));
SetCursorPos( pt.x, pt.y );//定位鼠标的位置
mouse_event( MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );
mouse_event( MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );
end;
 
1。鼠标
setcursorpos(坐标X,坐标Y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);// click down
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
2。PostMessage(窗口的句柄,WM_KEYDOWN,键值,转换键);
 
1.虚拟按键用Key_Event
procedure sendpaste;
begin
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0);
keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), 0, 0);
keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), KEYEVENTF_KEYUP, 0);
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0);
end;
2.移动鼠标位置用Mouse_Event
SetCursorPos(50,50);
mouse_event( MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 );
mouse_event( MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );

 
东子,我主页上的那个实例代码都放了一年多了
还在吧,你去看看
模拟鼠标键盘按键的
 
coolbaby 站点上的东西不错,只是有时候转不过去,我贴地址:
http://www.jxit.com.cn/devecom/coolbaby/default.asp
 
谢谢陈总,我去过了,看见了,不过有时候连不上。楼上的 小雨哥 对你的站点评价很高啊
 
羞刹人也,
说来惭愧
工作以后,在寝室,一直无法上宽带
(kick 网通1000遍)
所以,我那破站已是近一年未更新了,
对不起人民大众呀
东子,命苦呀!
 

Similar threads

后退
顶部