发送信件“i”使用发送键
我用 c# Windows 窗体制作了一个屏幕键盘。我使用 Sendkeys.Send() 函数来发送击键。除了字母 i 之外的所有字母都可以正常工作。当我在 Microsoft Word 打开时按键盘上的字母 i 时,它会发送 Ctrl + Alt + I并打开打印对话框。在 Notepad++ 中也是如此。但当我尝试在记事本中输入时,它工作正常。
在我的代码中,我使用 SendKeys.Send(value);
发送按键,其中 value 是按下的按钮的文本。我得到带有以下代码的文本:
string s = ((Button)sender).Text;
关于它为什么不能正常工作的任何评论?
编辑:我创建了一个新的 Windows 窗体项目,只有一个按钮,整个代码如下。仍然没有工作。任何想法将不胜感激。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SendKeys.Send("i");
}
// Prevent form being focused
const int WS_EX_NOACTIVATE = 0x8000000;
protected override CreateParams CreateParams
{
get
{
CreateParams ret = base.CreateParams;
ret.ExStyle |= WS_EX_NOACTIVATE;
return ret;
}
}
}
重写的函数是为了防止表单被聚焦。这样我就可以将击键发送到具有焦点的其他应用程序。
I made an on screen keyboard with c# Windows Forms. I use Sendkeys.Send()
function to send the keystrokes. All letters but the letter i works fine. When I press the letter i on the keyboard when Microsoft Word is open, it sends Ctrl + Alt + I and opens the print dialog. It is same in Notepad++ as well. But it works fine when I try to type in notepad.
In my code I send the keys with SendKeys.Send(value);
where value is the text of the button which is pressed. I get the text with the following code:
string s = ((Button)sender).Text;
Any comments about why it does not work properly?
Edit: I have created a new windows forms project with just a button and the whole code is below. Still not working. Any idea would be appreciated.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SendKeys.Send("i");
}
// Prevent form being focused
const int WS_EX_NOACTIVATE = 0x8000000;
protected override CreateParams CreateParams
{
get
{
CreateParams ret = base.CreateParams;
ret.ExStyle |= WS_EX_NOACTIVATE;
return ret;
}
}
}
The overridden function is to prevent the form being focused. So that I can send the keystrokes to the other application which has the focus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两种选择:
1-
模拟按键,请参阅 http:// /msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys(VS.71).aspx
示例:
2- 向窗口发送密钥,按住按钮 x 秒
Two alternatives:
1-
Simulates a keypress, see http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys(VS.71).aspx
Sample:
2- Sends a key to a window, pressing the button for x seconds
您没有调用“SetForegroundWindow”Win32 API 方法。因此,您的“SendKeys”调用可能会将密钥发送到您的应用程序,而不是按预期发送到目标应用程序。
以下是 MSDN 上的示例:
如何:在代码中模拟鼠标和键盘事件
另外,这是示例中的代码:
Your aren't calling the "SetForegroundWindow" Win32 API method. Therefore, your "SendKeys" call is likely sending the keys to your app, not the target app as intended.
Here's an example on MSDN:
How to: Simulate Mouse and Keyboard Events in Code
Also, here's the code from the example: