Disable Keyboard Shortcuts and Combination Keys with C# (1): Disable frequently used hotkeys

    Sometimes we may need disable keyboard shortcuts and combination keys in Windows. For instance, you want to turn a normal PC into a kiosk computer. A service program specified by the designer is running on it. Users can only see the interface of that program. Skipping out of the program interface and returning to normal Windows OS desktop are forbidden. To achieve this goal, a service program running in full screen mode need be developed in the first. It's easy to modify the Windows OS configure to autorun the program. But keyboard shortcuts and combination keys in Windows OS are annoying. The customized keyboard without hotkeys is expensive, so we have to try to find some methods to disable those keys by programming.
    In this article how to disable frequently used hotkeys with C# programming is introduced. Such hotkeys include: Alt + Tab, Alt +Esc, Alt + F4, Win, Ctrl + Esc. The ProcessCmdKey method can be overridden in .Net Framework. But only messages about modifier keys, including SHIFT, CTRL and ALT keys, can be intercepted. If intercepting more hotkeys is wanted, it is necessary to install a hook procedure by calling the SetWindowsHookEx function in 'User32.dll'. A C# example is given here. When it is run, the interface is as follows:


Step:
1. Create a C# Windows Forms application in Visual Studio. The project name is 'EnableOrDisableShortCutKeys';
2. Switch from code view to designer view, drag three buttons into the Windows Form from the toolbox, named them by StartButton, StopButton and ExitButton;
3. Modify the file 'Form1.cs', the source code is as listed below:

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

namespace EnableOrDisableShortCutKeys
{
    public partial class Form1 : Form
    {
        public const int WH_KEYBOARD_LL = 13;
        public const int WM_KEYDOWN = 0x0100;
        public const int WM_KEYUP = 0x0101;
        public const int WM_SYSKEYDOWN = 0x0104;
        public const int WM_SYSKEYUP = 0x0105;
        public const int VK_TAB = 0x9;
        public const int VK_MENU = 0x12; /* Alt key */
        public const int VK_ESCAPE = 0x1B;
        public const int VK_F4 = 0x73;
        public const int VK_LWIN = 0x5B;
        public const int VK_RWIN = 0x5C;
        public const int VK_CONTROL = 0x11;
        public const int VK_LCONTROL = 0xA2;
        public const int VK_RCONTROL = 0xA3;

        [StructLayout(LayoutKind.Sequential)]
        public class KeyBoardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Auto)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr GetModuleHandle(string lpModuleName);

        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
        static int hKeyboardHook = 0;
        HookProc KeyboardHookProcedure;

        public void HookStart()
        {
            if (hKeyboardHook == 0)
            {
                KeyboardHookProcedure = new HookProc(KeyboardHookProc);
                using (Process curProcess = Process.GetCurrentProcess())
                using (ProcessModule curModule = curProcess.MainModule)
                {
                    IntPtr hModule = GetModuleHandle(curModule.ModuleName);
                    hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, hModule, 0);
                }
                if (hKeyboardHook == 0)
                {
                    int error = Marshal.GetLastWin32Error();
                    HookStop();
                    throw new Exception("SetWindowsHookEx() function failed. " + "Error code: " + error.ToString());
                }
            }
        }

        public void HookStop()
        {
            bool retKeyboard = true;
            if (hKeyboardHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
                hKeyboardHook = 0;
            }
            if (!(retKeyboard))
            {
                throw new Exception("UnhookWindowsHookEx failed.");
            }
        }

        private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
            bool bMaskKeysFlag = false;
            switch (wParam)
            {
                case WM_KEYDOWN:
                case WM_KEYUP:
                case WM_SYSKEYDOWN:
                case WM_SYSKEYUP:
                    bMaskKeysFlag = ( (kbh.vkCode == VK_TAB) && (kbh.flags == 32) )      /* Tab + Alt */
                                    | ((kbh.vkCode == VK_ESCAPE) && (kbh.flags == 32))   /* Esc + Alt */
                                    | ((kbh.vkCode == VK_F4) && (kbh.flags == 32))       /* F4 + Alt */
                                    | ( (kbh.vkCode == VK_LWIN) && (kbh.flags == 1) )    /* Left Win */
                                    | ( (kbh.vkCode == VK_RWIN) && (kbh.flags == 1) )    /* Right Win */
                                    | ( (kbh.vkCode == VK_ESCAPE) && (kbh.flags == 0) ); /* Ctrl + Esc */
                    break;
                default:
                    break;
            }

            if (bMaskKeysFlag == true)
            {
                return 1;
            }
            else
            {
                return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            HookStart();
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            HookStop();
        }

        private void ExitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }
}


5. Build the program and run it.



设置Chrome的自定义键盘快捷键(浏览至chrome:// extensions / shortcuts)。 此扩展程序增加了为Chrome设置25种不同的自定义键盘快捷键的功能。 没有此扩展程序,Chrome将无法更改或覆盖任何默认键盘快捷键。 根据设计,此扩展程序完全不需要任何其他Chrome权限。 一切都在Chrome中本地执行,因此不需要访问任何网站或数据。 所有25个键盘快捷键设置都是可选的,可以随时更改。 Chrome扩展程序当前限制为默认的4个“ suggested_key”。 前四个快捷方式具有默认值(可以更改),但是您需要为所需的任何其他快捷方式设置值。 可以在chrome:// extensions / shortcuts上更改所有键盘快捷键。 您可以通过地址栏或单击“键盘快捷方式”扩展程序图标导航到那里。 可以设置的键盘快捷键列表:选择上一个选项卡选择下一个选项卡向左移动当前选项卡向右移动当前选项卡打开Chrome书签(chrome:// bookmarks)打开Chrome下载(chrome:// downloads)打开扩展(chrome://扩展程序)打开扩展程序快捷方式(chrome:// extensions / shortcuts)打开Chrome标志(chrome:// flags)打开Chrome帮助(chrome:// help)打开Chrome历史记录(chrome:// history)打开Chrome设置(chrome: //设置)关闭当前标签页新建标签页从内存中卸载标签页复制当前标签页回到上一页前进至下一页将当前标签页移至第一页将当前标签页移至最后一页静音/取消静音当前标签页固定/取消固定当前标签页重新加载标签页新窗口新隐身窗口图标设计:Freepik从www.flaticon.com制作的扩展图标 支持语言:English
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值