C#模拟键盘鼠标事件SendKeys.docx

上传人:b****0 文档编号:18375552 上传时间:2023-08-16 格式:DOCX 页数:22 大小:23.25KB
下载 相关 举报
C#模拟键盘鼠标事件SendKeys.docx_第1页
第1页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第2页
第2页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第3页
第3页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第4页
第4页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第5页
第5页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第6页
第6页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第7页
第7页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第8页
第8页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第9页
第9页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第10页
第10页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第11页
第11页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第12页
第12页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第13页
第13页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第14页
第14页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第15页
第15页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第16页
第16页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第17页
第17页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第18页
第18页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第19页
第19页 / 共22页
C#模拟键盘鼠标事件SendKeys.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C#模拟键盘鼠标事件SendKeys.docx

《C#模拟键盘鼠标事件SendKeys.docx》由会员分享,可在线阅读,更多相关《C#模拟键盘鼠标事件SendKeys.docx(22页珍藏版)》请在冰点文库上搜索。

C#模拟键盘鼠标事件SendKeys.docx

C#模拟键盘鼠标事件SendKeys

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

2007-09-1815:

137689人阅读评论

(1)收藏举报

1.模拟键盘事件

System.Windows.Forms.SendKeys

以下是 SendKeys 的一些特殊键代码表。

 

 键 代码  

 BACKSPACE {BACKSPACE}、{BS} 或 {BKSP}  

 BREAK {BREAK}  

 CAPS LOCK {CAPSLOCK}  

 DEL 或 DELETE {DELETE} 或 {DEL}  

 DOWN ARROW(下箭头键) {DOWN}  

 END {END}  

 ENTER {ENTER} 或 ~  

 ESC {ESC}  

 HELP {HELP}  

 HOME {HOME}  

 INS 或 INSERT {INSERT} 或 {INS}  

 LEFT ARROW(左箭头键) {LEFT}  

 NUM LOCK {NUMLOCK}  

 PAGE DOWN {PGDN}  

 PAGE UP {PGUP}  

 PRINT SCREEN {PRTSC}(保留,以备将来使用)  

 RIGHT ARROW(右箭头键) {RIGHT}  

 SCROLL LOCK {SCROLLLOCK}  

 TAB {TAB}  

 UP ARROW(上箭头键) {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}");

}

 

 看下方法的说明

publicclassSendKeys:

System.Object

System.Windows.Forms的成员

 

摘要:

提供将键击发送到应用程序的方法。

 

publicstaticvoidSend(System.Stringkeys)

System.Windows.Forms.SendKeys的成员

 

摘要:

向活动应用程序发送击键。

 

publicstaticvoidSleep(System.TimeSpantimeout)

System.Threading.Thread的成员

 

摘要:

将当前线程阻塞指定的时间。

 

publicstaticvoidFlush()

System.Windows.Forms.SendKeys的成员

 

2.模拟鼠标

有时,我们需在我们的程序中模拟鼠标的移动、点击等动作。

——比如,一个再现用户操作的宏,或者一个演示操作方法的Demo程序。

那么,我们在.Net中如何实现呢?

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

其中一个是:

        [DllImport("user32.dll")]

        static extern bool SetCursorPos(int X, int Y);  

该函数可以改变鼠标指针的位置。

其中X,Y是相对于屏幕左上角的绝对位置。

另一个函数是:

        [DllImport("user32.dll")]

        static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

这个函数不仅可以设置鼠标指针绝对的位置,而且可以以相对坐标来设置。

另外,该函数还可以模拟鼠标左右键点击、鼠标滚轮操作等。

其中的MouseEventFlag是一个基于uint类型的枚举,定义如下:

        [Flags]

        enum MouseEventFlag :

 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函数来查找任务栏及开始菜单)

点这里下载

postedon2007-08-0722:

01Thunderdanky阅读(185)评论(3) 编辑 收藏所属分类:

.NET技术文章

FeedBack:

# re:

C#模拟键盘鼠标事件

2007-08-0722:

01|Thunderdanky

看一个参考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类使用新的实现。

要强制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

//andwindownamewereobtainedusingthe

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2