C#使用System.Windows.Forms.SendKeys.Send()发送一个插入符将发送& (符号)

时间:2017-12-04 14:08:20

标签: c# sendkeys

使用SendKeys.Send() System.Windows.Forms发送插入符^ ...

SendKeys.Send ("{^}")

...它会发送&符号&

为什么?

2 个答案:

答案 0 :(得分:0)

根据

https://www.experts-exchange.com/questions/28994144/SendKeys-caret-sends-ampersand.html

这是一个本地化问题。

将键盘布局从德语布局更改为英文布局应该会有所帮助。

答案 1 :(得分:0)

我用德语键盘布局解决了这个问题。

问题在于某些键是这样硬编码的:

static SendKeys()
{
    keywords = new KeywordVk[49]
    {
        ...
        new KeywordVk("+", 107),
        new KeywordVk("%", 65589),
        new KeywordVk("^", 65590) // this is a problem !
    }
}

因此,破解方法是覆盖new KeywordVk("^", ...)的值

// fix caret, ^, circumflex on german keyboards
public static void FixSendKeys_Caret()
{
    // SendKeys.keywords
    var keywordsField = typeof(SendKeys)
        .GetField("keywords", BindingFlags.NonPublic | BindingFlags.Static)
        .GetValue(null) as IList;

    // KeywordVk { keyword= "^", vk = xx }, should be at index 48
    var keywordVk_Obj = keywordsField[48];

    var keywordField = keywordVk_Obj.GetType().GetField("keyword", BindingFlags.NonPublic | BindingFlags.Instance);
    // KeywordVk.vk
    var vkField = keywordVk_Obj.GetType().GetField("vk", BindingFlags.NonPublic | BindingFlags.Instance); 

    if (keywordField.GetValue(keywordVk_Obj).ToString() != "^")
        throw new Exception("wrong KeywordVk");

    // SendKeys.keywords[48].vk = (int)Keys.Oem5;
    // For german keyboards "^" = Keys.Oem5
    vkField.SetValue(keywordVk_Obj, (int)Keys.Oem5);
    // check if its correct
    var getVal = vkField.GetValue(keywordVk_Obj); 
}

在您的SendKeys代码之前调用一次