C#窗口全局快捷键程序集NHotkey

By admin at 2023-02-03 • 0人收藏 • 507人看过
using System;
using System.Windows.Forms;
using NHotkey;
using NHotkey.WindowsForms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            HotkeyManager.Current.AddOrReplace("Increment", /*Keys.Control | Keys.Alt |*/Keys.A, OnIncrement);
            HotkeyManager.Current.AddOrReplace("Decrement", /*Keys.Control | Keys.Alt |*/ Keys.B, OnDecrement);
        }

        private void OnDecrement(object sender, HotkeyEventArgs e)
        {
            Console.WriteLine("BBBOnDecrement, on \n ");
            e.Handled = true;
        }

        private void OnIncrement(object sender, HotkeyEventArgs e)
        {
            Console.WriteLine("AAAOnIncrement, on \n ");
            e.Handled = true;
        }
    }
}
private void OnIncrementOrDecrement(object sender, HotkeyEventArgs e)
{
    switch (e.Name)
    {
        case "Increment":
            Value++;
            break;
        case "Decrement":
            Value--;
            break;
    }
    e.Handled = true;
}

该方法是全局快捷键, 注册后无论窗口焦点在不在都会触发, 关闭窗口后失效.


NHotkey.zip


官方地址:

https://github.com/thomaslevesque/NHotkey


1 个回复 | 最后更新于 2023-02-03
2023-02-03   #1

如果需要窗口快捷键, 那么利用重载系统函数的办法能够更好的操作:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    Console.WriteLine(keyData.ToString());
    return base.ProcessCmdKey(ref msg, keyData);
}

screenshots.gif

然后自己在重载里各种配置就好


如下:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    Console.WriteLine(keyData.ToString());
    if (keyData == Keys.Tab)
    {
        return false;//继续传递
    }
    //return base.ProcessCmdKey(ref msg, keyData);//返回调用
    return true;//阻止继续传递
}


登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...