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

在后台捕获键盘按键

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

在后台捕获键盘按键

您想要的是 全局热键

  1. 在类的顶部导入所需的库:

    // DLL libraries used to manage hotkeys

    [Dllimport(“user32.dll”)]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
    [Dllimport(“user32.dll”)]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);


  2. 在类中添加一个字段,该字段将作为代码中热键的引用:

    const int MYACTION_HOTKEY_ID = 1;
  3. 注册热键(例如,在Windows窗体的构造函数中):

    // Modifier keys pres: Alt = 1, Ctrl = 2, Shift = 4, Win = 8

    // Compute the addition of each combination of the keys you want to be pressed
    // ALT+CTRL = 1 + 2 = 3 , CTRL+SHIFT = 2 + 4 = 6…
    RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 6, (int) Keys.F12);

  4. 通过在类中添加以下方法来处理键入的键:

    protected override void WndProc(ref Message m) {if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID) {    // My hotkey has been typed    // Do what you want here    // ...}base.WndProc(ref m);

    }



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

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

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