在做开发的时候,UI上的一些快捷键可以使用虚拟键来触发。比如:revit二次开发中的快捷键后台触发。
/// <summary>
/// 虚拟键盘.
/// </summary>
/// <author>YangSen</author>
/// <date>2016/12/1</date>
public class VirtualKeyboard
{
/// <summary>
/// 模拟按单个按键.
/// </summary>
/// <param name="key">The key.</param>
/// <author>YangSen</author>
public static void Press(System.Windows.Forms.Keys key)
{
Methods.keybd_event((byte)key, 0, 0, 0);
Methods.keybd_event((byte)key, 0, 2, 0);
}
/// <summary>
/// 模拟按组合键.
/// </summary>
/// <param name="key1">The key1.</param>
/// <param name="key2">The key2.</param>
/// <author>YangSen</author>
public static void Press(System.Windows.Forms.Keys key1, System.Windows.Forms.Keys key2)
{
Methods.keybd_event((byte)key1, 0, 0, 0);
Methods.keybd_event((byte)key2, 0, 0, 0);
Methods.keybd_event((byte)key1, 0, 2, 0);
Methods.keybd_event((byte)key2, 0, 2, 0);
}
}
[DllImport("user32.dll", EntryPoint = "keybd_event")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
在revit中的用法举例,比如我想在后台出发创建墙的命令,通常的操作是去点击UI,现在用虚拟键盘测试一下:
后台执行便会触发创建墙的命令。
VirtualKeyboard.Press(Keys.W, Keys.A);