发送密钥“挂起” C# 中的电脑

发布于 2024-10-08 17:45:56 字数 411 浏览 0 评论 0原文

当我在 Form_Shown()发送密钥并在 Form_KeyDown() 处放置断点时,我的电脑挂起

 private void Form1_KeyDown(object sender, KeyEventArgs e)
 {           //breakpoint here
        if (e.KeyCode == Keys.A)
        {
            MessageBox.Show("caught");
        }
 }
 private void Form1_Shown(object sender, EventArgs e)
 {
        SendKeys.Send("A");
 }

My PC hangs when I send key in Form_Shown() and placing Breakpoint at Form_KeyDown()

 private void Form1_KeyDown(object sender, KeyEventArgs e)
 {           //breakpoint here
        if (e.KeyCode == Keys.A)
        {
            MessageBox.Show("caught");
        }
 }
 private void Form1_Shown(object sender, EventArgs e)
 {
        SendKeys.Send("A");
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2024-10-15 17:45:56

我重现了,Win7和VS2008。这看起来像是一个相当令人讨厌的死锁,您可以通过按键盘上的 Ctrl+Esc 来摆脱它。默认情况下,SendKeys 使用 Windows 挂钩来注入密钥。 Windows 挂钩可能会产生相当令人不快的副作用,但我会毫不犹豫地将其称为 Windows 错误。

要修复此问题,请使用“项目”+“添加新项”并选择“应用程序配置文件”项模板。让它看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="SendKeys" value="SendInput"/>
  </appSettings>
</configuration>

如果这确实是为了将击键发送到您的表单,那么有更好的方法来实现这一点。

I repro, Win7 and VS2008. That looks like a fairly nasty deadlock, you can get out of it by pressing Ctrl+Esc on the keyboard. By default, SendKeys uses a windows hook to inject the keys. Windows hooks can have fairly unpleasant side effects but I wouldn't hesitate to call this a Windows bug.

To fix it, use Project + Add New Item and select the Application Configuration File item template. Make it look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="SendKeys" value="SendInput"/>
  </appSettings>
</configuration>

If this is really meant to send a keystroke to your form then there are better ways to accomplish that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文