发送键"挂起" PC在C# [英] Sending key "hangs" PC in C#

查看:197
本文介绍了发送键"挂起" PC在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的电脑挂起,当我发送键 Form_Shown()断点 Form_KeyDown()

 私人无效Form1_KeyDown (对象发件人,发送KeyEventArgs E)
{//这里断点
如果(e.KeyCode == Keys.A)
{
MessageBox.Show(捕获);
}
}
私人无效Form1_Shown(对象发件人,EventArgs五)
{
SendKeys.Send(A);
}


解决方案

我的摄制,Win7和VS2008 。这看起来像一个相当讨厌的僵局,你可以通过按Ctrl + Esc键在键盘上把它弄出来。默认情况下,的SendKeys使用Windows钩子注入钥匙。 windows钩子可以有相当令人不快的副作用,但我会毫不犹豫地称之为Windows漏洞。



要解决这个问题,使用Project +添加新项并选择应用程序配置文件项模板。使它看起来像这样:<?/ p>

 < XML版本=1.0编码=UTF-8> 
<结构>
<&的appSettings GT;
<添加键=的SendKeysVALUE =SendInput/>
< /的appSettings>
< /结构>

如果这是真正的意思发送击键到窗体然后有更好的方法来实现这一目标。


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

解决方案

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.

这篇关于发送键&QUOT;挂起&QUOT; PC在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆