C# SendKeys.Send
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1012616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# SendKeys.Send
提问by milot
I am running on an issue using C# SendKeys.Send method. 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:
我正在使用 C# SendKeys.Send 方法解决一个问题。我正在尝试用其他键替换键盘键,例如,当我在键盘中按“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”字符,但是在这种情况下我只需要打印“s”字符而不是打印“sa”字符,因为当我在键盘上按“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?
我怎样才能防止这种情况发生?
采纳答案by milot
回答by tom.dietrich
I get "a" printed because backspace is sent immediately after "s" and it deletes the "s" character. How can I prevent this from happening?
我打印了“a”,因为在“s”之后立即发送退格键并删除“s”字符。我怎样才能防止这种情况发生?
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"
}
回答by Andrew Austin
Have you tried switching .Send({BS})
with .Send("s")
? Otherwise, can you clarify?
您是否尝试过交换.Send({BS})
用.Send("s")
?否则,你能澄清一下吗?
回答by AllenG
I would reverse my calls:
我会撤回我的电话:
SendKeys.Send("{BS}");
SendKeys.Send("{BS}");
SendKeys.Send("S");
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? If the other solutions people have been mentioning aren't working, that's where I'd try next...
如果您正在使用字符串(对于您的特殊字符),您是否可以只捕获按键(“a”)生成的字符串并通过将字符串设置为您尝试的字符的 unicode 值来修改它代表?如果人们提到的其他解决方案不起作用,那么我接下来会尝试...
回答by AllenG
SendKeys.SendWait
SendKeys.SendWait
回答by CodingWithSpike
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?
或者,如果这不仅仅用于文本框,那么您不能只反转退格键和 s 键发送吗?
if ((Keys)keyCode== Keys.A)
{
SendKeys.Send("{BS}"); // remove A
SendKeys.Send("s"); // add S
}
回答by Sean
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:
这可能不是您想要/需要的,但如果您只想重新映射一些键盘键,我建议您考虑使用其他键盘布局,或创建自定义布局。您可以使用以下方法创建新布局:
回答by Erich Mirabal
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,但它使您能够完全捕获原始密钥,因此应用程序甚至看不到它。
回答by Utaal
I think that the 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. 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 (e.g. notepad's).
我认为这种SendKeys.Send("{BS}");
方法永远不会奏效(无论顺序如何)。那是因为原始的按键/按键释放事件在SendKeys.Send
发布后得到处理。我认为您应该在目标窗口过程(例如记事本)处理之前以某种方式取消要删除的字符的按下/按下事件。