服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - c# 屏蔽快捷键的实现示例

c# 屏蔽快捷键的实现示例

2022-11-07 13:05leestar54 C#

这篇文章主要介绍了c# 屏蔽快捷键的实现示例,帮助大家更好的理解和利用c#进行桌面开发,感兴趣的朋友可以了解下

前言

有时候开发会遇到这样一个需求,软件需要屏蔽用户的组合快捷键或某些按键,避免强制退出软件,防止勿操作等。

原理

1、要实现组合键,按键拦截,需要用到user32.dll中的SetWindowsHookEx。

2、要拦截ctrl+alt+del,需要使用ntdll.dll的ZwSuspendProcess函数挂起winlogon程序,退出之后使用ZwResumeProcess恢复winlogon程序。

3、软件需要开启topMost,以及全屏,否则离开软件则拦截无效。

4、如果要实现热键监听(非焦点拦截),则需要用到user32.dll的RegisterHotKey以及UnregisterHotKey。

实现

1、Program类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
namespace LockForm
{
 static class Program
 {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);
   SuspendWinLogon();
   Application.Run(new Form1());
   ResumeWinLogon();
  }
 
  [DllImport("ntdll.dll")]
  public static extern int ZwSuspendProcess(IntPtr ProcessId);
  [DllImport("ntdll.dll")]
  public static extern int ZwResumeProcess(IntPtr ProcessId);
 
  private static void SuspendWinLogon()
  {
   Process[] pc = Process.GetProcessesByName("winlogon");
   if (pc.Length > 0)
   {
    ZwSuspendProcess(pc[0].Handle);
   }
  }
 
  private static void ResumeWinLogon()
  {
   Process[] pc = Process.GetProcessesByName("winlogon");
   if (pc.Length > 0)
   {
    ZwResumeProcess(pc[0].Handle);
   }
  }
 }
}

2、Form1类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
 
namespace LockForm
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
 
  private void button1_Click(object sender, EventArgs e)
  {
   if (textBox1.Text == "123")
   {
    Application.ExitThread();
   }
   else
   {
    webBrowser1.Navigate(textBox1.Text);
   }
  }
 
  private void Form1_Load(object sender, EventArgs e)
  {
   //webBrowser1.Navigate("https://baidu.com");
   HookStart();
   //this.TopMost = false;
   //SuspendWinLogon();
  }
 
  private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
  {
   e.Cancel = true;
   webBrowser1.Navigate(webBrowser1.Document.ActiveElement.GetAttribute("href"));
  }
 
  private void button3_Click(object sender, EventArgs e)
  {
   webBrowser1.GoBack();
  }
 
  private void button2_Click(object sender, EventArgs e)
  {
   webBrowser1.GoForward();
  }
 
  private void button4_Click(object sender, EventArgs e)
  {
   webBrowser1.GoHome();
  }
 
  private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  {
   textBox1.Text = webBrowser1.Url.ToString();
  }
 
  private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
  {
 
  }
 
  #region 键盘钩子
 
  public delegate int HookProc(int nCode, int wParam, IntPtr lParam);//定义全局钩子过程委托,以防被回收(钩子函数原型)
  HookProc KeyBoardProcedure;
 
  //定义键盘钩子的相关内容,用于截获键盘消息
  static int hHook = 0;//钩子函数的句柄
  public const int WH_KEYBOARD = 13;
  //钩子结构函数
  public struct KeyBoardHookStruct
  {
   public int vkCode;
   public int scanCode;
   public int flags;
   public int time;
   public int dwExtraInfo;
 
  }
  //安装键盘钩子
  public void HookStart()
  {
 
   if (hHook == 0)
   {
    //实例化一个HookProc对象
    KeyBoardProcedure = new HookProc(Form1.KeyBoardHookProc);
 
    //创建线程钩子
    hHook = Win32API.SetWindowsHookEx(WH_KEYBOARD, KeyBoardProcedure, Win32API.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
 
    //如果设置线程钩子失败
    if (hHook == 0)
    {
     HookClear();
    }
   }
  }
 
  //取消钩子
  public void HookClear()
  {
   bool rsetKeyboard = true;
   if (hHook != 0)
   {
    rsetKeyboard = Win32API.UnhookWindowsHookEx(hHook);
    hHook = 0;
   }
   if (!rsetKeyboard)
   {
    throw new Exception("取消钩子失败!");
   }
  }
  //对截获的键盘操作的处理
  public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
  {
   if (nCode >= 0)
   {
    KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
 
    if (kbh.vkCode == 91)//截获左边WIN键
    {
     return 1;
    }
    if (kbh.vkCode == 92)//截获右边WIN键
    {
     return 1;
    }
    if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control)//截获Ctrl+ESC键
    {
     return 1;
    }
    if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Alt)
    {
     return 1;
    }
    if (kbh.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt)//截获ALT+F4
    {
     return 1;
    }
    if (kbh.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt)//截获ALT+TAB
    {
     return 1;
    }
    if (kbh.vkCode == (int)Keys.Delete&&(int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt)
    {
     return 1;
    }
    if ( kbh.vkCode == (int) Keys.Escape && (int) Control.ModifierKeys == (int) Keys.Control + (int) Keys.Alt ) /* 截获Ctrl+Shift+Esc */
    {
     return 1;
    }
 
   }
 
   return Win32API.CallNextHookEx(hHook, nCode, wParam, lParam);
  }
  #endregion
 }
}

3、声明windows api

?
1
2
3
4
5
6
7
8
9
10
11
//设置钩子
  [DllImport("user32.dll")]
  public static extern int SetWindowsHookEx(int idHook, LockForm.Form1.HookProc lpfn, IntPtr hInstance, int threadID);
 
  //卸载钩子
  [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
  public static extern bool UnhookWindowsHookEx(int idHook);
 
  //调用下一个钩子
  [DllImport("user32.dll")]
  public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

PS:

windows api查询

http://www.pinvoke.net/index.aspx

demo下载

链接:http://pan.baidu.com/s/1jGpOvsE 密码:dbj2

以上就是c# 屏蔽快捷键的实现示例的详细内容,更多关于c# 屏蔽快捷键的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/leestar54/p/4811711.html

延伸 · 阅读

精彩推荐
  • C#C#中Equals方法的常见误解

    C#中Equals方法的常见误解

    equals方法被用来检测两个对象是否相等,即两个对象的内容是否相等。本文主要介绍的是equals方法,初学者对它几个常见的误解,一起来看。...

    C#教程网9932021-10-29
  • C#C#事件订阅发布实现原理详解

    C#事件订阅发布实现原理详解

    这篇文章主要介绍了C#事件订阅发布实现原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考...

    David Huang6722022-10-19
  • C#C#调用微信接口的相关代码

    C#调用微信接口的相关代码

    这篇文章主要为大家详细介绍了C#调用微信接口的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    小熊吉米7992022-01-12
  • C#C# XML中的转义字符操作

    C# XML中的转义字符操作

    这篇文章主要介绍了C# XML中的转义字符操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    长得帅性格好技术屌11672022-10-27
  • C#详解C#中的字符串拼接@ $

    详解C#中的字符串拼接@ $

    这篇文章主要介绍了C#中的字符串拼接@,$的相关知识,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下...

    胜天半子_王二_王半仙4172022-08-07
  • C#C#创建不规则窗体的4种方式详解

    C#创建不规则窗体的4种方式详解

    在这里我们将实现的是C#创建不规则窗体的几种方式,包括自定义窗体,不规则图形等等。希望对大家有所帮助。...

    Alexis10932021-10-29
  • C#C# WinForm-Timer控件的使用

    C# WinForm-Timer控件的使用

    这篇文章主要介绍了C# WinForm-Timer控件的使用,帮助大家更好的理解和学习c# winform,感兴趣的朋友可以了解下...

    野性狼心10562022-10-18
  • C#UnityShader3实现2D描边效果

    UnityShader3实现2D描边效果

    这篇文章主要为大家详细介绍了UnityShader3实现2D描边效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    lyh9168522022-03-11