zoukankan      html  css  js  c++  java
  • 【转】获取其他程序的按键消息,利用钩子函数

          
    using System.Runtime.InteropServices;

            /// <summary>
            /// Register HotKeys
            /// </summary>
            /// <param name="hWnd">handle to window</param>
            /// <param name="id">hot key identifier</param>
            /// <param name="fsModifiers">key-modifier options</param>
            /// <param name="vk">virtual-key code</param>
            /// <returns></returns>
            [DllImport("user32.dll", SetLastError = true)]
            public static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);

            /// <summary>
            /// UnRegister HotKeys
            /// </summary>
            /// <param name="hWnd">handle to window</param>
            /// <param name="id">hot key identifier</param>
            /// <returns></returns>
            [DllImport("user32.dll", SetLastError = true)]
            public static extern bool UnRegisterHotKey(IntPtr hWnd,int id);

            /// <summary>
            /// Keys for Combination
            /// </summary>
            [Flags()]
            public enum KeyModifiers{
                None = 0,
                Alt = 1,
                Control = 2,
                Shift = 4,
                Windows = 8
            }

            /// <summary>
            /// Override the WndProc,When Lose focus it can also activate the registered hot keys
            /// </summary>
            /// <param name="m"></param>
            protected override void WndProc(ref Message m)
            {
                const int WM_HOTKEY = 0x0312; //Press the HotKey
                switch (m.Msg)
                {
                    case WM_HOTKEY:
                        callScr();
                        break;
                }
                base.WndProc(ref m);
            }

  • 相关阅读:
    数据库出现中文乱码解决方法
    OO第四次博客作业
    OO第三次博客作业
    OO第二次博客作业
    OO前三次作业反思
    mybatis怎么自动生成实体类,Mapper配置文件和Dao接口
    Win7+VS2013初试Thrift
    静态链接库与动态链接库
    排序算法总结
    TCP/IP协议详解
  • 原文地址:https://www.cnblogs.com/vic_lu/p/1806986.html
Copyright © 2011-2022 走看看