为什么 SendKey.Send() 偶尔会起作用?
我正在制作一个捕获全局键盘输入的 Windows 应用程序。当用户使用 CTRL + ALT + G 快捷组合时,应用程序将使用它
SendKey.Send(Guid.NewGuid().ToString());
在焦点所在的任何文本字段中键入生成的 GUID。无论应用程序接受输入如何,它都应该执行此操作。
它的工作原理与我第一次输入 CTRL + ALT + G 时的预期完全一样,但随后的尝试没有任何结果,或者只是非常罕见的成功。
我的意思是,这一切都应该非常简单且一致。我有一个始终有效的全局键盘钩子,我已经对其进行了测试,但是 SendKey.Send() 方法并非每次都有效。
我已经在谷歌上查看了与我的问题相关的所有内容,但到目前为止没有任何效果。
有人有任何见解吗?
编辑1: 我也尝试过使用 SendKey.SendWait() ,做同样的事情。我真的想要一种更灵敏的方式来使用这种键盘快捷键方法生成新的 GUID。
编辑2:
下面是代码的基本部分:
/* Initialization Code Here */
// register the event that is fired after the key press.
hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
// register the control + alt + F12 combination as hot key.
hook.RegisterHotKey((uint)(HotkeyModifiers.Control | HotkeyModifiers.Alt), Keys.G);
事件代码非常简单:
void hook_KeyPressed(object sender, KeyPressedEventArgs e)
{
SendKeys.SendWait(Guid.NewGuid().ToString());
}
我的项目中的其他所有内容都只是无用的。
更新 1:
我确实对这个主题还有更多问题,但今天没有时间继续解决这个问题。我已经实施了 Jon Raynor 使用 app.config 方法的建议,并取得了一定程度的成功。一旦我隔离了我的新问题,我将发布一个编辑,如果我的应用程序按预期运行,我可能会关闭这个问题。
I'm making a Windows application that captures keyboard input globally. When a user uses the CTRL + ALT + G shortcut combo the application uses
SendKey.Send(Guid.NewGuid().ToString());
to type a generaged GUID into whatever text field is in focus. And it should do this regardless of the application taking the input.
It works exactly as I intended the first time you type CTRL + ALT + G but subsequent attempts result in nothing, or just very infrequent successes.
I mean, it all should be very simple and consistent. I have a working global keyboard hook which works all the time, I've tested it, but the SendKey.Send() method doesn't work every time.
I've looked all over Google at anything related to my issue but nothing has worked so far.
Anyone have any insight?
EDIT 1:
I've tried using SendKey.SendWait() as well, does the same thing. I really want a more responsive way to generate new GUID using this keyboard shortcut approach.
EDIT 2:
Below is the essential parts of the code:
/* Initialization Code Here */
// register the event that is fired after the key press.
hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
// register the control + alt + F12 combination as hot key.
hook.RegisterHotKey((uint)(HotkeyModifiers.Control | HotkeyModifiers.Alt), Keys.G);
The event code is quite simple:
void hook_KeyPressed(object sender, KeyPressedEventArgs e)
{
SendKeys.SendWait(Guid.NewGuid().ToString());
}
Everything else in my project is just fluff.
Update 1:
I do have more questions about this subject, but I am out of time to continue working on this for today. I have implemented Jon Raynor's suggestion of using the app.config approach to some degree of success. Once I isolate my new problems I'll post an edit and possibly close this question if I get my application functioning as intended.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会遇到时间安排问题。根据 MSDN 文档:
You may be running into timing issues. According to the MSDN documentation:
这不会解决您的具体问题,而是一种替代方法。我之前曾在托管代码中尝试过全局键挂钩。很痛苦。尽管听起来很做作和原始,但我喜欢 AutoHotKey。你可以用它做一些非常强大的事情。
这是一个示例,可实现全局挂钩 Ctrl+Alt+G 并输入 guid 的要求。最好的部分是它编译内联 C#,因此您仍然可以使用 System.Guid.NewGuid()。一旦你有了一个工作脚本,你可以将其编译为 exe,这样没有安装 AutoHotKey 的人就可以运行它。
如果您想尝试一下,请安装 AutoHotKey_L 然后下载两个额外的脚本此处 和 此处。
This won't address your exact issue, but is an alternative approach. I've experimented with global key hooks in managed code before. It's a pain. As hokey and primitive as it sounds, I love AutoHotKey. You can do some really powerful things with it.
Here's a sample that achieves your requirement of globally hooking Ctrl+Alt+G and typing a guid. The best part is it compiles inline C# so you can still use
System.Guid.NewGuid()
. Once you have a working script, you can compile it to an exe so someone without AutoHotKey installed can run it.If you want to try it out, install AutoHotKey_L then download two extra scripts here and here.