简体   繁体   English

C#SendKeys.Send

[英]C# SendKeys.Send

I am running on an issue using C# SendKeys.Send method. 我正在使用C#SendKeys.Send方法运行一个问题。 I am trying to replace keyboard keys with other keys, for example when I press "a" in keyboard I want that key to be "s" for example, when I am doing this in my code: 我试图用其他键替换键盘键,例如当我在键盘上按“a”时,我希望该键为“s”,例如,当我在我的代码中执行此操作时:

if ((Keys)keyCode== Keys.A)
{                    
    SendKeys.Send("s");                    

}

Right now I get only "sa" character printed in my notepad, but instead of printing "sa" I need to get only "s" character in this case because when I press "a" on my keyboard, "a" must be replaced with "s". 现在我只在记事本中打印出“sa”字符,但在这种情况下我不需要打印“sa”而只需要“s”字符,因为当我按下键盘上的“a”时,必须更换“a”与“s”。

I tried removing the last character by adding this line: 我尝试通过添加以下行删除最后一个字符:

SendKeys.Send("{BS}");

But all I got is "s" character removed and "a" character was there. 但我所得到的只是“s”字符被删除,而“a”字符就在那里。

How can I prevent this from happening? 我怎样才能防止这种情况发生?

I would reverse my calls: 我会改变我的电话:

SendKeys.Send("{BS}");

SendKeys.Send("S");

EDIT (After Question Updated): 编辑(问题更新后):

If you're working with the string (for your special characters), can you not just capture the string generated by the key press ("a") and modify it by setting the string to the unicode value of the character you're attempting to represent? 如果您正在处理字符串(对于您的特殊字符),您是否可以捕获按键(“a”)生成的字符串并通过将字符串设置为您正在尝试的字符的unicode值来修改它代表什么? If the other solutions people have been mentioning aren't working, that's where I'd try next... 如果人们提到的其他解决方案不起作用,那就是我接下来要尝试的地方......

I might be making a bad assumption, but if you are using this with a text box, you could: 我可能会做出错误的假设,但如果您使用文本框,则可以:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 'a')
    {
        e.Handled = true;
    }
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.A)
    {
        e.Handled = true;
        SendKeys.Send("s");
    }
}

Or even simpler: 甚至更简单:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 'a')
    {
        e.Handled = true;
        SendKeys.Send("s");
    }
}

Or if this isn't for use just with a text box, then can't you just revers your backspace and s key sends? 或者,如果这不仅仅用于文本框,那么你不能只是反转你的退格和密钥发送吗?

if ((Keys)keyCode== Keys.A)
{                    
    SendKeys.Send("{BS}"); // remove A
    SendKeys.Send("s");    // add S
}

Another option you have is to use Low-Level keyboard hooks to trap the old character and then send the new one. 另一个选择是使用低级键盘钩子来捕获旧角色,然后发送新角色。 It will require some P/Invoke, but it gives you the ability to completely trap the original key so apps don't even see it. 它需要一些P / Invoke,但它使您能够完全捕获原始密钥,以便应用程序甚至看不到它。

I get "a" printed because backspace is sent immediately after "s" and it deletes the "s" character. 我打印“a”因为在“s”之后立即发送退格并删除“s”字符。 How can I prevent this from happening? 我怎样才能防止这种情况发生?

uhm..... 嗯.....

don't send backspace immediately after sending s? 发送s后不要立即发送退格?

if ((Keys)keyCode== Keys.A)
            {
                Sendkeys.Send("{BS}"); // Deletes the "A" (already sent)
                SendKeys.Send("s"); // Sends the "S"
            }

SendKeys.SendWait

This may not be what you want/need, but if you just want to remap some keyboard keys, I would suggest looking into using another keyboard layout, or creating a custom layout. 这可能不是您想要/需要的,但如果您只想重新映射一些键盘键,我建议您考虑使用其他键盘布局或创建自定义布局。 You can create new layouts with this: 您可以使用以下方法创建新布局:

http://msdn.microsoft.com/en-ca/goglobal/bb964665.aspx http://msdn.microsoft.com/en-ca/goglobal/bb964665.aspx

I think that the SendKeys.Send("{BS}"); 我认为SendKeys.Send("{BS}"); approach will never work (in either order). 方法永远不会起作用(以任何顺序)。 That's because the original key-pressed/key-released event gets processed after SendKeys.Send is posted. 这是因为发布SendKeys.Send后处理原始的按键/键释放事件。 I think you should somehow cancel the key-down/key-up event for the character you want to remove before it is processed by the target window procedure (eg notepad's). 我认为在目标窗口过程(例如记事本)处理之前,您应该以某种方式取消要删除的字符的按键/键入事件。

Have you tried switching .Send({BS}) with .Send("s") ? 您是否尝试过切换。 .Send({BS}) . .Send("s") Otherwise, can you clarify? 否则,你能澄清一下吗?

I solved this problem by registering global hot keys. 我通过注册全局热键解决了这个问题。 Thank you. 谢谢。 This was the resourse that I used to solve my problem. 是我用来解决问题的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM