C#windows应用程序类文件中的事件处理

C#windows应用程序类文件中的事件处理,c#,C#,我正在使用下面粘贴的类来侦听按键事件ctrl+alt+q。 当我在表单中使用相同的代码时,事件正在触发并且运行良好。 但是我需要在类文件中使用这个函数,并让它在线程中运行,这样每当用户执行ctrl+alt+q时,我就会得到一个确认框 现在的问题是,按键事件没有触发! 有没有其他方法可以让此代码在后台使用线程而不使用表单运行 namespace sam1 { class LogoutManager { public static DialogResult Result;

我正在使用下面粘贴的类来侦听按键事件ctrl+alt+q。 当我在表单中使用相同的代码时,事件正在触发并且运行良好。 但是我需要在类文件中使用这个函数,并让它在线程中运行,这样每当用户执行ctrl+alt+q时,我就会得到一个确认框

现在的问题是,按键事件没有触发! 有没有其他方法可以让此代码在后台使用线程而不使用表单运行

namespace sam1 
{
  class LogoutManager
  {
      public static DialogResult Result;
      public static int allow;

      public void LogoutMgr()
      {
          KeyboardHook hook = new KeyboardHook();
          hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
          hook.RegisterHotKey((ModifierKeys)2 | (ModifierKeys)1, Keys.Q);
      }

      void hook_KeyPressed(object sender, KeyPressedEventArgs e)
      {
          if (allow == 0)
          {
              allow = allow + 1;
              Result = MessageBox.Show("Are you sure, you want to log off?", "Log off"
                      , MessageBoxButtons.YesNo
                      , MessageBoxIcon.Warning);

              if (Result == DialogResult.Yes)
              {
                  allow = 0;
              }
              else
              {
                  allow = 0;
              }
          }
      }
  }
}
名称空间sam1
{
类注销管理器
{
公共静态对话结果;
公共静态输入允许;
public void LogoutMgr()
{
键盘挂钩=新键盘挂钩();
hook.KeyPressed+=新事件处理程序(hook\u KeyPressed);
hook.RegisterHotKey((ModifierKeys)2 |(ModifierKeys)1,key.Q);
}
无效钩_键按下(对象发送器,按键打开)
{
如果(允许==0)
{
允许=允许+1;
结果=MessageBox.Show(“您确定要注销吗?”,“注销”
,MessageBoxButtons.YesNo
,MessageBoxIcon.Warning);
if(Result==DialogResult.Yes)
{
允许=0;
}
其他的
{
允许=0;
}
}
}
}
}

名称空间sam1
{
公共密封类键盘挂钩:IDisposable
{
//向Windows注册热键。
[DllImport(“user32.dll”)]
私有静态外部布尔寄存器hotkey(IntPtr hWnd、int id、uint fsModifiers、uint vk);
//在Windows中注销热键。
[DllImport(“user32.dll”)]
私有静态外部bool unregister热键(IntPtr hWnd,intid);
/// 
///表示内部用于获取消息的窗口。
/// 
私有类窗口:NativeWindow,IDisposable
{
私有静态int WM_热键=0x0312;
公共窗口()
{
//创建窗口的控制柄。
this.CreateHandle(新的CreateParams());
}
/// 
///重写以获取通知。
/// 
/// 
受保护的覆盖无效WndProc(参考消息m)
{
基准WndProc(参考m);
//检查我们是否按下了热键。
if(m.Msg==WM_热键)
{
//去拿钥匙。
Keys key=(Keys)((int)m.LParam>>16)和0xFFFF);
ModifierKeys修饰符=(ModifierKeys)((int)m.LParam和0xFFFF);
//调用事件以通知父级。
如果(按下键!=null)
按键按下(此,新按键按下EventArgs(修改器,按键));
}
}
按下公共事件处理程序键;
#区域IDisposable成员
公共空间处置()
{
这个.handle();
}
#端区
}
私有窗口_Window=新窗口();
私有int_currentId;
公用键盘钩子()
{
//注册内部本机窗口的事件。
_window.KeyPressed+=委托(对象发送者,KeyPressedEventArgs参数)
{
如果(按下键!=null)
按键(此,args);
};
}
/// 
///在系统中注册热键。
/// 
///与热键关联的修饰符。
///与热键关联的键本身。
公共无效注册表快捷键(ModifierKeys修饰符,快捷键)
{
//递增计数器。
_currentId=_currentId+1;
//注册热键。
if(!RegisterHotKey(_window.Handle,_currentId,(uint)修饰符,(uint)键))
抛出新的InvalidOperationException(“无法注册热键”);
}
/// 
///已按下热键。
/// 
按下公共事件处理程序键;
#区域IDisposable成员
公共空间处置()
{
//注销所有已注册的热键。
对于(int i=_currentId;i>0;i--)
{
取消注册热键(_window.Handle,i);
}
//处理内部本机窗口。
_window.Dispose();
}
#端区
}
/// 
///按下热键后触发的事件的事件参数。
/// 
public class KeyPressedEventArgs:EventArgs
{
私有修饰符keys_修饰符;
私钥(u key),;
内部按键按下EventArgs(ModifierKeys修改器,按键)
{
_修饰语=修饰语;
_钥匙=钥匙;
}
公共修饰语
{
获取{return\u修饰符;}
}
公钥
{
获取{return\u key;}
}
}
/// 
///可能的修饰符的枚举。
/// 
[旗帜]
公共枚举修饰符:uint
{
Alt=1,
控制=2,
班次=4,
赢=8
}
}

什么是键盘挂钩类

在不了解该类的情况下,我只能猜测:问题可能是在Windows中,键盘快捷键(与其他事件一样)是通过窗口消息分发的,而窗口消息通常是通过Windows接收的。窗体是一个窗口,因此它可以接收键盘事件

既然您已经知道它与表单一起工作,为什么不创建一个表单来处理这个问题,但让它不可见呢?然后表单可以对键盘事件做出反应,并将其传递给类的实例

这能解决你的问题吗

阅读评论和更新后编辑: 好的,KeyboardHook来自NativeWindow;这意味着,它本身已经是一个窗口——不需要表单

然而,您需要的是一个应用程序循环。您可以通过Application.Run启动它。然后在运行消息循环的同一线程上创建KeyboardHook实例

最简单的方法是,如果您已经有一个主窗体,只需将该主窗体传递给Application.Run,然后执行e
namespace sam1
{
    public sealed class KeyboardHook : IDisposable
{
    // Registers a hot key with Windows.
    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
    // Unregisters the hot key with Windows.
    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    /// <summary>
    /// Represents the window that is used internally to get the messages.
    /// </summary>
    private class Window : NativeWindow, IDisposable
    {
        private static int WM_HOTKEY = 0x0312;

        public Window()
        {
            // create the handle for the window.
            this.CreateHandle(new CreateParams());
        }

        /// <summary>
        /// Overridden to get the notifications.
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            // check if we got a hot key pressed.
            if (m.Msg == WM_HOTKEY)
            {
                // get the keys.
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
                ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);

                // invoke the event to notify the parent.
                if (KeyPressed != null)
                    KeyPressed(this, new KeyPressedEventArgs(modifier, key));
            }
        }

        public event EventHandler<KeyPressedEventArgs> KeyPressed;

        #region IDisposable Members

        public void Dispose()
        {
            this.DestroyHandle();
        }

        #endregion
    }

    private Window _window = new Window();
    private int _currentId;

    public KeyboardHook()
    {
        // register the event of the inner native window.
        _window.KeyPressed += delegate(object sender, KeyPressedEventArgs args)
        {
            if (KeyPressed != null)
                KeyPressed(this, args);
        };
    }

    /// <summary>
    /// Registers a hot key in the system.
    /// </summary>
    /// <param name="modifier">The modifiers that are associated with the hot key.</param>
    /// <param name="key">The key itself that is associated with the hot key.</param>
    public void RegisterHotKey(ModifierKeys modifier, Keys key)
    {
        // increment the counter.
        _currentId = _currentId + 1;

        // register the hot key.
        if (!RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key))
            throw new InvalidOperationException("Couldn’t register the hot key.");
    }

    /// <summary>
    /// A hot key has been pressed.
    /// </summary>
    public event EventHandler<KeyPressedEventArgs> KeyPressed;

    #region IDisposable Members

    public void Dispose()
    {
        // unregister all the registered hot keys.
        for (int i = _currentId; i > 0; i--)
        {
            UnregisterHotKey(_window.Handle, i);
        }

        // dispose the inner native window.
        _window.Dispose();
    }

    #endregion
}

/// <summary>
/// Event Args for the event that is fired after the hot key has been pressed.
/// </summary>
public class KeyPressedEventArgs : EventArgs
{
    private ModifierKeys _modifier;
    private Keys _key;

    internal KeyPressedEventArgs(ModifierKeys modifier, Keys key)
    {
        _modifier = modifier;
        _key = key;
    }

    public ModifierKeys Modifier
    {
        get { return _modifier; }
    }

    public Keys Key
    {
        get { return _key; }
    }
}

/// <summary>
/// The enumeration of possible modifiers.
/// </summary>
[Flags]
public enum ModifierKeys : uint
{
    Alt = 1,
    Control = 2,
    Shift = 4,
    Win = 8
}

}
Application.Idle += Initialize;
Application.Run();
...

private void Initialize (object sender, EventArgs e) 
{ 
  Application.Idle -= Initialize; 
  _hook = new KeyboardHook();

  // this must be performed from the thread running Application.Run!
  // do not move it out of this event handler
  _hook.RegisterHotKey (...);      
}