为什么SendKey.Send()只在一段时间工作的一次?只在、工作、SendKey、Send

2023-09-04 08:29:51 作者:用一世时光换你伴我久久

我在做一个Windows应用程序,在全球捕获键盘输入。当用户使用CTRL + ALT + G快捷键组合的应用程序使用

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());

键入generaged GUID到任何文本字段焦点。它应该做以输入这个应用程序的考虑。

to type a generaged GUID into whatever text field is in focus. And it should do this regardless of the application taking the input.

它的工作原理正是我打算第一次输入CTRL + ALT + G,但后续尝试导致什么都没有,或者只是非常罕见的成功。

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.

我的意思是,这一切都应该是很简单的,一贯的。我有一个工作的全局键盘钩子该工作的时候,我测试过它,但SendKey.Send()方法不会每次都工作。

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.

任何人有什么见解?

修改1 : 我已经使用SendKey.SendWait()为好,做同样的事情尝试。我真的希望有一个更敏感的方式使用该键盘快捷方式来生成新的GUID。

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.

编辑2

下面的code中的重要部分:

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);

本次活动code是相当简单:

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.

更新1

我有关于这个主题的更多的问题,但我没有时间来继续这方面的工作为今天。我已经实现了使用在app.config的方法在一定程度上成功的乔恩·雷诺的建议。有一次,我找出我的新问题我会后编辑,并可能关闭这个问题,如果我得到我的应用程序发挥预期的。

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.

推荐答案

您可能会运行到时间的问题。按照 MSDN文档:

You may be running into timing issues. According to the MSDN documentation:

SendKeys类已更新为.NET Framework 3.0   使其在Windows Vista上运行的应用程序使用。增强   Windows Vista中的安全性(称为用户帐户控制UAC或)   prevents无法按预期工作的previous实施。

The SendKeys class has been updated for the .NET Framework 3.0 to enable its use in applications that run on Windows Vista. The enhanced security of Windows Vista (known as User Account Control or UAC) prevents the previous implementation from working as expected.

SendKeys类容易受到计时问题,其中一些   开发商不得不解决。更新后的实施   仍然容易受到计时问题,但速度稍快,并且可以   需要改变的解决方法。 SendKeys类尝试使用   的previous实现首先,如果失败,使用新的   实现。其结果是,SendKeys类行为可能不同   在不同的操作系统。此外,当SendKeys类   使用新的实现,SendWait方法不会等待   当它们被发送到另一个进程消息以进行处理。

The SendKeys class is susceptible to timing issues, which some developers have had to work around. The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds. The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation. As a result, the SendKeys class may behave differently on different operating systems. Additionally, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process.

如果您的应用程序依赖于一致的行为,无论   操作系统,您可以强制SendKeys类使用新   实现通过添加以下应用程序设置到您的   app.config文件。

If your application relies on consistent behavior regardless of the operating system, you can force the SendKeys class to use the new implementation by adding the following application setting to your app.config file.

<appSettings> 

<add key="SendKeys" value="SendInput"/> 

</appSettings> 

要强制SendKeys类使用previous实施,使用   值JournalHook代替。

To force the SendKeys class to use the previous implementation, use the value "JournalHook" instead.