键鼠模拟的妙用
妙用一:登录web邮箱发信
妙用二:外挂或远程控制
妙用三:web 应用的白盒测试程序
妙用四:股票自动盯盘
妙用一:外挂或远程控制
可以预想,外挂和远程控制会用到键鼠模拟功能。尤其是外挂,很多键鼠模拟精灵软件都支持脚本。
妙用二:黑盒测试工具
用录制或者用户编写的脚本,执行的时候会用到键鼠模拟功能。
妙用三:登录web邮箱发信
上述用处已经是众所周知的了,本功能这才是本文的重点。
下面的代码先打开mail.163.com,然后用脚本模拟鼠标和键盘行为,做这样的步骤:
1移动鼠标到用户名的地方按下,模拟键盘打入用户名
2移动鼠标到密码的地方按下,模拟键盘打入密码
3移动鼠标到登录按钮的地方按下,然后等待8秒
4移动鼠标到写信按钮的地方按下,等待1秒钟,让浏览器处理一下
5然后依次类推打入收件人,主体,信件内容等等。
6最后鼠标移动到发送按钮的地方按下,完成全部过程。
脚本代码在这里
#include <WindowsAPI.js>
System.ExecuteScriptFile(System.GetAppPath()+"SystemDB.php","PHP");
function Browser_OnClose()
{
System.Exit();
}
var browser;
var receiver_name;
var receiver_mail;
var account_name;
var account_pwd;
logined=false;
function Browser_OnNavigateComplete2(url)
{
Output.println(url);
if(logined)
return;
//鼠标跳到email位置
System.GotoPos(browser.GetHandle(),580,185);
System.DoMouseDown();
System.DoMouseUp();
System.DoMouseDown();
System.DoMouseUp();
System.SendInputText(account_name);
System.ProcessMessages();
//鼠标跳到密码
System.GotoPos(browser.GetHandle(),580,230);
System.DoMouseDown();
System.DoMouseUp();
System.SendInputText(account_pwd);
Sleep(1220);
System.ProcessMessages();
//按登录按钮
System.GotoPos(browser.GetHandle(),585,360);
System.DoMouseDown();
System.DoMouseUp();
logined=true;
//若干秒后点击写信
setTimeout("点击写信();",10000);
}
function 点击写信()
{
System.GotoPos(browser.GetHandle(),130,105);
System.DoMouseDown();
System.DoMouseUp();
//若干秒后写信内容
setTimeout("写信内容();",1000);
}
function 写信内容()
{
System.SendInputControlKeyDown(0x1B,0x01);//VK_ESCAPE key down
System.SendInputControlKeyUp(0x1B,0x01);//VK_ESCAPE key up
//跳到收件人
System.GotoPos(browser.GetHandle(),330,175);
System.DoMouseDown();
System.DoMouseUp();
receiver=receiver_name+"<"+receiver_mail+">"
System.SendInputText(receiver);
//跳到主题
System.GotoPos(browser.GetHandle(),330,205);
System.DoMouseDown();
System.DoMouseUp();
s=subject.replace("[receiver_name]",receiver_name);
System.SendInputText(s);
//跳到信件内容
System.GotoPos(browser.GetHandle(),270,295);
System.DoMouseDown();
System.DoMouseUp();
c=content.replace("[receiver_name]",receiver_name);
System.SendInputText(c);
//点击发送按钮
System.GotoPos(browser.GetHandle(),280,816);
System.DoMouseDown();
System.DoMouseUp();
//循环下一封,若干秒后点击写信
//setTimeout("点击写信();",8000);
}
Screen=new dobject("TScreen");
browser=new dobject("MiniBrowser");
browser.SetNoMenu();
browser.OnClose=Browser_OnClose;
browser.SetPosition(0,0,940,870);
browser.Show();
browser.OnNavigateComplete2=Browser_OnNavigateComplete2;
Output=new dobject("Output");
Output.SetPosition(0,870,Screen.WorkAreaWidth,80);
Output.Show();
//从文件获得标题
fs=new ActiveXObject("Scripting.FileSystemobject");
fn=System.GetWidgetPath()+"subject.txt";
if(fs.FileExists(fn))
{
ts=fs.OpenTextFile(fn);
subject=ts.ReadLine();
ts.Close();
ts=null;
}
fn=System.GetWidgetPath()+"content.txt";
if(fs.FileExists(fn))
{
ts=fs.OpenTextFile(fn);
content=ts.ReadAll();
ts.Close();
ts=null;
}
fs=null;
function ShowModal_InputAccount()
{
form=new dobject("TForm",null);
form.Caption="163 Web 邮箱鼠键模拟发邮件";
form.Width=350;
form.Height=230;
form.Left=(Screen.WorkAreaWidth-form.Width)/2;
form.Top=(Screen.WorkAreaHeight-form.Height)/2;
label_account=new dobject("TLabel",form);
label_account.Parent=form;
label_account.Left=11;
label_account.Top=10;
label_account.Caption="163邮箱帐号";
edit_account=new dobject("TEdit",form);
edit_account.Parent=form;
edit_account.Left=85;
edit_account.Top=10;
label_pwd=new dobject("TLabel",form);
label_pwd.Parent=form;
label_pwd.Left=11;
label_pwd.Top=40;
label_pwd.Caption="密码:";
edit_pwd=new dobject("TEdit",form);
edit_pwd.Parent=form;
edit_pwd.Left=85;
edit_pwd.Top=40;
label_receiver=new dobject("TLabel",form);
label_receiver.Parent=form;
label_receiver.Left=11;
label_receiver.Top=70;
label_receiver.Caption="接收者姓名";
edit_receiver=new dobject("TEdit",form);
edit_receiver.Parent=form;
edit_receiver.Left=85;
edit_receiver.Top=70;
label_email=new dobject("TLabel",form);
label_email.Parent=form;
label_email.Left=11;
label_email.Top=110;
label_email.Caption="接收者email";
edit_email=new dobject("TEdit",form);
edit_email.Parent=form;
edit_email.Left=85;
edit_email.Top=110;
btn=new dobject("TSpeedButton",form);
btn.OnClick=function(Sender){form.ModalResult=1;};
btn.Parent=form;
btn.Top=140;
btn.Width=65;
btn.Height=26;
btn.Left=22;
btn.Caption="开始";
mr=form.ShowModal();
if(mr==1)
{
receiver_name=edit_receiver.Text;
receiver_mail=edit_email.Text;
account_name=edit_account.Text;
account_pwd=edit_pwd.Text;
browser.SetTopmost();
browser.Navigate2("http://mail.163.com");
Output.SetTopmost();
}else
{
System.Exit();
}
}
ShowModal_InputAccount();
//以上代码需要在 Duceland Widgets Engine 中运行
#include <WindowsAPI.js>
System.ExecuteScriptFile(System.GetAppPath()+"SystemDB.php","PHP");
function Browser_OnClose()
{
System.Exit();
}
var browser;
var receiver_name;
var receiver_mail;
var account_name;
var account_pwd;
logined=false;
function Browser_OnNavigateComplete2(url)
{
Output.println(url);
if(logined)
return;
//鼠标跳到email位置
System.GotoPos(browser.GetHandle(),580,185);
System.DoMouseDown();
System.DoMouseUp();
System.DoMouseDown();
System.DoMouseUp();
System.SendInputText(account_name);
System.ProcessMessages();
//鼠标跳到密码
System.GotoPos(browser.GetHandle(),580,230);
System.DoMouseDown();
System.DoMouseUp();
System.SendInputText(account_pwd);
Sleep(1220);
System.ProcessMessages();
//按登录按钮
System.GotoPos(browser.GetHandle(),585,360);
System.DoMouseDown();
System.DoMouseUp();
logined=true;
//若干秒后点击写信
setTimeout("点击写信();",10000);
}
function 点击写信()
{
System.GotoPos(browser.GetHandle(),130,105);
System.DoMouseDown();
System.DoMouseUp();
//若干秒后写信内容
setTimeout("写信内容();",1000);
}
function 写信内容()
{
System.SendInputControlKeyDown(0x1B,0x01);//VK_ESCAPE key down
System.SendInputControlKeyUp(0x1B,0x01);//VK_ESCAPE key up
//跳到收件人
System.GotoPos(browser.GetHandle(),330,175);
System.DoMouseDown();
System.DoMouseUp();
receiver=receiver_name+"<"+receiver_mail+">"
System.SendInputText(receiver);
//跳到主题
System.GotoPos(browser.GetHandle(),330,205);
System.DoMouseDown();
System.DoMouseUp();
s=subject.replace("[receiver_name]",receiver_name);
System.SendInputText(s);
//跳到信件内容
System.GotoPos(browser.GetHandle(),270,295);
System.DoMouseDown();
System.DoMouseUp();
c=content.replace("[receiver_name]",receiver_name);
System.SendInputText(c);
//点击发送按钮
System.GotoPos(browser.GetHandle(),280,816);
System.DoMouseDown();
System.DoMouseUp();
//循环下一封,若干秒后点击写信
//setTimeout("点击写信();",8000);
}
Screen=new dobject("TScreen");
browser=new dobject("MiniBrowser");
browser.SetNoMenu();
browser.OnClose=Browser_OnClose;
browser.SetPosition(0,0,940,870);
browser.Show();
browser.OnNavigateComplete2=Browser_OnNavigateComplete2;
Output=new dobject("Output");
Output.SetPosition(0,870,Screen.WorkAreaWidth,80);
Output.Show();
//从文件获得标题
fs=new ActiveXObject("Scripting.FileSystemobject");
fn=System.GetWidgetPath()+"subject.txt";
if(fs.FileExists(fn))
{
ts=fs.OpenTextFile(fn);
subject=ts.ReadLine();
ts.Close();
ts=null;
}
fn=System.GetWidgetPath()+"content.txt";
if(fs.FileExists(fn))
{
ts=fs.OpenTextFile(fn);
content=ts.ReadAll();
ts.Close();
ts=null;
}
fs=null;
function ShowModal_InputAccount()
{
form=new dobject("TForm",null);
form.Caption="163 Web 邮箱鼠键模拟发邮件";
form.Width=350;
form.Height=230;
form.Left=(Screen.WorkAreaWidth-form.Width)/2;
form.Top=(Screen.WorkAreaHeight-form.Height)/2;
label_account=new dobject("TLabel",form);
label_account.Parent=form;
label_account.Left=11;
label_account.Top=10;
label_account.Caption="163邮箱帐号";
edit_account=new dobject("TEdit",form);
edit_account.Parent=form;
edit_account.Left=85;
edit_account.Top=10;
label_pwd=new dobject("TLabel",form);
label_pwd.Parent=form;
label_pwd.Left=11;
label_pwd.Top=40;
label_pwd.Caption="密码:";
edit_pwd=new dobject("TEdit",form);
edit_pwd.Parent=form;
edit_pwd.Left=85;
edit_pwd.Top=40;
label_receiver=new dobject("TLabel",form);
label_receiver.Parent=form;
label_receiver.Left=11;
label_receiver.Top=70;
label_receiver.Caption="接收者姓名";
edit_receiver=new dobject("TEdit",form);
edit_receiver.Parent=form;
edit_receiver.Left=85;
edit_receiver.Top=70;
label_email=new dobject("TLabel",form);
label_email.Parent=form;
label_email.Left=11;
label_email.Top=110;
label_email.Caption="接收者email";
edit_email=new dobject("TEdit",form);
edit_email.Parent=form;
edit_email.Left=85;
edit_email.Top=110;
btn=new dobject("TSpeedButton",form);
btn.OnClick=function(Sender){form.ModalResult=1;};
btn.Parent=form;
btn.Top=140;
btn.Width=65;
btn.Height=26;
btn.Left=22;
btn.Caption="开始";
mr=form.ShowModal();
if(mr==1)
{
receiver_name=edit_receiver.Text;
receiver_mail=edit_email.Text;
account_name=edit_account.Text;
account_pwd=edit_pwd.Text;
browser.SetTopmost();
browser.Navigate2("http://mail.163.com");
Output.SetTopmost();
}else
{
System.Exit();
}
}
ShowModal_InputAccount();
//以上代码需要在 Duceland Widgets Engine 中运行
当然,邮件内容从文件读进来的,用的是Scripting.FileSystemobject类。Scripting.FileSystemobject类开发ASP和VBS的人经常用,开发js的朋友可能没有碰过,因为js原本由不得浏览器使用这个类,但是本例用了一个支持js的客户端,所以支持js调用Scripting.FileSystemobject。
妙用四:股票自动盯盘
这个功能也是很有意思的
我们知道,要在比现价高卖出,可以提前委托,对于上班族来说,可以在半夜12点刚过就可以进行委托了,但是如果希望在低于某个百分比(例如1.2%)止损卖出的话,提前委托是不可能的。
如果要自动盯着盘子,发现高于预定于既定价格就进行模拟键鼠的方式卖掉,也可以低于某个价位进行止损。
点开代码
fn="uAutoScan.dfm";
AutoScan=vcl.LoadFormFromDfmFile(fn,scope,share_all);
AutoScan.OnClose=Form_OnClose;
AutoScan.Left=0;
AutoScan.Top=0;
AutoScan.Width=Screen.WorkAreaWidth;
AutoScan.Show();
function FindZXJT()
{
desktop=GetDesktopWindow();
child=null;
while(child=FindWindowEx(desktop,child,null,null))
{
t=GetWindowText(child);
if(t.indexOf("中信建投")==0)
{
return child;
}
}
return null;
}
zxjt_wnd=FindZXJT();
SetWindowPos(zxjt_wnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOREDRAW|SWP_NOMOVE);
MoveWindow(zxjt_wnd,1,AutoScan.Height,AutoScan.Width,Screen.WorkAreaHeight-AutoScan.Height,true);
MoveWindow(zxjt_wnd,0,AutoScan.Height,AutoScan.Width,Screen.WorkAreaHeight-AutoScan.Height,true);
function 拷贝当前价()
{
//点中有当前价的位置
System.GotoPos(null,550,257);
System.DoMouseDown();
System.DoMouseUp();
Sleep(100);
//拷贝-右键弹出式
System.DoMouseDown(true);
System.DoMouseUp(true);
//拷贝-C
System.SendInputControlKeyDown(0x0043,0x2e0001);//'C' key down
System.SendInputControlKeyUp(0x0043,0xc02e0001);//'C' key up
editBuyPrice=dvm.GetSharedObject("zxjt.AutoScan.editBuyPrice");
System.GotoPos(editBuyPrice.Handle,10,6);
System.DoMouseDown(true);
System.DoMouseUp(true);
System.SendInputControlKeyDown(0x0050,0x190001);//'P' key down
System.SendInputControlKeyUp(0x0050,0xc0190001);//'P' key up
}
function TickTask()
{
//获得当前价
System.GotoPos(null,67,328);
System.DoMouseDown();
System.DoMouseUp();
System.setTimeout("拷贝当前价();",2000);
}
function OnTimer()
{
TickTask();
//System.setTimeout("OnTimer();",5000);//做完所有事情,又开始新一轮
}
function CheckMouseTimer()
{
edit=dvm.GetSharedObject("zxjt.AutoScan.editMousePos");
mouse=new Object();
GetCursorPos(mouse);
edit.Text=""+mouse.x+","+mouse.y;
mouse=null;
System.setTimeout("CheckMouseTimer();",3000);
}
function AutoScan_btnStart_OnClick(Sender)
{
System.setTimeout("OnTimer();",5000);
}
AutoScan_btnStart=dvm.GetSharedObject("zxjt.AutoScan.btnStart");
AutoScan_btnStart.OnClick=AutoScan_btnStart_OnClick;
function AutoScan_btnSale_OnClick(Sender)
{
//卖出按钮
System.GotoPos(null,247,187);
System.DoMouseDown();
System.DoMouseUp();
Sleep(1200);
//股票代码
editStockID=dvm.GetSharedObject("zxjt.AutoScan.editStockID");
System.SendInputText(editStockID.Text);
Sleep(1200);
//卖出价格
editSalePrice=dvm.GetSharedObject("zxjt.AutoScan.editSalePrice");
System.SendInputText(editSalePrice.Text);
Sleep(1200);
//点击全部按钮
System.GotoPos(null,365,315);
System.DoMouseDown();
System.DoMouseUp();
Sleep(1200);
//点击下单按钮
System.GotoPos(null,357,370);
System.DoMouseDown();
System.DoMouseUp();
Sleep(1200);
//回车确定
System.SendInputControlKeyDown(0x000d,0x11c0001);//RTURN key down
System.SendInputControlKeyUp(0x000d,0xc11c0001);//RTURN key up
Sleep(1200);
//回车关闭确认对话框
System.SendInputControlKeyDown(0x000d,0x11c0001);//RTURN key down
System.SendInputControlKeyUp(0x000d,0xc11c0001);//RTURN key up
Sleep(1200);
}
AutoScan_btnSale=dvm.GetSharedObject("zxjt.AutoScan.btnSale");
AutoScan_btnSale.OnClick=AutoScan_btnSale_OnClick;
System.setTimeout("CheckMouseTimer();",3000);//启动鼠标跟踪
//以上代码需要在 Duceland Widgets Engine 中运行
fn="uAutoScan.dfm";
AutoScan=vcl.LoadFormFromDfmFile(fn,scope,share_all);
AutoScan.OnClose=Form_OnClose;
AutoScan.Left=0;
AutoScan.Top=0;
AutoScan.Width=Screen.WorkAreaWidth;
AutoScan.Show();
function FindZXJT()
{
desktop=GetDesktopWindow();
child=null;
while(child=FindWindowEx(desktop,child,null,null))
{
t=GetWindowText(child);
if(t.indexOf("中信建投")==0)
{
return child;
}
}
return null;
}
zxjt_wnd=FindZXJT();
SetWindowPos(zxjt_wnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOREDRAW|SWP_NOMOVE);
MoveWindow(zxjt_wnd,1,AutoScan.Height,AutoScan.Width,Screen.WorkAreaHeight-AutoScan.Height,true);
MoveWindow(zxjt_wnd,0,AutoScan.Height,AutoScan.Width,Screen.WorkAreaHeight-AutoScan.Height,true);
function 拷贝当前价()
{
//点中有当前价的位置
System.GotoPos(null,550,257);
System.DoMouseDown();
System.DoMouseUp();
Sleep(100);
//拷贝-右键弹出式
System.DoMouseDown(true);
System.DoMouseUp(true);
//拷贝-C
System.SendInputControlKeyDown(0x0043,0x2e0001);//'C' key down
System.SendInputControlKeyUp(0x0043,0xc02e0001);//'C' key up
editBuyPrice=dvm.GetSharedObject("zxjt.AutoScan.editBuyPrice");
System.GotoPos(editBuyPrice.Handle,10,6);
System.DoMouseDown(true);
System.DoMouseUp(true);
System.SendInputControlKeyDown(0x0050,0x190001);//'P' key down
System.SendInputControlKeyUp(0x0050,0xc0190001);//'P' key up
}
function TickTask()
{
//获得当前价
System.GotoPos(null,67,328);
System.DoMouseDown();
System.DoMouseUp();
System.setTimeout("拷贝当前价();",2000);
}
function OnTimer()
{
TickTask();
//System.setTimeout("OnTimer();",5000);//做完所有事情,又开始新一轮
}
function CheckMouseTimer()
{
edit=dvm.GetSharedObject("zxjt.AutoScan.editMousePos");
mouse=new Object();
GetCursorPos(mouse);
edit.Text=""+mouse.x+","+mouse.y;
mouse=null;
System.setTimeout("CheckMouseTimer();",3000);
}
function AutoScan_btnStart_OnClick(Sender)
{
System.setTimeout("OnTimer();",5000);
}
AutoScan_btnStart=dvm.GetSharedObject("zxjt.AutoScan.btnStart");
AutoScan_btnStart.OnClick=AutoScan_btnStart_OnClick;
function AutoScan_btnSale_OnClick(Sender)
{
//卖出按钮
System.GotoPos(null,247,187);
System.DoMouseDown();
System.DoMouseUp();
Sleep(1200);
//股票代码
editStockID=dvm.GetSharedObject("zxjt.AutoScan.editStockID");
System.SendInputText(editStockID.Text);
Sleep(1200);
//卖出价格
editSalePrice=dvm.GetSharedObject("zxjt.AutoScan.editSalePrice");
System.SendInputText(editSalePrice.Text);
Sleep(1200);
//点击全部按钮
System.GotoPos(null,365,315);
System.DoMouseDown();
System.DoMouseUp();
Sleep(1200);
//点击下单按钮
System.GotoPos(null,357,370);
System.DoMouseDown();
System.DoMouseUp();
Sleep(1200);
//回车确定
System.SendInputControlKeyDown(0x000d,0x11c0001);//RTURN key down
System.SendInputControlKeyUp(0x000d,0xc11c0001);//RTURN key up
Sleep(1200);
//回车关闭确认对话框
System.SendInputControlKeyDown(0x000d,0x11c0001);//RTURN key down
System.SendInputControlKeyUp(0x000d,0xc11c0001);//RTURN key up
Sleep(1200);
}
AutoScan_btnSale=dvm.GetSharedObject("zxjt.AutoScan.btnSale");
AutoScan_btnSale.OnClick=AutoScan_btnSale_OnClick;
System.setTimeout("CheckMouseTimer();",3000);//启动鼠标跟踪
//以上代码需要在 Duceland Widgets Engine 中运行