C# 如何提高TestApi&x27的性能;键盘。打字方法?

C# 如何提高TestApi&x27的性能;键盘。打字方法?,c#,microsoft-ui-automation,C#,Microsoft Ui Automation,我正在使用键盘。从项目中键入方法,以便在文本框中输入字符串 /// <summary> /// Types the specified text. /// </summary> /// <remarks> /// Note that a combination of a combination of Key.Shift or Key.Capital and a Unicode point above 0xFE /

我正在使用
键盘。从项目中键入
方法,以便在文本框中输入字符串

    /// <summary>
    /// Types the specified text.
    /// </summary>
    /// <remarks>
    /// Note that a combination of a combination of Key.Shift or Key.Capital and a Unicode point above 0xFE
    /// is not considered valid and will not result in the Unicode point being types without 
    /// applying of the modifier key.
    /// </remarks>
    /// <param name="text">The text to type.</param>
    public static void Type(string text)
    {
        foreach (char c in text)
        {
            // If code point is bigger than 8 bits, we are going for Unicode approach by setting wVk to 0.
            if (c > 0xFE)
            {
                SendKeyboardKey(c, true, false, true);
                SendKeyboardKey(c, false, false, true);
            }
            else
            {
                // We get the vKey value for the character via a Win32 API. We then use bit masks to pull the
                // upper and lower bytes to get the shift state and key information. We then use WPF KeyInterop
                // to go from the vKey key info into a System.Windows.Input.Key data structure. This work is
                // necessary because Key doesn't distinguish between upper and lower case, so we have to wrap
                // the key type inside a shift press/release if necessary.
                int vKeyValue = NativeMethods.VkKeyScan(c);
                bool keyIsShifted = (vKeyValue & NativeMethods.VKeyShiftMask) == NativeMethods.VKeyShiftMask;
                Key key = (Key)(vKeyValue & NativeMethods.VKeyCharMask);

                if (keyIsShifted)
                {
                    Type(key, new Key[] { Key.Shift });
                }
                else
                {
                    Type(key);
                }
            }
        }
    }

    private static void SendKeyboardKey(ushort key, bool isKeyDown, bool isExtended, bool isUnicode)
    {
        var input = new NativeMethods.INPUT();
        input.Type = NativeMethods.INPUT_KEYBOARD;
        if (!isKeyDown)
        {
            input.Data.Keyboard.dwFlags |= NativeMethods.KEYEVENTF_KEYUP;
        }

        if (isUnicode)
        {
            input.Data.Keyboard.dwFlags |= NativeMethods.KEYEVENTF_UNICODE;
            input.Data.Keyboard.wScan = key;
            input.Data.Keyboard.wVk = 0;
        }
        else
        {
            input.Data.Keyboard.wScan = 0;
            input.Data.Keyboard.wVk = key;
        }

        if (isExtended)
        {
            input.Data.Keyboard.dwFlags |= NativeMethods.KEYEVENTF_EXTENDEDKEY;
        }

        input.Data.Keyboard.time = 0;
        input.Data.Keyboard.dwExtraInfo = IntPtr.Zero;

        NativeMethods.SendInput(1, new NativeMethods.INPUT[] { input }, Marshal.SizeOf(input));
        Thread.Sleep(100);
    }
//
///键入指定的文本。
/// 
/// 
///请注意,Key.Shift或Key.Capital的组合以及0xFE上方的Unicode点
///被视为无效,并且不会导致Unicode点成为不带
///应用修改器键。
/// 
///要键入的文本。
公共静态无效类型(字符串文本)
{
foreach(文本中的字符c)
{
//若代码点大于8位,我们将采用Unicode方法,将wVk设置为0。
如果(c>0xFE)
{
SendKeyboardKey(c,真,假,真);
SendKeyboardKey(c,false,false,true);
}
其他的
{
//我们通过Win32 API获取角色的vKey值,然后使用位掩码提取
//上下字节以获取移位状态和键信息。然后使用WPF KEYINTROP
//从vKey密钥信息转到System.Windows.Input.key数据结构。此工作是
//因为键不区分大小写,所以我们必须包装
//如有必要,按下/释放换档按钮内的键类型。
int vKeyValue=NativeMethods.VkKeyScan(c);
布尔键移位=(vKeyValue&NativeMethods.vKeyShift掩码)==NativeMethods.vKeyShift掩码;
Key=(Key)(vKeyValue和NativeMethods.VKeyCharMask);
如果(钥匙已换档)
{
键入(键,新键[]{key.Shift});
}
其他的
{
类型(键);
}
}
}
}
私有静态void SendKeyboardKey(ushort key、bool isKeyDown、bool isExtended、bool isUnicode)
{
var input=新的NativeMethods.input();
input.Type=NativeMethods.input\u键盘;
如果(!isKeyDown)
{
input.Data.Keyboard.dwFlags |=NativeMethods.KEYEVENTF_KEYUP;
}
如果(isUnicode)
{
input.Data.Keyboard.dwFlags |=NativeMethods.KEYEVENTF_UNICODE;
input.Data.Keyboard.wScan=键;
input.Data.Keyboard.wVk=0;
}
其他的
{
input.Data.Keyboard.wScan=0;
input.Data.Keyboard.wVk=键;
}
如果(扩展)
{
input.Data.Keyboard.dwFlags |=NativeMethods.KEYEVENTF_EXTENDEDKEY;
}
input.Data.Keyboard.time=0;
input.Data.Keyboard.dwExtraInfo=IntPtr.Zero;
SendInput(1,新的NativeMethods.INPUT[]{INPUT},Marshal.SizeOf(INPUT));
睡眠(100);
}
它可以工作,但进入文本的速度相当慢。我想提高速度。理想情况下,它应该立即设置文本(考虑将文本粘贴到文本字段中)

有没有一种简单的方法可以在C#中实现它(一次将文本字段的文本设置为某个字符串)?

找到了答案:

    private void setText(AutomationElement aEditableTextField, string aText)
    {
        ValuePattern pattern = aEditableTextField.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

        if (pattern != null)
        {
            pattern.SetValue(aText);
        }
    }