在C#中发送键"挂起"PC

Jav*_*ram 4 .net c# hang sendkeys

我的电脑挂起,当我send keyForm_Shown()和放置BreakpointForm_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");
 }
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 11

我责备,Win7和VS2008.这看起来像一个相当讨厌的死锁,你可以通过按键盘上的Ctrl + Esc来摆脱它.默认情况下,SendKeys使用Windows挂钩来注入密钥.Windows挂钩可能会有相当不愉快的副作用,但我会毫不犹豫地称之为Windows错误.

要解决此问题,请使用Project + Add New Item并选择Application Configuration File项目模板.看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="SendKeys" value="SendInput"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

如果这真的是为了向您的表单发送击键,那么有更好的方法来实现它.