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

如何在c#中禁用Windows键

时间:2024-03-08 20:46:48浏览次数:35  
标签:wParam IntPtr c# 禁用 WM CharSet private Windows int

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

public class KeyboardHook : IDisposable
{
    private const int WM_KEYDOWN = 0x0100;
    private const int WM_KEYUP = 0x0101;
    private const int WM_SYSKEYDOWN = 0x0104;
    private const int WM_SYSKEYUP = 0x0105;

    private IntPtr _hookId = IntPtr.Zero;

    public event KeyEventHandler KeyDown;
    public event KeyEventHandler KeyUp;

    public KeyboardHook()
    {
        _hookId = SetHook(HookCallback);
    }

    private IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (var curProcess = Process.GetCurrentProcess())
        using (var curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

    private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0)
        {
            var key = (Keys)Marshal.ReadInt32(lParam);
            if (key == Keys.LWin || key == Keys.RWin)
            {
                return (IntPtr)1;
            }

            var eventArgs = new KeyEventArgs(key);
            if ((wParam.ToInt32() == WM_KEYDOWN || wParam.ToInt32() == WM_SYSKEYDOWN) && KeyDown != null)
            {
                KeyDown(this, eventArgs);
            }
            else if ((wParam.ToInt32() == WM_KEYUP || wParam.ToInt32() == WM_SYSKEYUP) && KeyUp != null)
            {
                KeyUp(this, eventArgs);
            }

            if (eventArgs.Handled)
            {
                return (IntPtr)1;
            }
        }

        return CallNextHookEx(_hookId, nCode, wParam, lParam);
    }

    public void Dispose()
    {
        if (_hookId != IntPtr.Zero)
        {
            UnhookWindowsHookEx(_hookId);
            _hookId = IntPtr.Zero;
        }
    }

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

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

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

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

    private const int WH_KEYBOARD_LL = 13;
}

  

public static void Main()
{
    using (var keyboardHook = new KeyboardHook())
    {
        keyboardHook.KeyDown += (sender, args) =>
        {
            if (args.KeyCode == Keys.LWin || args.KeyCode == Keys.RWin)
            {
                args.Handled = true;
            }
        };

        Application.Run();
    }
}

  

标签:wParam,IntPtr,c#,禁用,WM,CharSet,private,Windows,int
From: https://www.cnblogs.com/ZhyjEye/p/18061807

相关文章

  • 【C/C++】四舍五入、向上取整、向下取整
    #include<stdio.h>#include<stdint.h>doublecustom_pow(doublebase,intexponent){doubleresult=1.0;if(exponent>=0){for(inti=0;i<exponent;i++){result*=base;}}else{......
  • react面试
     <pdangerouslySetInnerHTML={{__html:rowHtml}}>事件bind(this)this是undefined静态方法来定义不需要bind:handle=()=>{} event参数event.target指向当前元素,表示绑定在哪里event.currentTarget输出也是当前元素,但是是假象不是原声的event是Synthe......
  • How to use try-catch with DB::transaction in Laravel
    Howtousetry-catchwithDB::transactioninLaravel.Whenaddingnumerousqueries,DB::transaction()isusedtocheckwhethereachquerywasproperlyrunbeforerollingback.Therefore,canweuseatry-catchblockwithdb::transactionaswell?Usingat......
  • NewStarCTF 2023 公开赛道 做题随笔(WEEK1|MISC部分)
    第一题下载打开得到TXT文件好的看样子应该是base32,复制到base在线转换看看得到这玩意 base58转换得到 出了flag  第二题 下载得到一张二维码用隐写软件试试得到一张这个以为是摩斯密码,试试得到有个这玩意,嘶,好像不是试试LSB 得到flag 第三题......
  • tomcat拒绝连接解决方法
    当发生拒绝连接的时候1.首先要排除端口的占用上一篇文章已经详细介绍了,这里不再赘述tomcat端口配置2.设置防火墙放行tomcat3.配置环境变量此电脑→属性→高级系统设置→环境变量点击新建添加角色变量,值是文件的目录点击path,新建tomcat目录添加系统变量测试环境变量......
  • 使用Web Vitals针对性的优化前端LCP指标
    1、安装WebVitals浏览器插件2、打开设置3、勾选打印日志 4、打开浏览器控制台即可查看需要优化的点5、LCP耗时的构成部分"LCPsub-part":"TimetoFirstByte""Time(ms)":39这个部分时间表示服务器的首个字节到达所花费的时间。这是指从浏览器发出请求到服务......
  • 完全颠覆Windows使用体验!微软将在今年发布“AI Explorer”
    据WindowsCentral报道,微软将在今年晚些时候在Windows11上推出一系列AI功能,其中就包括被内部称为“AIExplorer”的新功能。据消息人士透露,“AIExplorer” 被微软描述为“高级Copilot”,是将AIPC与非AIPC区分开来的重磅AI体验。其内置的历史记录/时间线功能可以在所有应用中......
  • 通达信求财MACD指标公式源码
    {股票指标}DIF:Ema(CLOSE,12)-EMA(CLOSE,26);DEA:EMA(DIF,9),COLOR888888;macd:(DIF-DEA)*2,COLORstICK;NOTEXT5:IF(DIF>=DEA,DIF,DRAWNULL),COLORRED;NOTEXT6:IF(DIF<dea,dif,drawnull),colorwhite;<br="">AT1:=BArslAST(REF(crOSS(DIF,DEA),1)......
  • cin、getline()的用法和易错事项
    一、cin>>用法1:输入一个数字或字符#include<iostream>usingnamespacestd;intmain(){inta,b;cin>>a>>b;cout<<a+b<<endl;}用法2:接收一个字符串,遇“空格”、“TAB”、“回车”就结束#include<iostream>usingnamespacestd;intmain(){c......
  • C# implement late binding via type in runtime
    staticvoidRuntimeGetTypeLateBinding(){objects="Hello";PropertyInfopi=s.GetType().GetProperty("Length");Console.WriteLine((int)pi.GetValue(s,null));}  DynamicallycallmethodGetMethod()via reflectionan......