阻止使用C#快捷键 [英] Blocking shortcut keys using c#

查看:120
本文介绍了阻止使用C#快捷键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码以禁用Alt + Tab键,Alt + Esc键,按Ctrl + Esc键和Windows键,但不知它不工作。请帮我纠正它。



 命名空间BlockShortcuts 
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
}

私人委托INT LowLevelKeyboardProcDelegate(INT nCode,诠释
的wParam,楼盘KBDLLHOOKSTRUCT lParam的);

函数[DllImport(user32.dll中,入口点=SetWindowsHookExA,字符集= CharSet.Ansi)]
私人静态外部INT和SetWindowsHookEx(
INT idHook,
LowLevelKeyboardProcDelegate lpfn,
INT HMOD,
INT dwThreadId);

函数[DllImport(user32.dll中)]
私人静态外部INT UnhookWindowsHookEx(INT HHOOK);

函数[DllImport(user32.dll中,入口点=CallNextHookEx方法,字符集= CharSet.Ansi)]
私人静态外部INT CallNextHookEx方法(
INT HHOOK,诠释nCode,
INT的wParam,楼盘KBDLLHOOKSTRUCT lParam的);

const int的WH_KEYBOARD_LL = 13;
私人诠释intLLKey;
私人KBDLLHOOKSTRUCT lParam的;

私人结构KBDLLHOOKSTRUCT
{
公众诠释vkCode;
INT扫描码;
公众诠释标志;
INT时间;
INT dwExtraInfo;
}

私人诠释LowLevelKeyboardProc(
INT nCode,wParam中诠释,
参考KBDLLHOOKSTRUCT的lParam)
{
布尔blnEat = FALSE;
开关(wParam参数)
{
案例256:
案例257:
案例260:
案例261:
// Alt + Tab键,Alt + Esc键,按Ctrl + Esc键,Windows键
如果(((lParam.vkCode == 9)及及(lParam.flags == 32))||
((lParam.vkCode == 27)及及(lParam.flags == 32))||((lP​​aram.vkCode ==
27)及及(lParam.flags == 0))||((lP​​aram的.vkCode == 91)及及(lParam.flags
== 1))||((lP​​aram.vkCode == 92)及及(lParam.flags == 1))||( (真)及和放大器;
(lParam.flags == 32)))
{
blnEat = TRUE;
}
中断;
}

如果(blnEat)
返回1;
,否则返回CallNextHookEx方法(0,nCode,wParam中,裁判的lParam);

}

私人无效的KeyboardHook(对象发件人,EventArgs五)
{
intLLKey =调用SetWindowsHookEx(WH_KEYBOARD_LL,新LowLevelKeyboardProcDelegate(LowLevelKeyboardProc),
System.Runtime.InteropServices.Marshal.GetHINSTANCE(
System.Reflection.Assembly.GetExecutingAssembly()GetModules()[0])。ToInt32(),0)。
}

私人无效ReleaseKeyboardHook()
{
intLLKey = UnhookWindowsHookEx(intLLKey);
}

私人无效checkBox1_CheckedChanged(对象发件人,EventArgs五)
{
如果(checkBox1.Checked)
的KeyboardHook(这一点,E);
,否则
ReleaseKeyboardHook();
}
}
}


解决方案

在一般的代码工作得很好。在
Visual Studio中的托管进程(vshost.exe)



这意味着调用 System.Reflection.Assembly。 GetExecutingAssembly()你的的KeyboardHook()函数内vshost.exe返回,而不是可执行文件,因此安装挂钩可执行的预期效果,是无法实现的。



所以看到你的代码实际上你必须执行下列操作之一:




  • 运行它的Visual Studio之外

  • 运行它在Visual Studio中,但通过菜单调试 - >开始不调试

  • 禁用的Visual Studio宿主进程,请参见下面



请注意,您可以的disable在Visual Studio宿主进程,但请注意潜在的副作用,引文:




在托管过程中被禁用,
几个调试功能
不可用或经验减小
性能。欲了解更多信息,请参阅
调试和托管进程



I am using the below code to disable the Alt+Tab, Alt+Esc, Ctrl+Esc, and Windows Key but somehow it is not working. Please help me rectify it.

namespace BlockShortcuts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private delegate int LowLevelKeyboardProcDelegate(int nCode, int
           wParam, ref KBDLLHOOKSTRUCT lParam);

        [DllImport("user32.dll", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi)]
        private static extern int SetWindowsHookEx(
           int idHook,
           LowLevelKeyboardProcDelegate lpfn,
           int hMod,
           int dwThreadId);

        [DllImport("user32.dll")]
        private static extern int UnhookWindowsHookEx(int hHook);

        [DllImport("user32.dll", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi)]
        private static extern int CallNextHookEx(
            int hHook, int nCode,
            int wParam, ref KBDLLHOOKSTRUCT lParam);

        const int WH_KEYBOARD_LL = 13;
        private int intLLKey;
        private KBDLLHOOKSTRUCT lParam;

        private struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            int scanCode;
            public int flags;
            int time;
            int dwExtraInfo;
        }

        private int LowLevelKeyboardProc(
            int nCode, int wParam,
            ref KBDLLHOOKSTRUCT lParam)
        {
            bool blnEat = false;
            switch (wParam)
            {
                case 256:
                case 257:
                case 260:
                case 261:
                    //Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
                    if (((lParam.vkCode == 9) && (lParam.flags == 32)) ||
                    ((lParam.vkCode == 27) && (lParam.flags == 32)) || ((lParam.vkCode ==
                    27) && (lParam.flags == 0)) || ((lParam.vkCode == 91) && (lParam.flags
                    == 1)) || ((lParam.vkCode == 92) && (lParam.flags == 1)) || ((true) &&
                    (lParam.flags == 32)))
                    {
                        blnEat = true;
                    }
                    break;
            }

            if (blnEat)
                return 1;
            else return CallNextHookEx(0, nCode, wParam, ref lParam);

        }

        private void KeyboardHook(object sender, EventArgs e)
        {
            intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL,new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc),
                       System.Runtime.InteropServices.Marshal.GetHINSTANCE(
                       System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(), 0);
        }

        private void ReleaseKeyboardHook()
        {
            intLLKey = UnhookWindowsHookEx(intLLKey);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                KeyboardHook(this, e);
            else
                ReleaseKeyboardHook();
        }
    }
}

解决方案

The code in general is working just fine. The effect you are witnessing likely stems from running under the Visual Studio debugger, which usually implies you are running within the Visual Studio Hosting Process (vshost.exe).

This means that the call to System.Reflection.Assembly.GetExecutingAssembly() within your KeyboardHook() function is returning vshost.exe rather than your executable, consequently the desired effect of installing a hook for your executable can not be achieved.

So to see your code in effect you'll have to do one of the following:

  • run it outside of Visual Studio
  • run it inside Visual Studio, but via menu 'Debug'->'Start without Debugging'
  • disable the Visual Studio Hosting Process, see below

Note that you could disable the Visual Studio Hosting Process, but please be aware of the potential side effects, citation:

When the hosting process is disabled, several debugging features are unavailable or experience decreased performance. For more information, see Debugging and the Hosting Process.

这篇关于阻止使用C#快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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