C# 发送键盘输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/340816/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:50:51  来源:igfitidea点击:

C# Sending Keyboard Input

c#windows

提问by smack0007

How can I send keyboard input messages to either the currently selected window or the previously selected window?

如何将键盘输入消息发送到当前选择的窗口或先前选择的窗口?

I have a program which I use to type some characters which are not present on my keyboard and I would like it if I could just send the input directly rather than me having to copy and paste all the time.

我有一个程序,我用它来输入一些键盘上不存在的字符,如果我可以直接发送输入而不是我必须一直复制和粘贴,我会喜欢它。

EDIT:

编辑:

The application of this is typing the German Umlauts. I'm an American and I work in Germany. I'm working on an American keyboard and from time to time I have to type in the umlauts / the euro symbol / the sharp S. Currently I have a simple WinForms application with a textfield and some buttons with the extra characters on it. I type into the textfield and I can press the buttons to append text to the textfield. I then copy the text and paste it where ever. What would be nice though if I could just just hit one of the buttons and it would send the text where ever I'm typing / was typing. The current program works fairly well but I could make it better.

其应用是键入德语变音。我是美国人,在德国工作。我正在使用美式键盘,有时我必须输入变音符号/欧元符号/尖锐的 S。目前我有一个简单的 WinForms 应用程序,其中包含一个文本字段和一些带有额外字符的按钮。我在文本字段中输入,然后我可以按下按钮将文本附加到文本字段。然后我复制文本并将其粘贴到任何地方。如果我只需点击一个按钮,它就会将文本发送到我正在输入/正在输入的地方,那该有多好。当前的程序运行良好,但我可以做得更好。

采纳答案by David Vidmar

SendKeys.Sendwill help you with that.

SendKeys.Send将帮助您。

回答by ng5000

Look at System.Windows.Forms.SendKeys.Send( string ). This allows key presses to be sent to the currently active application.

查看 System.Windows.Forms.SendKeys.Send( string )。这允许将按键发送到当前活动的应用程序。

Update: just found this on MSDN forums: MSDN Forum

更新:刚刚在 MSDN 论坛上找到了这个:MSDN 论坛

回答by Patrick Desjardins

If it's not present on your keyboard, you might simply add a button and send the code key of this character to the reference object you want to append this character. Give us an example of what you want, we might be able to show you some code.

如果它不存在于您的键盘上,您可以简单地添加一个按钮并将此字符的代码键发送到要附加此字符的参考对象。给我们一个你想要的例子,我们可能会向你展示一些代码。

Short example of code:

代码的简短示例:

myField.Text += "?"; //This is a char that I do not have on my keyboard