原文:wpf 屏蔽热键

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a771948524/article/details/9428923

 

using System;
using System.Windows;
using System.Runtime.InteropServices; namespace WpfApplication1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private struct KBDLLHOOKSTRUCT
{
public int vkCode;
int scanCode;
public int flags;
int time;
int dwExtraInfo;
} private delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam); [DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, int dwThreadId); [DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hHook); [DllImport("user32.dll")]
private static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam); [DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(IntPtr path); private IntPtr hHook;
LowLevelKeyboardProcDelegate hookProc; // prevent gc
const int WH_KEYBOARD_LL = 13; public App()
{
// hook keyboard
IntPtr hModule = GetModuleHandle(IntPtr.Zero);
hookProc = new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hModule, 0);
if (hHook == IntPtr.Zero)
{
MessageBox.Show("Failed to set hook, error = " + Marshal.GetLastWin32Error());
}
} protected override void OnExit(ExitEventArgs e)
{
UnhookWindowsHookEx(hHook); // release keyboard hook
base.OnExit(e);
} private static int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
{
if (nCode >= 0)
switch (wParam)
{
case 256: // WM_KEYDOWN
case 257: // WM_KEYUP
case 260: // WM_SYSKEYDOWN
case 261: // M_SYSKEYUP
if (
(lParam.vkCode == 0x09 && lParam.flags == 32) || // Alt+Tab
(lParam.vkCode == 0x1b && lParam.flags == 32) || // Alt+Esc
(lParam.vkCode == 0x73 && lParam.flags == 32) || // Alt+F4
(lParam.vkCode == 0x1b && lParam.flags == 0) || // Ctrl+Esc
(lParam.vkCode == 0x5b && lParam.flags == 1) || // Left Windows Key
(lParam.vkCode == 0x5c && lParam.flags == 1)) // Right Windows Key
{
return 1;
}
break;
}
return CallNextHookEx(0, nCode, wParam, ref lParam);
}
}
}

 

wpf 屏蔽热键的更多相关文章

  1. wpf程序热键的一个类

    using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...

  2. Ring3下无驱动移除winlogon.exe进程ctrl+alt+del,win+u,win+l三个系统热键,非屏蔽热键(子类化SAS 窗口)

    随手而作,纯粹技术研究,没什么实际意义. 打开xuetr,正常情况下.winlogon.exe注册了三个热键.ctrl+alt+del,win+u,win+l三个. 这三个键用SetWindowsHo ...

  3. WPF注册热键后处理热键消息(非winform方式)

    由于最近在做wpf版的截图软件,在处理全局热键的时候,发现国内博客使用的都是winform窗体的键盘处理方式,此方式需要使用winform的动态库,如此不协调的代码让我开始在github中寻找相关代码 ...

  4. WPF 屏蔽Alt+F4强制退出

    if (e.KeyStates == Keyboard.GetKeyStates(Key.F4) && Keyboard.Modifiers == ModifierKeys.Alt) ...

  5. wpf屏蔽快捷键alt+space,alt+F4

    /// <summary>        /// 阻止 alt+f4和alt+space 按键        /// </summary>        /// <par ...

  6. C#使用API屏蔽系统热键和任务管理器

    最近做的一个winform类型的项目中需要屏蔽系统热键,在网上搜索了一下,基本上都是调用api来进行hook操作,下面的代码就可以完成功能 using System; using System.IO; ...

  7. 屏蔽windows快捷键的方法

    using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using ...

  8. [转自小兵的工具箱]C++ Builder 基础

    1. C++ Builder 网上资源 C++ Builder 研究    http://www.ccrun.com/ C++ Builder 程序员    http://mybcb.diy.myri ...

  9. 基于C#实现的HOOK键盘钩子实例代码

    本文所述为基于C#实现的HOOK实例,该实例可用来屏蔽系统热键.程序主要实现了安装钩子.传递钩子.卸载钩子等功能.在传递钩子中:<param name="pHookHandle&quo ...

随机推荐

  1. keil出现蓝色小箭头

  2. [AngularJS Ng-redux] Integrate ngRedux

    Up to this point, we have created an effective, yet rudimentary, implementation of Redux by manually ...

  3. 【29.42%】【POJ 1182】食物链

    Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64875 Accepted: 19085 Description 动物王国中有三 ...

  4. Mysql错误: ERROR 1205: Lock wait timeout exceeded解决办法(MySQL锁表、事物锁表的处理方法)

    Java执行一个SQL查询未提交,遇到1205错误. java.lang.Exception: ### Error updating database.  Cause: java.sql.SQLExc ...

  5. html5 在移动端的缩放控制

    viewport 语法介绍: 01 <!-- html document --> 02 <meta name="viewport" 03     content= ...

  6. SpringCloud微服务框架搭建

    一.微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么是集群 多台服务器部署相同应用构成一个集群 作用:通过负载均衡设备共同对外提供服务 1.3 ...

  7. 【23.58%】【code forces 321E】Ciel and Gondolas

    time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  8. oracle员工表和部门表基本操作

    emp 员工表(empno 员工号/ename 员工姓名/job 工作/mgr 上级编号/hiredate 受雇日期/sal 薪金/comm 佣金/deptno 部门编号) dept 部门表(dept ...

  9. jQuery 淡入淡出

    演示 jQuery fadeIn() 方法: <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

  10. .NET-架构优化实战-前端优化

    原文:.NET-架构优化实战-前端优化 前言 上一篇<.NET-架构优化实战-梳理篇>整理了基本的业务知识,同时也罗列了存在的问题,本篇主要是针对任务列表的页面进行性能优化. 该篇主要涉及 ...