自动化电脑键盘功能基础知识各键的功能

现在的位置:
使用UI Automation实现自动化测试&# (模拟键盘输入数据在自动化测试中的应用)(转载)
7.1 模拟键盘输入
7.1.1 Introduction
此部分我们通过System.Windows.Forms.SendKeys类中的Send方法来模拟通过键盘输入数据到相应的控件中。
7.1.2 SendKeys Class Introduction(MSDN)
使用 SendKeys 将键击和组合键击发送到活动应用程序。此类无法实例化。若要发送一个键击给某个类并立即继续程序流,请使用 。若要等待键击启动的任何进程,请使用 。
每个键都由一个或多个字符表示。若要指定单个键盘字符,请使用该字符本身。例如,若要表示字母 A,请将字符串“A”传递给方法。若要表示多个字符,请将各个附加字符追加到它之前的字符的后面。若要表示字母 A、B 和 C,请将参数指定为“ABC”。
加号 (+)、插入符号 (^)、百分号 (%)、波浪号 (~) 以及圆括号 ( ) 对 SendKeys 具有特殊含义。若要指定这些字符中的某个字符,请将其放在大括号 ({}) 内。例如,若要指定加号,请使用“{+}”。若要指定大括号字符,请使用“{{}”和“{}}”。中括号 ([ ]) 对SendKeys 没有特殊含义,但必须将它们放在大括号内。在其他应用程序中,中括号具有特殊含义,此含义可能会在发生动态数据交换 (DDE) 时起重要作用。
7.1.3 SendKeys Method(MSDN)
SendKeys有两个重要的方法:
Send方法:向活动应用程序发送击键。
SendWait方法:向活动应用程序发送给定的键,然后等待消息被处理。
有关SendKeys的参考:
英文站点:
中文站点:
下面我们通过一个实例来演示通过SendKeys类来模拟键盘操作进行数据输入和快捷菜单的打开:
using System.T
using System.D
using System.T
using System.Windows.A
using System.Runtime.InteropS
using System.W
using System.Windows.F
namespace UIATest
class Program
static void Main(string[] args)
Process process = Process.Start(@"E:\WorkBook\ATP\WpfApp\bin\Debug\WpfApp.exe");
int processId = process.Id;
AutomationElement element = FindElementById(processId, "textBox1");
SendKeys sendkeys = new SendKeys();
sendkeys.Sendkeys(element, "Sending keys to input data");
Console.WriteLine(sendkeys.ToString());
sendkeys.Sendkeys(element, sendkeys.ContextMenu);
Console.WriteLine(sendkeys.ToString());
Console.WriteLine("Test finised.");
/// &summary&
/// Get the automation elemention of current form.
/// &/summary&
/// &param name="processId"&Process Id&/param&
/// &returns&Target element&/returns&
public static AutomationElement FindWindowByProcessId(int processId)
AutomationElement targetWindow =
int count = 0;
Process p = Process.GetProcessById(processId);
targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
return targetW
catch (Exception ex)
StringBuilder sb = new StringBuilder();
string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
if (count & 5)
throw new InvalidProgramException(message, ex);
return FindWindowByProcessId(processId);
/// &summary&
/// Get the automation element by automation Id.
/// &/summary&
/// &param name="windowName"&Window name&/param&
/// &param name="automationId"&Control automation Id&/param&
/// &returns&Automatin element searched by automation Id&/returns&
public static AutomationElement FindElementById(int processId, string automationId)
AutomationElement aeForm = FindWindowByProcessId(processId);
AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
return tarFindE
我们自定义一个SendKeys类来对已有的SendKeys类进行优化,代码如下:
using System.Windows.A
using System.T
using System.T
using System.Windows.F
namespace UIATest
public class SendKeys
StringBuilder builder = new StringBuilder();
public string Alt = "%";
public string ContextMenu = "+{F10}";
public string Ctrl = "^";
public string Shift = "+";
public string Enter = "{Enter}";
public string Delete = "{Del}";
public string Save = "^S";
public string SaveAll = "^+S";
public string Copy = "^C";
public string Cut = "^X";
public string Paste = "^V";
public string Undo = "^Z";
public string Redo = "^Y";
public string Print = "^P";
public string Help = "{F1}";
public string New = "^N";
public string[] Keys{ }
public void Sendkeys(AutomationElement element, string[] keys)
this.Keys =
element.SetFocus();
catch (Exception exception)
throw new Exception("Cannot set focus to this element.", exception);
string myKeys = "";
foreach (string str2 in this.Keys)
myKeys = myKeys + str2;
Thread.Sleep(200);
if ((this.ContainsUnescapedKey(myKeys, '^') || this.ContainsUnescapedKey(myKeys, '%')) ||this.ContainsUnescapedKey(myKeys, '+'))
myKeys = myKeys.ToLower();
System.Windows.Forms.SendKeys.SendWait(myKeys);
Thread.Sleep(0x3e8);
public void Sendkeys(AutomationElement element, string myKeys)
this.Keys = new string[1];
this.Keys[0] = myK
element.SetFocus();
catch (Exception exception)
throw new Exception("Cannot set focus to this element.", exception);
Thread.Sleep(200);
if ((this.ContainsUnescapedKey(myKeys, '^') || this.ContainsUnescapedKey(myKeys, '%')) ||this.ContainsUnescapedKey(myKeys, '+'))
myKeys = myKeys.ToLower();
System.Windows.Forms.SendKeys.SendWait(myKeys);
Thread.Sleep(0x3e8);
private bool ContainsUnescapedKey(string keys, char key)
for (int i = 0; i & keys.L i++)
if (keys[i] == key)
if ((i == 0) || (i == (keys.Length - 1)))
if ((keys[i - 1] != '{') || (keys[i + 1] != '}'))
private string KeysToString(string[] keys)
if (keys != null)
for (int i = 0; i & keys.L i++)
string str = keys[i];
if (str == null)
builder.Append(keys[i]);
int length = keys.Length - 1;
switch (str)
builder.Append("Ctrl");
IsEquals(i, length, builder);
case "+{F10}":
builder.Append("Open Context Menu");
IsEquals(i, length, builder);
builder.Append("Alt");
IsEquals(i, length, builder);
builder.Append("Shift");
IsEquals(i, length, builder);
case "^S":
builder.Append("Save");
IsEquals(i, length, builder);
case "^X":
builder.Append("Cut");
IsEquals(i, length, builder);
case "^C":
builder.Append("Copy");
IsEquals(i, length, builder);
case "^V":
builder.Append("Paste");
IsEquals(i, length, builder);
case "^+S":
builder.Append("Save All");
IsEquals(i, length, builder);
case "^P":
builder.Append("Print");
IsEquals(i, length, builder);
case "^Z":
builder.Append("Undo");
IsEquals(i, length, builder);
case "^Y":
builder.Append("Redo");
IsEquals(i, length, builder);
case "^N":
builder.Append("New");
IsEquals(i, length, builder);
builder.Append(keys[i]);
IsEquals(i, length, builder);
return builder.ToString();
void IsEquals(int i, int length, StringBuilder builder)
if(i&length)
builder.Append("+");
#region Public Method
public override string ToString()
return string.Format("Sendkeys to input data or operator with keys = '{0}'",
this.KeysToString(Keys));
#endregion
如下代码为对应的WPF窗体XAML代码:
&Window x:Class="WpfApp.SendKeysInputData"
xmlns="/winfx/2006/xaml/presentation"
xmlns:x="/winfx/2006/xaml"
Title="SendKeysInputData" Height="250" Width="367"&
&TextBox Margin="28,80,41,101" Name="textBox1"&
&TextBox.ContextMenu&
&ContextMenu&
&MenuItem Header="Open" /&
&MenuItem Header="Save" /&
&MenuItem Header="New" /&
&MenuItem Header="Save As" /&
&MenuItem Header="Load" /&
&MenuItem Header="ReLoad" /&
&/ContextMenu&
&/TextBox.ContextMenu&
&/TextBox&
7.1.4 Summary
本小节首先简单介绍了如何在.NET通过调用Win32 API来模拟键盘的操作,进而通过实例演示了模拟鼠标与模拟键盘在基于UI Automation的自动化测试中应用。
【上篇】【下篇】
您可能还会对这些文章感兴趣!
日志:456篇
评论:98条
分类:30个
网站运行:2941天
最后更新:日西西软件下载最安全的下载网站、值得信赖的软件下载站!
您的位置:
→ win10在登陆界面自动开启小键盘数字锁定功能的设置教程
一些用户升级系统后,不是很熟悉win10的一些操作设置,win10在登陆界面自动开启小键盘数字锁定功能该怎么设置呢,有时候每次都需要自己开启会显得比较麻烦,下面小编就给大家带来win10在登陆界面自动开启小键盘数字锁定功能的设置教程。&1、win+R快捷键或者右击开始菜单选择运行2、运行regedit,打开注册表编辑器3、定位到HKEY_USERS\.DEFAULT\Control Panel\Keyboard;4、右击InitialKeyboardIndicators,选择修改5、数值修改为 6、重启即可,若想恢复之前的默认设定,只需将InitialKeyboardIndicators的数值数据改为0。这就是小编给大家带来的win10在登陆界面自动开启小键盘数字锁定功能的设置教程了,希望对大家有用。
阅读本文后您有什么感想? 已有
人给出评价!
访问量多的TyperTask – 键盘快捷键自动化工具
(快来投票)
Loading...
比如截图中使用 Ctrl + Shift + Alt + A 可以打开小众软件主页,在文本框输入 hth 会自动变换成 Hope this helps!,这点很像黑莓手机的自动图文集功能。除了打开网页、打开程序以及替换文本外,还可以控制延时输入,鼠标点击,控制音量等。
并且可以类似 VIM 的用键盘控制光标到上下左右,页面顶部、底部,行首、行尾,删除、插入等操作。
具体的使用手册可见程序的帮助文档,简单明了。
也推荐不会编程的同学玩玩这个 AHK 简单版的 TyperTask。
下载地址:(24.9 KB):
喜欢这篇文章?
这个给力!我喜欢:)
按分类查看文章:
大家都在讨论些什么
: 什么梗: 在 偏好设置 > 整合 里面: 可笑,
有没有报毒就直接删除,是你个人设置的。
既然出了测试病毒码,那么有心的杀毒软件公司自然会加到自己的软件里面,让其检测出来的时候注明是测试。
而一复制就报毒,就需要杀毒软件对你的每次复制行为,进行检测剪贴板。不过一般没有什么病毒能做到一进入你的剪贴板就发作的吧?所以没必要……: 用了一段时间新版快图,最终还是换回了官网的4.5.2版: 为什么我一直没找到“自动推送到第三方服务”这个选项呢?
文章我看你是2013年写的,莫非现在是这个功能已经取消?: Apple 从来没有此种广告预算: 98 啊~~
最热门标签
传说中的小众软件 让你的手机应用与众不同。
个人 blog 转载时请遵循 “署名-非商业性使用-相同方式共享” 的创作共用协议;
商业网站或未授权媒体不得复制本站内容。开开眼!盘点一下市面上的各类奇葩键盘,键盘指示灯闪一下就灭,键盘开机时闪一下就不亮了,电脑键盘按键功能,键盘部分按键失灵,键盘自动按键器
开开眼!盘点一下市面上的各类奇葩键盘
时间:日14:44 来源:中关村在线
原标题:开开眼!盘点一下市面上的各类奇葩键盘
  键盘是计算机必不可少的外设之一,但即便是和几百年前的打字机相比,键盘的面貌并未发生多少改变。但凡事总有例外,市面上的键盘并不都是一个样子的,当中还有个别非常独特的存在。  SafeType人体工学键盘  把这款键盘摆在办公桌上一定会招来同事们关注的目光。SafeType的确是一款造型非常奇葩的键盘,它不仅是竖直站立着的,甚至还带有后视镜。可别被它的样子吓到了,SafeType实际上就是一款QWERTY键盘,只不过被分成了两半垂直放立。如果你能盲打,那使用上就没有任何问题。虽然用SafeType来玩游戏肯定十分不便,但如果你想避免打字时出现的手腕酸痛,那它的效果一定会让你满意。  AlphaGrip iGrip  不要被它的样子所欺骗了,iGrip实际上是一款键盘,而非游戏手柄。它的正反两面全都是按键,上面的滚动球还能把鼠标也一并替代了。但是,使用iGrip简直就是一种折磨:光是熟悉它的按键布局可能就会花去你相当长的时间,更别提它的滚动球还会经常卡住了。  AlphaSmart Neo  这款键盘看上去就要正常得多,它的特别之处并非来自于造型,而是功能。Neo可以被看作是现代打字机,除了键盘区域之外,它还配备了一块LCD显示屏和文字处理器。作为一款面向教育市场的设备,Neo并没有无线功能,存储空间非常小,只有计算器、“测验”和字典功能。Neo能够独立工作,你可以直接将文字输入其中,随后再将其导入计算机。它的续航能力也非常惊人,可连续使用700小时,部分用户甚至声称自己2年都没有换过电池。  Keyboard.io  Keyboard.io虽然还处于研发阶段,但它的身上已经拥有不少让人眼前一亮的特点了。这款键盘采用了更加以拇指为中心的按键布局,并可侧立放置,你的手无需离开按键区域就能控制鼠标。按下手掌下方的“功能”键后,你就可以使用一系列的热键来控制光标的移动,并通过WASD键进行手动控制。  DataHand  DataHand从外观上看更像是空间站里的控制面板,它以十字键的形式对键盘按键进行排列。此外,它还整合了鼠标操控的能力
15-02-0415-02-0415-02-0415-02-0415-02-0315-02-02东莞市恒誉自动化有限公司
东莞市恒誉自动化有限公司坐落在美丽的东莞市石碣镇,专业致力于提供电子通讯行业自动生产、测试等解决方案;集自动化设备开发、制造、销售和技术服务于一体的综合性公司。&&&&&东莞市恒誉自动化有限公司技术力量雄厚,成功在键盘鼠标行业拥有关键的解决方案,公司主要设备有,自动摆KEY机、台式键盘自动测试机(ATE)、全自动锁螺丝机、手持式螺丝机、多轴螺丝机、Rubber机,鼠标功能测试机、全自动键盘组装生产线、cell&line整体配套生产线等。&&&&&&主要生产设备有CNC加工中心、CNC车床、精...
还没有添加新闻资讯
&技术支持: &}

我要回帖

更多关于 键盘功能键大全 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信