惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
B
Blog
N
Netflix TechBlog - Medium
量子位
月光博客
月光博客
博客园_首页
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
M
MIT News - Artificial intelligence
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
腾讯CDC
Engineering at Meta
Engineering at Meta
云风的 BLOG
云风的 BLOG
L
LangChain Blog
GbyAI
GbyAI
IT之家
IT之家
Y
Y Combinator Blog
人人都是产品经理
人人都是产品经理

博客园 - 许明会

异步编程,采用WorkgroupWorker,async和await关键字 OCR图像识别技术-Asprise OCR 关于委托,事件和类的设计准则 JavaScript能干什么? C#泛型代理、泛型接口、泛型类型、泛型方法 Delegate, Method as Parameter. DES对称性加密 利用委托实现异步调用 通过Windows组策略限制证书组织流氓软件的安装运行 枚举\位域\结构综合实验 - 许明会 - 博客园 public static void Invoke (Action action) C#编写WIN32系统托盘程序 C#的互操作性:缓冲区、结构、指针 SQLServer异步调用,批量复制 Python体验(10)-图形界面之计算器 Python体验(09)-图形界面之Pannel和Sizer Python体验(08)-图形界面之工具栏和状态栏 Python体验(07)-图形界面之菜单 Javascript猜数字游戏
C#利用WIN32实现按键注册
许明会 · 2016-03-14 · via 博客园 - 许明会
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System;
using System.Windows.Forms;

namespace Phoenix
{
    //注册系统按键消息
    class HotKeys
    {
        [DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int modifiers, Keys vk);
        [DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        public enum HotkeyModifiers
        {
            Alt = 1, Control = 2, Shift = 4, Win = 8
        }

        public delegate void HotkeyCallbackHandler();
        int keyId = 10;
        Dictionary<int, HotkeyCallbackHandler> keyMap = new Dictionary<int, HotkeyCallbackHandler>();

        /// <summary>
        /// 注册快捷键
        /// </summary>
        /// <param name="hWnd">持有快捷键窗口的句柄</param>
        /// <param name="modifiers">组合键</param>
        /// <param name="vk">快捷键的虚拟码</param>
        /// <param name="callback">回调函数,在按下快捷键后调用</param>
        public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotkeyCallbackHandler callback)
        {
            int id = keyId++;
            if (!RegisterHotKey(hWnd, id, modifiers, vk))
            {
                throw new Exception("RegisterHotKey Error!");
            }
            keyMap[id] = callback;
        }
        public void UnRegist(IntPtr hWnd, HotkeyCallbackHandler callback)
        {
            foreach (var item in keyMap)
            {
                if (item.Value == callback)
                    UnregisterHotKey(hWnd, item.Key);
            }
        }

        public void ProcessHotKey(Message message)
        {
            if (message.Msg == 0x312)
            {
                int id = message.WParam.ToInt32();
                HotkeyCallbackHandler handler;
                if (keyMap.TryGetValue(id, out handler))
                    handler();
            }
        }
    }
}
//http://www.cnblogs.com/vvv999vz/archive/2011/08/03/2126503.html

using System;
using System.Collections.Generic;

using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Phoenix
{

    class MyForm : Form
    {
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string className, string titleName);
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr child, string className, string formText);
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, string lParam);


        HotKeys hotKey = new HotKeys();

        protected override void WndProc(ref Message message)
        {
            hotKey.ProcessHotKey(message);
            base.WndProc(ref message);
        }

        
        //跨越程序输入,向记事本的文本框写入指定字符
        public void callBack()
        {
            const int WM_SETTEXT = 0x00c;
            IntPtr hWndNotepad = FindWindow(null, "无标题 - 记事本");
            IntPtr hTextbox1 = FindWindowEx(hWndNotepad, IntPtr.Zero, "EDIT", null);
            IntPtr hTextbox2 = FindWindowEx(hWndNotepad, hTextbox1, "EDIT", null);
            SendMessage(hTextbox1, WM_SETTEXT, IntPtr.Zero, this.Text);
        }

        static void Main(string[] args)
        {
            MyForm form = new MyForm() { Text = "C#模拟键盘输入" };
            form.Load += delegate
             {
                 //register Ctrl+E 
                 form.hotKey.Regist(form.Handle, (int)HotKeys.HotkeyModifiers.Control, Keys.E, form.callBack);
             };
            Application.Run(form);

        }
    }
}