摘要:熟悉Windows Script Host的朋友一定使用过SendKeys方法。比如说下面的一段脚本就相当于用户通过键盘输入Alt+F4:
Dim wshShellSet wshShell = WScript.CreateObject("WScript.Shell")wshShell.SendKeys("%{F4}")
在.NET框架之中,可以使用静态方法System.Windows.Forms.SendKeys.Send来做到这一点:System.Windows.Forms.SendKeys.Send("%{F4}");
这个功能在Avalon中则可以使用其Automation框架下的Input.SendKeyboardInput静态方法来实现。
与SendKeys相比,SendKeyboardInput 更从细节出发模拟了用户使用键盘的过程。每一次击键,都有key press(按下键)和key release(释放键)两个不同的行为。SendKeyboardInput 的第二个参数用bool值作了区分。
SendKeyboardInput 的第一个参数指定键盘上的具体的一个key,其类型是枚举System.Windows.Input.Key(注意名称空间没有Automation)。可以看到这涵括了大量的key,比如:LWin, RWin, F23, F24, VolumnUp, VolumnDown, LaunchMail, Zoom. (你的键盘上有所有这些key么?)
为了实现同样的Alt+F4的效果,我们必须
按下Alt
按下F4
释放F4
释放Alt
以下是代码:
Input.SendKeyboardInput(System.Windows.Input.Key.LMenu, true);
Input.SendKeyboardInput(System.Windows.Input.Key.F4, true);
Input.SendKeyboardInput(System.Windows.Input.Key.F4, false);
Input.SendKeyboardInput(System.Windows.Input.Key.LMenu, false);
...[
阅读全文]