如何使用C#禁用/替代Windows 10热键 [英] How to disable/override Windows 10 Hotkeys with C#

查看:346
本文介绍了如何使用C#禁用/替代Windows 10热键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试注册某些热键,但由于它们是Windows默认设置,所以不能。

  CTRL + WIN +1最小化当前窗口。我想做点其他的事情。 
我想完全禁用WIN + LEFT / RIGHT。
我也在尝试在自己的虚拟桌面管理器中处理CTRL + WIN +箭头。

zVirtualDesktop



如果需要,必须使用c#和Win32 API来完成。它绝对不能使用Autohotkey。



我发现每个页面都描述了如何使用AutoHotKey来完成。



我要输入邮政编码,但我真的不知道从哪里开始。我使用Win32注册热键。我认为有一种方法可以覆盖它们,但我找不到任何信息。



有人有想法吗?

解决方案

使用键盘挂钩执行此操作。可以在以下 CodeProject文章



使用以下代码将防止 WIN + LEFT WIN + RIGHT 发生了。



这甚至可以覆盖通过 RegisterHotKey

> Win API。



一旦您的项目中有这些类,就可以将处理程序添加到静态 HookManager 类中

  //在这里值得注意的是,如果您订阅Key_Press事件,它将破坏国际重音键。 
HookManager.KeyPress + = HookManager_KeyPress;
HookManager.KeyDown + = HookManager_KeyDown;
HookManager.KeyUp + = HookManager_KeyUp;

您还可以添加鼠标事件,但为简单起见,我仅显示键盘钩子。



我还创建了一个通用列表,以便知道当前哪些键已关闭,并在 KeyUp

  public static List< Keys> keysDown =新的List< Keys>(); 
private static void HookManager_KeyDown(object sender,KeyEventArgs e)
{
//用于覆盖Windows默认热键
if(keysDown.Contains(e.KeyCode)== false )
{
keysDown.Add(e.KeyCode);
}

if(e.KeyCode == Keys.Right&& WIN())
{
e.Handled = true;
//按此组合键时您想要做什么
}
else if(e.KeyCode == Keys.Left&& WIN())
{
e.Handled = true;
//按此组合键时需要执行的操作
}

}

private static void HookManager_KeyUp(object sender,KeyEventArgs e)
{
//用于覆盖Windows默认热键
while(keysDown.Contains(e.KeyCode))
{
keysDown.Remove(e.KeyCode);
}
}

private static void HookManager_KeyPress(object sender,KeyPressEventArgs e)
{
//用于覆盖Windows默认热键

}

public static bool CTRL()
{
//返回keysDown.Contains(Keys.LShiftKey)
if(keysDown.Contains( Keys.LControlKey)||
keysDown.Contains(Keys.RControlKey)||
keysDown.Contains(Keys.Control)||
keysDown.Contains(Keys.ControlKey))
{
返回true;
}
其他
{
return false;
}
}

public static bool SHIFT()
{
//返回keysDown.Contains(Keys.LShiftKey)
if( keysDown.Contains(Keys.LShiftKey)||
keysDown.Contains(Keys.RShiftKey)||
keysDown.Contains(Keys.Shift)||
keysDown.Contains(Keys.ShiftKey) )
{
返回true;
}
其他
{
return false;
}
}

public static bool WIN()
{
//返回keysDown.Contains(Keys.LShiftKey)
if( keysDown.Contains(Keys.LWin)||
keysDown.Contains(Keys.RWin))
{
返回true;
}
其他
{
return false;
}
}

public static bool ALT()
{
//返回keysDown.Contains(Keys.LShiftKey)
if( keysDown.Contains(Keys.Alt)||
keysDown.Contains(Keys.LMenu)||
keysDown.Contains(Keys.RMenu))
{
返回true;
}
其他
{
return false;
}
}


I'm trying to register certain hotkeys, but I can't because they are Windows defaults.

CTRL+WIN+1 minimizes the current window. I'd like it to do something else.
I'd like to completely disable WIN+LEFT/RIGHT.
I'm also trying to handle the CTRL+WIN+Arrow in my own virtual desktop manager.

zVirtualDesktop

This has to be done using c# and Win32 API if necessary. It absolutely cannot use Autohotkey.

Every page I find descibes how this can be done with AutoHotKey.

I'd post code, but I really don't know where to start. I use Win32 to register hotkeys. I assume there is a way to override them, but I can't find any info.

Does anyone have an idea?

解决方案

It is possible to do this using a keyboard hook. A good hook class for this can be found on this CodeProject Article

Using the below code will prevent the WIN+LEFT or WIN+RIGHT from occurring. You can use this to override whichever keys you'd like.

This will even override hotkeys which you added via RegisterHotKey Win API.

Once you have those classes in your project you can add handlers to the static HookManager class like below.

//It's worth noting here that if you subscribe to the Key_Press event then it will break the international accent keys.
HookManager.KeyPress += HookManager_KeyPress;
HookManager.KeyDown += HookManager_KeyDown;
HookManager.KeyUp += HookManager_KeyUp;

You can also add mouse events, but for simplicity I'm just showing the keyboard hook.

I've also created a generic list so that I know which keys are currently down and I remove those keys from the list on the KeyUp event.

public static List<Keys> keysDown = new List<Keys>();
private static void HookManager_KeyDown(object sender, KeyEventArgs e)
        {
            //Used for overriding the Windows default hotkeys
            if(keysDown.Contains(e.KeyCode) == false)
            {
                keysDown.Add(e.KeyCode);
            }

            if (e.KeyCode == Keys.Right && WIN())
            {
                e.Handled = true;
                //Do what you want when this key combination is pressed
            }
            else if (e.KeyCode == Keys.Left && WIN())
            {
                e.Handled = true;
                //Do what you want when this key combination is pressed
            }

        }

        private static void HookManager_KeyUp(object sender, KeyEventArgs e)
        {
            //Used for overriding the Windows default hotkeys
            while(keysDown.Contains(e.KeyCode))
            {
                keysDown.Remove(e.KeyCode);
            }
        }

        private static void HookManager_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Used for overriding the Windows default hotkeys

        }

        public static bool CTRL()
        {
            //return keysDown.Contains(Keys.LShiftKey)
            if (keysDown.Contains(Keys.LControlKey) || 
                keysDown.Contains(Keys.RControlKey) || 
                keysDown.Contains(Keys.Control) || 
                keysDown.Contains(Keys.ControlKey))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool SHIFT()
        {
            //return keysDown.Contains(Keys.LShiftKey)
            if (keysDown.Contains(Keys.LShiftKey) || 
                keysDown.Contains(Keys.RShiftKey) ||
                keysDown.Contains(Keys.Shift) ||
                keysDown.Contains(Keys.ShiftKey))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool WIN()
        {
            //return keysDown.Contains(Keys.LShiftKey)
            if (keysDown.Contains(Keys.LWin) || 
                keysDown.Contains(Keys.RWin))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool ALT()
    {
        //return keysDown.Contains(Keys.LShiftKey)
        if (keysDown.Contains(Keys.Alt) ||
            keysDown.Contains(Keys.LMenu) ||
            keysDown.Contains(Keys.RMenu))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

这篇关于如何使用C#禁用/替代Windows 10热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆