栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

控制台应用程序中的全局热键

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

控制台应用程序中的全局热键

您可以做的是在控制台应用程序中创建一个隐藏窗口,该窗口用于处理热键通知并引发事件。

这里的代码演示了原理。这里是有关在控制台应用程序中处理消息的文章,使用它您应该能够增强HotKeyManager使其在控制台应用程序中运行。

对HotKeyManager的以下更新将创建一个后台线程,该线程运行消息循环并处理Windows消息。

using System;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Threading;namespace ConsoleHotKey{  public static class HotKeyManager  {    public static event EventHandler<HotKeyEventArgs> HotKeyPressed;    public static int RegisterHotKey(Keys key, KeyModifiers modifiers)    {      _windowReadyEvent.WaitOne();      int id = System.Threading.Interlocked.Increment(ref _id);      _wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key);      return id;    }    public static void UnregisterHotKey(int id)    {      _wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id);    }    delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key);    delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id);    private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key)    { RegisterHotKey(hwnd, id, modifiers, key);          }    private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id)    {      UnregisterHotKey(_hwnd, id);    }    private static void onHotKeyPressed(HotKeyEventArgs e)    {      if (HotKeyManager.HotKeyPressed != null)      {        HotKeyManager.HotKeyPressed(null, e);      }    }    private static volatile MessageWindow _wnd;    private static volatile IntPtr _hwnd;    private static ManualResetEvent _windowReadyEvent = new ManualResetEvent(false);    static HotKeyManager()    {      Thread messageLoop = new Thread(delegate()        {          Application.Run(new MessageWindow());        });      messageLoop.Name = "MessageLoopThread";      messageLoop.IsBackground = true;      messageLoop.Start();          }    private class MessageWindow : Form    {      public MessageWindow()      {        _wnd = this;        _hwnd = this.Handle;        _windowReadyEvent.Set();      }      protected override void WndProc(ref Message m)      {        if (m.Msg == WM_HOTKEY)        {          HotKeyEventArgs e = new HotKeyEventArgs(m.LParam);          HotKeyManager.onHotKeyPressed(e);        }        base.WndProc(ref m);      }      protected override void SetVisibleCore(bool value)      {        // Ensure the window never becomes visible        base.SetVisibleCore(false);      }      private const int WM_HOTKEY = 0x312;    }    [Dllimport("user32", SetLastError=true)]    private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);    [Dllimport("user32", SetLastError = true)]    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);    private static int _id = 0;  }  public class HotKeyEventArgs : EventArgs  {    public readonly Keys Key;    public readonly KeyModifiers Modifiers;    public HotKeyEventArgs(Keys key, KeyModifiers modifiers)    {      this.Key = key;      this.Modifiers = modifiers;    }    public HotKeyEventArgs(IntPtr hotKeyParam)    {      uint param = (uint)hotKeyParam.ToInt64();      Key = (Keys)((param & 0xffff0000) >> 16);      Modifiers = (KeyModifiers)(param & 0x0000ffff);    }  }  [Flags]  public enum KeyModifiers  {    Alt = 1,    Control = 2,    Shift = 4,    Windows = 8,    NoRepeat = 0x4000  }}

这是从控制台应用程序使用HotKeyManager的示例

using System;using System.Windows.Forms;namespace ConsoleHotKey{  class Program  {    static void Main(string[] args)    {      HotKeyManager.RegisterHotKey(Keys.A, KeyModifiers.Alt);      HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);      Console.ReadLine();          }    static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)    {      Console.WriteLine("Hit me!");    }  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433054.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号