您的位置:首页 > 编程语言 > C#

[转载]C#模拟键盘鼠标事件-SendKeys

2012-01-12 12:23 666 查看

C#模拟键盘鼠标事件-SendKeys

2007-09-1815:138596人阅读评论(1)收藏举报1.模拟键盘事件
System.Windows.Forms.SendKeys
以下是SendKeys的一些特殊键代码表。
键代码
BACKSPACE{BACKSPACE}、{BS}或{BKSP}
BREAK{BREAK}
CAPSLOCK{CAPSLOCK}
DEL或DELETE{DELETE}或{DEL}
DOWNARROW(下箭头键){DOWN}
END{END}
ENTER{ENTER}或~
ESC{ESC}
HELP{HELP}
HOME{HOME}
INS或INSERT{INSERT}或{INS}
LEFTARROW(左箭头键){LEFT}
NUMLOCK{NUMLOCK}
PAGEDOWN{PGDN}
PAGEUP{PGUP}
PRINTSCREEN{PRTSC}(保留,以备将来使用)
RIGHTARROW(右箭头键){RIGHT}
SCROLLLOCK{SCROLLLOCK}
TAB{TAB}
UPARROW(上箭头键){UP}
F1{F1}
F2{F2}
F3{F3}
F4{F4}
F5{F5}
F6{F6}
F7{F7}
F8{F8}
F9{F9}
F10{F10}
F11{F11}
F12{F12}
F13{F13}
F14{F14}
F15{F15}
F16{F16}
数字键盘加号{ADD}
数字键盘减号{SUBTRACT}
数字键盘乘号{MULTIPLY}
数字键盘除号{DIVIDE}

若要指定与SHIFT、CTRL和ALT键的任意组合一起使用的键,请在这些键代码之前加上以下一个或多个代码:

键代码
SHIFT+(SHIFT="+")
CTRL^(CTRL="^")如果输入
ALT%privatevoidbutton1_Click(objectsender,System.EventArgse)
{//英文输入
this.richTextBox1.Focus();
for(inti=65;i<91;i++)
{
charLetter=(char)i;
SendKeys.Send(Letter.ToString());
System.Threading.Thread.Sleep(100);
SendKeys.Flush();
}
for(inti=97;i<123;i++)
{
charLetter=(char)i;
SendKeys.Send(Letter.ToString());
System.Threading.Thread.Sleep(100);
SendKeys.Flush();
}
}
privatevoidbutton3_Click(objectsender,System.EventArgse)
{//数字输入
this.richTextBox1.Focus();
for(inti=0;i<10;i++)
{
SendKeys.Send(i.ToString());
System.Threading.Thread.Sleep(100);
SendKeys.Flush();
}
}
privatevoidbutton4_Click(objectsender,System.EventArgse)
{//Backspace
this.richTextBox1.Focus();
SendKeys.Send("{Backspace}");
}
privatevoidbutton5_Click(objectsender,System.EventArgse)
{//Home
this.richTextBox1.Focus();
SendKeys.Send("{Home}");
}
privatevoidbutton6_Click(objectsender,System.EventArgse)
{//End
this.richTextBox1.Focus();
SendKeys.Send("{End}");
}
privatevoidbutton7_Click(objectsender,System.EventArgse)
{//Enter
this.richTextBox1.Focus();
SendKeys.Send("{Enter}");
}
privatevoidbutton8_Click(objectsender,System.EventArgse)
{//Delete
this.richTextBox1.Focus();
SendKeys.Send("{Delete}");
}
privatevoidbutton2_Click(objectsender,System.EventArgse)
{//Shift+Home
this.richTextBox1.Focus();
SendKeys.Send("+{Home}");
}
privatevoidbutton9_Click(objectsender,System.EventArgse)
{//Shift+End
this.richTextBox1.Focus();
SendKeys.Send("+{End}");
}

看下方法的说明

.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
publicclassSendKeys:System.Object
System.Windows.Forms的成员
摘要:
提供将键击发送到应用程序的方法。

publicstaticvoidSend(System.Stringkeys)
System.Windows.Forms.SendKeys的成员
摘要:
向活动应用程序发送击键。

publicstaticvoidSleep(System.TimeSpantimeout)
System.Threading.Thread的成员
摘要:
将当前线程阻塞指定的时间。



.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
publicstaticvoidFlush()
System.Windows.Forms.SendKeys的成员
2.模拟鼠标
有时,我们需在我们的程序中模拟鼠标的移动、点击等动作。——比如,一个再现用户操作的宏,或者一个演示操作方法的Demo程序。那么,我们在.Net中如何实现呢?

.Net并没有提供改变鼠标指针位置、模拟点击操作的函数;但是WindowsAPI提供了。其中一个是:

[DllImport("user32.dll")]
staticexternboolSetCursorPos(intX,intY);


该函数可以改变鼠标指针的位置。其中X,Y是相对于屏幕左上角的绝对位置。
另一个函数是:

[DllImport("user32.dll")]
staticexternvoidmouse_event(MouseEventFlagflags,intdx,intdy,uintdata,UIntPtrextraInfo);

这个函数不仅可以设置鼠标指针绝对的位置,而且可以以相对坐标来设置。另外,该函数还可以模拟鼠标左右键点击、鼠标滚轮操作等。其中的MouseEventFlag是一个基于uint类型的枚举,定义如下:

[Flags]
enumMouseEventFlag:uint
{
Move=0x0001,
LeftDown=0x0002,
LeftUp=0x0004,
RightDown=0x0008,
RightUp=0x0010,
MiddleDown=0x0020,
MiddleUp=0x0040,
XDown=0x0080,
XUp=0x0100,
Wheel=0x0800,
VirtualDesk=0x4000,
Absolute=0x8000
}

关于这两个函数的详细说明,可以查看MSDNLibrary或者Windows的PlatformSDK文档。
下面的演示程序(完整版源代码,VS.Net2005/C#)演示了使用上面的函数,控制鼠标移动到任务栏并点击“开始”按钮的方法。
(该程序使用了FindWindowEx等API函数来查找任务栏及开始菜单)

点这里下载
[/code]
postedon2007-08-0722:01Thunderdanky阅读(185)评论(3)编辑收藏所属分类:.NET技术文章


FeedBack:#re:C#模拟键盘鼠标事件2007-08-0722:01|Thunderdanky
[align=left]看一个参考MSDN上的
如何:在代码中模拟鼠标和键盘事件

Windows窗体提供以编程方式模拟鼠标和键盘输入的几个选项。本主题提供这些选项的概述。

模拟鼠标输入
模拟鼠标事件的最佳方法是调用引发要模拟的鼠标事件的OnEventName方法。此选项通常只在自定义控件和窗体中是可能的,因为引发事件的方法受保护,而且不能从控件或窗体外部访问。例如,下面的步骤阐释如何用代码模拟单击鼠标右键的事件。

以编程方式单击鼠标右键
创建一个Button属性设置为System.Windows.Forms.MouseButtons.Right值的MouseEventArgs。

将此MouseEventArgs用作参数调用OnMouseClick方法。

有关自定义控件的更多信息,请参见设计时开发Windows窗体控件。

还有其他模拟鼠标输入的方法。例如,可以通过编程方式设置一个表示通常通过鼠标输入设置的状态的控件属性(如CheckBox控件的Checked属性),或者您可以直接调用附加到要模拟的事件的委托。

模拟键盘输入
虽然您可以通过使用上面讨论的鼠标输入策略来模拟键盘输入,但Windows窗体还提供了用于将键击发送到活动应用程序的SendKeys类。

警告
如果您的应用程序打算用于可以使用各种键盘的国际使用,则使用System.Windows.Forms.SendKeys.Send(System.String)可能产生不可预知的结果,因而应当避免。

注意
SendKeys类已针对.NETFramework3.0进行了更新,能够用于在WindowsVista上运行的应用程序中。WindowsVista增强的安全性(称为用户账户控件或UAC)使以前的实现无法按预期方式工作。

SendKeys类容易出现计时问题,有些开发人员必须解决这个问题。更新的实现仍然容易发生计时问题,但速度略有提高,并且可能要求更改解决方法。SendKeys类首先会尝试使用以前的实现,失败后再使用新的实现。因此,SendKeys类的行为可能因操作系统的不同而有所差异。此外,当SendKeys类使用新的实现时,SendWait方法在消息被发送到另一进程时不会等待消息的处理。

如果您的应用程序依赖于不受操作系统影响的一致性行为,则可通过向app.config文件添加以下应用程序设置,强制SendKeys类使用新的实现。

<appSettings>

<addkey="SendKeys"value="SendInput"/>

</appSettings>

要强制SendKeys类使用以前的实现,请改用值"JournalHook"。

向同一应用程序发送键击
调用SendKeys类的Send或SendWait方法。应用程序的活动控件将接收指定的键击。下面的代码示例使用Send在用户双击窗体的图面时模拟按Enter键。此示例假定一个Form,该窗体具有单个Tab键索引为0的Button控件。

VisualBasic复制代码
'Sendakeytothebuttonwhentheuserdouble-clicksanywhere
'ontheform.
PrivateSubForm1_DoubleClick(ByValsenderAsObject,_
ByValeAsEventArgs)HandlesMe.DoubleClick

'Sendtheenterkeytothebutton,whichraisestheclick
'eventforthebutton.Thisworksbecausethetabstopof
'thebuttonis0.
SendKeys.Send("{ENTER}")
EndSub

C#复制代码
//Sendakeytothebuttonwhentheuserdouble-clicksanywhere
//ontheform.
privatevoidForm1_DoubleClick(objectsender,EventArgse)
{
//Sendtheenterkeytothebutton,whichraisestheclick
//eventforthebutton.Thisworksbecausethetabstopof
//thebuttonis0.
SendKeys.Send("{ENTER}");
}

C++复制代码
//Sendakeytothebuttonwhentheuserdouble-clicksanywhere
//ontheform.
private:
voidForm1_DoubleClick(Object^sender,EventArgs^e)
{
//Sendtheenterkeytothebutton,whichtriggerstheclick
//eventforthebutton.Thisworksbecausethetabstopof
//thebuttonis0.
SendKeys::Send("{ENTER}");
}

向另一个应用程序发送键击
激活将接收键击的应用程序窗口,然后调用Send或SendWait方法。由于没有激活另一个应用程序的托管方法,因此必须使用本机Windows方法强制将焦点放在其他应用程序上。下面的代码示例使用平台调用来调用FindWindow和SetForegroundWindow方法,以激活计算器应用程序窗口,然后调用SendWait向计算器应用程序发出一系列计算。

VisualBasic复制代码
'Getahandletoanapplicationwindow.
DeclareAutoFunctionFindWindowLib"USER32.DLL"(_
ByVallpClassNameAsString,_
ByVallpWindowNameAsString)AsIntPtr

'Activateanapplicationwindow.
DeclareAutoFunctionSetForegroundWindowLib"USER32.DLL"_
(ByValhWndAsIntPtr)AsBoolean

'SendaseriesofkeypressestotheCalculatorapplication.
PrivateSubbutton1_Click(ByValsenderAsObject,_
ByValeAsEventArgs)Handlesbutton1.Click

'GetahandletotheCalculatorapplication.Thewindowclass
'andwindownamewereobtainedusingtheSpy++tool.
DimcalculatorHandleAsIntPtr=FindWindow("SciCalc","Calculator")

'VerifythatCalculatorisarunningprocess.
IfcalculatorHandle=IntPtr.ZeroThen
MsgBox("Calculatorisnotrunning.")
Return
EndIf

'MakeCalculatortheforegroundapplicationandsendit
'asetofcalculations.
SetForegroundWindow(calculatorHandle)
SendKeys.SendWait("111")
SendKeys.SendWait("*")
SendKeys.SendWait("11")
SendKeys.SendWait("=")
EndSub

C#复制代码
//Getahandletoanapplicationwindow.
[DllImport("USER32.DLL")]
publicstaticexternIntPtrFindWindow(stringlpClassName,
stringlpWindowName);

//Activateanapplicationwindow.
[DllImport("USER32.DLL")]
publicstaticexternboolSetForegroundWindow(IntPtrhWnd);

//SendaseriesofkeypressestotheCalculatorapplication.
privatevoidbutton1_Click(objectsender,EventArgse)
{
//GetahandletotheCalculatorapplication.Thewindowclass
//andwindownamewereobtainedusingtheSpy++tool.
IntPtrcalculatorHandle=FindWindow("SciCalc","Calculator");

//VerifythatCalculatorisarunningprocess.
if(calculatorHandle==IntPtr.Zero)
{
MessageBox.Show("Calculatorisnotrunning.");
return;
}

//MakeCalculatortheforegroundapplicationandsendit
//asetofcalculations.
SetForegroundWindow(calculatorHandle);
SendKeys.SendWait("111");
SendKeys.SendWait("*");
SendKeys.SendWait("11");
SendKeys.SendWait("=");
}

C++复制代码
//Getahandletoanapplicationwindow.
public:
[DllImport("USER32.DLL")]
staticIntPtrFindWindow(String^lpClassName,String^lpWindowName);
public:
//Activateanapplicationwindow.
[DllImport("USER32.DLL")]
staticboolSetForegroundWindow(IntPtrhWnd);

//SendaseriesofkeypressestotheCalculatorapplication.
private:
voidbutton1_Click(Object^sender,EventArgs^e)
{
//GetahandletotheCalculatorapplication.Thewindowclass
//andwindownamewereobtainedusingtheSpy++tool.
IntPtrcalculatorHandle=FindWindow("SciCalc","Calculator");

//VerifythatCalculatorisarunningprocess.
if(calculatorHandle==IntPtr::Zero)
{
MessageBox::Show("Calculatorisnotrunning.");
return;
}

//MakeCalculatortheforegroundapplicationandsendit
//asetofcalculations.
SetForegroundWindow(calculatorHandle);
SendKeys::SendWait("111");
SendKeys::SendWait("*");
SendKeys::SendWait("11");
SendKeys::SendWait("=");
}

示例
下面的代码示例是前面代码示例的完整应用。

VisualBasic复制代码
ImportsSystem
ImportsSystem.Runtime.InteropServices
ImportsSystem.Drawing
ImportsSystem.Windows.Forms

NamespaceSimulateKeyPress

ClassForm1
InheritsForm
PrivateWithEventsbutton1AsNewButton()

<STAThread()>_
PublicSharedSubMain()
Application.EnableVisualStyles()
Application.Run(NewForm1())
EndSub

PublicSubNew()
button1.Location=NewPoint(10,10)
button1.TabIndex=0
button1.Text="ClicktoautomateCalculator"
button1.AutoSize=True
Me.Controls.Add(button1)
EndSub

'Getahandletoanapplicationwindow.
DeclareAutoFunctionFindWindowLib"USER32.DLL"(_
ByVallpClassNameAsString,_
ByVallpWindowNameAsString)AsIntPtr

'Activateanapplicationwindow.
DeclareAutoFunctionSetForegroundWindowLib"USER32.DLL"_
(ByValhWndAsIntPtr)AsBoolean

'SendaseriesofkeypressestotheCalculatorapplication.
PrivateSubbutton1_Click(ByValsenderAsObject,_
ByValeAsEventArgs)Handlesbutton1.Click

'GetahandletotheCalculatorapplication.Thewindowclass
'andwindownamewereobtainedusingtheSpy++tool.
DimcalculatorHandleAsIntPtr=FindWindow("SciCalc","Calculator")

'VerifythatCalculatorisarunningprocess.
IfcalculatorHandle=IntPtr.ZeroThen
MsgBox("Calculatorisnotrunning.")
Return
EndIf

'MakeCalculatortheforegroundapplicationandsendit
'asetofcalculations.
SetForegroundWindow(calculatorHandle)
SendKeys.SendWait("111")
SendKeys.SendWait("*")
SendKeys.SendWait("11")
SendKeys.SendWait("=")
EndSub

'Sendakeytothebuttonwhentheuserdouble-clicksanywhere
'ontheform.
PrivateSubForm1_DoubleClick(ByValsenderAsObject,_
ByValeAsEventArgs)HandlesMe.DoubleClick

'Sendtheenterkeytothebutton,whichraisestheclick
'eventforthebutton.Thisworksbecausethetabstopof
'thebuttonis0.
SendKeys.Send("{ENTER}")
EndSub

EndClass
EndNamespace

C#复制代码
usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.Drawing;
usingSystem.Windows.Forms;

namespaceSimulateKeyPress
{
classForm1:Form
{
privateButtonbutton1=newButton();

[STAThread]
publicstaticvoidMain()
{
Application.EnableVisualStyles();
Application.Run(newForm1());
}

publicForm1()
{
button1.Location=newPoint(10,10);
button1.TabIndex=0;
button1.Text="ClicktoautomateCalculator";
button1.AutoSize=true;
button1.Click+=newEventHandler(button1_Click);

this.DoubleClick+=newEventHandler(Form1_DoubleClick);
this.Controls.Add(button1);
}

//Getahandletoanapplicationwindow.
[DllImport("USER32.DLL")]
publicstaticexternIntPtrFindWindow(stringlpClassName,
stringlpWindowName);

//Activateanapplicationwindow.
[DllImport("USER32.DLL")]
publicstaticexternboolSetForegroundWindow(IntPtrhWnd);

//SendaseriesofkeypressestotheCalculatorapplication.
privatevoidbutton1_Click(objectsender,EventArgse)
{
//GetahandletotheCalculatorapplication.Thewindowclass
//andwindownamewereobtainedusingtheSpy++tool.
IntPtrcalculatorHandle=FindWindow("SciCalc","Calculator");

//VerifythatCalculatorisarunningprocess.
if(calculatorHandle==IntPtr.Zero)
{
MessageBox.Show("Calculatorisnotrunning.");
return;
}

//MakeCalculatortheforegroundapplicationandsendit
//asetofcalculations.
SetForegroundWindow(calculatorHandle);
SendKeys.SendWait("111");
SendKeys.SendWait("*");
SendKeys.SendWait("11");
SendKeys.SendWait("=");
}

//Sendakeytothebuttonwhentheuserdouble-clicksanywhere
//ontheform.
privatevoidForm1_DoubleClick(objectsender,EventArgse)
{
//Sendtheenterkeytothebutton,whichraisestheclick
//eventforthebutton.Thisworksbecausethetabstopof
//thebuttonis0.
SendKeys.Send("{ENTER}");
}
}
}

C++复制代码
#using<System.Drawing.dll>
#using<System.Windows.Forms.dll>
#using<System.dll>

usingnamespaceSystem;
usingnamespaceSystem::Runtime::InteropServices;
usingnamespaceSystem::Drawing;
usingnamespaceSystem::Windows::Forms;

namespaceSimulateKeyPress
{

publicrefclassForm1:publicForm
{
public:
Form1()
{
Button^button1=gcnewButton();
button1->Location=Point(10,10);
button1->TabIndex=0;
button1->Text="ClicktoautomateCalculator";
button1->AutoSize=true;
button1->Click+=gcnewEventHandler(this,&Form1::button1_Click);

this->DoubleClick+=gcnewEventHandler(this,
&Form1::Form1_DoubleClick);
this->Controls->Add(button1);
}

//Getahandletoanapplicationwindow.
public:
[DllImport("USER32.DLL")]
staticIntPtrFindWindow(String^lpClassName,String^lpWindowName);
public:
//Activateanapplicationwindow.
[DllImport("USER32.DLL")]
staticboolSetForegroundWindow(IntPtrhWnd);

//SendaseriesofkeypressestotheCalculatorapplication.
private:
voidbutton1_Click(Object^sender,EventArgs^e)
{
//GetahandletotheCalculatorapplication.Thewindowclass
//andwindownamewereobtainedusingtheSpy++tool.
IntPtrcalculatorHandle=FindWindow("SciCalc","Calculator");

//VerifythatCalculatorisarunningprocess.
if(calculatorHandle==IntPtr::Zero)
{
MessageBox::Show("Calculatorisnotrunning.");
return;
}

//MakeCalculatortheforegroundapplicationandsendit
//asetofcalculations.
SetForegroundWindow(calculatorHandle);
SendKeys::SendWait("111");
SendKeys::SendWait("*");
SendKeys::SendWait("11");
SendKeys::SendWait("=");
}

//Sendakeytothebuttonwhentheuserdouble-clicksanywhere
//ontheform.
private:
voidForm1_DoubleClick(Object^sender,EventArgs^e)
{
//Sendtheenterkeytothebutton,whichtriggerstheclick
//eventforthebutton.Thisworksbecausethetabstopof
//thebuttonis0.
SendKeys::Send("{ENTER}");
}
};
}

[STAThread]
intmain()
{
Application::EnableVisualStyles();
Application::Run(gcnewSimulateKeyPress::Form1());
}

[/align]C#模拟键盘鼠标事件
[align=left]一个简单的模拟键盘鼠标操作的类

一个简单的模拟键盘鼠标操作的类,扩充VirtualKeys枚举就可以了,或者直接写!

usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.Text;
classKeyboard
{
constuintKEYEVENTF_EXTENDEDKEY=0x1;
constuintKEYEVENTF_KEYUP=0x2;
[DllImport("user32.dll")]
staticexternshortGetKeyState(intnVirtKey);
[DllImport("user32.dll")]
staticexternvoidkeybd_event(
bytebVk,
bytebScan,
uintdwFlags,
uintdwExtraInfo
);

publicenumVirtualKeys:byte
{
VK_NUMLOCK=0x90,//数字锁定键
VK_SCROLL=0x91,//滚动锁定
VK_CAPITAL=0x14,//大小写锁定
VK_A=62
}

publicstaticboolGetState(VirtualKeysKey)
{
return(GetKeyState((int)Key)==1);
}
publicstaticvoidSetState(VirtualKeysKey,boolState)
{
if(State!=GetState(Key))
{
keybd_event(
(byte)Key,
0x45,
KEYEVENTF_EXTENDEDKEY|0,
0
);
keybd_event(
(byte)Key,
0x45,
KEYEVENTF_EXTENDEDKEY|KEYEVENTF_KEYUP,
0
);
}
}
}

示例:
模拟操作
Keyboard.SetState(
VirtualKeys.VK_CAPITAL,
!Keyboard.GetState(VirtualKeys.VK_CAPITAL)
);
得到键盘状态
Keyboard.GetState(VirtualKeys.VK_CAPITAL)

*十进制值标识符IBM兼容键盘 

--------------------------------------------------------------------------------

1   VK_LBUTTON  鼠标左键
2   VK_RBUTTON  鼠标右键
3   VK_CANCEL   Ctrl+Break(通常不需要处理)
4   VK_MBUTTON  鼠标中键
8   VK_BACK    Backspace
9   VK_TAB    Tab
12   VK_CLEAR   NumLock关闭时的数字键盘5
13   VK_RETURN   Enter(或者另一个)
16   VK_SHIFT   Shift(或者另一个)
17   VK_CONTROL  Ctrl(或者另一个)
18   VK_MENU    Alt(或者另一个)
19   VK_PAUSE   Pause
20   VK_CAPITAL  CapsLock
27   VK_ESCAPE   Esc
32   VK_SPACE   Spacebar
33   VK_PRIOR   PageUp
34   VK_NEXT    PageDown
35   VK_END    End
36   VK_HOME    Home
37   VK_LEFT   左箭头
38   VK_UP    上箭头
39   VK_RIGHT   右箭头
40   VK_DOWN   下箭头
41   VK_SELECT  可选
42   VK_PRINT   可选
43   VK_EXECUTE  可选
44   VK_SNAPSHOT  PrintScreen
45   VK_INSERT   Insert
46   VK_DELETE  Delete
47   VK_HELP   可选
48~57 无     主键盘上的0~9
65~90 无      A~Z
96~105 VK_NUMPAD0~VK_NUMPAD9   NumLock打开时数字键盘上的0~9
106  VK_NULTIPLY        数字键盘上的*
107   VK_ADD          数字键盘上的+
108  VK_SEPARATOR        可选
109  VK_SUBTRACT        数字键盘上的-
110  VK_DECIMAL         数字键盘上的.
111  VK_DIVIDE         数字键盘上的/
112~135VK_F1~VK_F24        功能键F1~F24
144  VK_NUMLOCK         NumLock
145  VK_SCROLL         ScrollLock

*/

突然发现在c#里面原来还有一个System.Windows.Forms.SendKeys

不过这个只能模拟键盘

真正能模拟鼠标操作的代码在这里!找的我好辛苦啊!

函数声明:
privatereadonlyintMOUSEEVENTF_LEFTDOWN=0x2;
privatereadonlyintMOUSEEVENTF_LEFTUP=0x4;
[DllImport("user32")]
publicstaticexternvoidmouse_event(intdwFlags,intdx,intdy,intdwData,intdwExtraInfo);

调用方法:
mouse_event(MOUSEEVENTF_LEFTDOWN,X*65536/1024,Y*65536/768,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,X*65536/1024,Y*65536/768,0,0);
其中X,Y分别是你要点击的点的横坐标和纵坐标
[/align]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: