首页 > 系统相关 >如何在c#中禁用Windows键?

如何在c#中禁用Windows键?

时间:2024-05-27 22:55:23浏览次数:11  
标签:c# 禁用 CharSet 0x00 Windows static key public

如何禁用或锁定Windows按钮?

 

 

 

 

 

使用 windows hooks 比修改注册表要干净得多。此外,有时人们会设置自己的个性化扫描码图,覆盖它们并不是一件好事。

要使用 windows 键钩函数,您需要 DllImport 几个 winapi 函数:

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

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, HookHandlerDelegate lpfn, IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode); 
Run Code Online (Sandbox Code Playgroud)

可以在CodeProject上找到相当完整的解释和演练。这是来自该示例的自包含类文件的直接链接,该文件可以执行所有操作(如果您使用 WPF,要使其编译干净将需要您手动引用 System.Windows.Forms dll 或仅更改 'System.Windows.Forms.dll 文件)。 Forms.Keys 对 System.Windows.Input.Key 的引用应该可以工作)。

请记住调用 UnhookWindowsHookEx()(该类在 Dispose() 中执行此操作)以取消您的捕获,否则人们会讨厌您。

 


Dav*_*son  4

 

    /// <summary>
    /// Security routines related to the Windows Key on a standard personal computer Keyboard
    /// </summary>
    public static class WindowsKey {
        /// <summary>
        /// Disables the Windows Key
        /// </summary>
        /// <remarks>May require the current user to logoff or restart the system</remarks>
        public static void Disable() {
            RegistryKey key = null;
            try {
                key = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Control\\Keyboard Layout", true);
                byte[] binary = new byte[] { 
                    0x00, 
                    0x00, 
                    0x00, 
                    0x00, 
                    0x00, 
                    0x00, 
                    0x00, 
                    0x00, 
                    0x03, 
                    0x00, 
                    0x00, 
                    0x00, 
                    0x00, 
                    0x00, 
                    0x5B, 
                    0xE0, 
                    0x00, 
                    0x00, 
                    0x5C, 
                    0xE0, 
                    0x00, 
                    0x00, 
                    0x00, 
                    0x00 
                };
                key.SetValue("Scancode Map", binary, RegistryValueKind.Binary);
            }
            catch (System.Exception ex) {
                Debug.Assert(false, ex.ToString());
            }
            finally {
                key.Close();
            }
        }

        /// <summary>
        /// Enables the Windows Key
        /// </summary>
        /// <remarks>May require the current user to logoff or restart the system</remarks>
        public static void Enable() {
            RegistryKey key = null;
            try {
                key = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Control\\Keyboard Layout", true);
                key.DeleteValue("Scancode Map", true);
            }
            catch (System.Exception ex) {
                Debug.Assert(false, ex.ToString());
            }
            finally {
                key.Close();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

 

 

标签:c#,禁用,CharSet,0x00,Windows,static,key,public
From: https://www.cnblogs.com/webenh/p/18216759

相关文章

  • OpenQA.Selenium.WebDriverException The HTTP request to the remote WebDriver serv
    OpenQA.Selenium.WebDriverException:“TheHTTPrequesttotheremoteWebDriverserverforURLhttp://localhost:xxxx/sessiontimedoutafter60seconds.”1.在谷歌浏览器上加上中括号中的内容[--remote-debugging-port=9222]2.使用管理员模式打开谷歌浏览器3.重新生成......
  • CS61A
    CS61A是加州大学伯克利分校计算机科学系的一门入门课程,主要面向大一新生。该课程的核心内容是编程和计算机科学的基础知识,特别强调抽象技术作为管理程序复杂性的手段。CS61A的教学重点在于让学生掌握用程序来解决实际问题,而不关注底层的硬件细节。一、基本用法python命令py......
  • ffmpeg之视频(avc+aac)无损转mp4(批处理,拖放)
    很多能够无损转视频的工具都来自命令行的ffmpeg版本,本文将介绍如何简单的批处理方法(直接拖放到bat文件上)来实现无损转视频。工具/原料ffmpeg(默认的static版本)方法/步骤 1.桌面左下角开始菜单,点Windows附件→记事本。 2.复制本步骤以下全部内......
  • FFmpeg Batch AV Converter 2.2.2 官方版
    基本简介FFmpegBatchAVConverter官方版是一款WindowsFFmpeg用户的前端程序,FFmpegBatchAVConverter最新版允许使用FFmpeg命令行的全部潜力,用户只需在方便的GUI中点击几下鼠标,即可拖放进度信息,FFmpegBatchAVConverter还能更改编码优先级,暂停和恢复,设置自动关机等。FFmp......
  • 来自多彩世界的控制台——C#控制台输出彩色字符画
    引言看到酷安上有这样一个活动,萌生了用C#生成字符画的想法,先放出原图。 酷安手绘牛啤  §1黑白将图像转换成字符画在C#中很简单,思路大致如下:加载图像,逐像素提取明度。根据明度映射到字符列表中对应的字符。输出字符。GetChars函数负责将传入的......
  • 谁说爬虫只能Python?看我用C#快速简单实现爬虫开发和演示!
    前言:说到爬虫,基本上清一色的都知道用Python,但是对于一些没玩过或者不想玩Python的来说,却比较头大一点。所以以下我站在C#的角度,来写一个简单的Demo,用来演示C#实现的简单小爬虫。大家感兴趣可以自己拓展出更加丰富的爬虫功能。前提:引用包HtmlAgilityPack 先来个爬取文本。新......
  • oracle密码即将过期,ERROR: ORA-28002: 7 天之后口令将过期
    oracle数据库默认的密码是180天,可以用以下语句查询:1、查询用户的profile,一般是DEFAULTselectusername,profilefromdba_users;2、以DEFAULT作为dba_profiles表profile字段的参数,查询密码生效天数select*fromdba_profileswhereprofile='DEFAULT'andresource_name=......
  • Codeforces Round 948 (Div. 2) B - C
    总结:做了A,B,然后开局A看错题wa了一发,B出的又很慢,所以掉大分。总的来说还是c没开出来。B.BinaryColouring1.题目大意:给你一个int范围内的数x,要求构造一个二进制串,能有-1、1、0,二进制串的值不能出现两个连续的地方不为0,二进制串的值要等于x。2.思路分析:我们可以发现,对于x的......
  • 4 React Router
    一环境搭建npxcreate-react-appreact-router-pronpmireact-router-dom普通版: 抽象后: 二路由导航2.1两种方式 2.2参数传递路径传参和拼接传参数,路径的时候注意再路由处配置参数名2.3嵌套路由实现步骤:1、使用children属性配置路由嵌套关系,两种路由模式......
  • (五星)用Python学数学-2021 ([美] 彼得 • 法雷尔(Peter Farrell) [Farrell) etc.)
    书:pan.baidu.com/s/1tIHXj9HmIYojAHqje09DTA?pwd=jqso提取码:jqso引言:介绍了本书的目标,即通过Python学习数学的优势和乐趣,以及Python在数学领域的应用概述。Python基础:简要介绍了Python编程语言的基本概念、语法和常用库,为后续的数学学习打下基础。可视化方法:讲解了如何使用Py......