【发布时间】:2026-01-22 05:25:01
【问题描述】:
我正在尝试制作一个阻止某些 Windows 热键和类似功能的程序,以帮助用户避免在浏览器中运行的 flash 程序中执行他们可能不想执行的操作。
我希望制作的程序是一个 .NET C# WinForms 程序,它充当键盘挂钩。
目前,它可以屏蔽 Ctrl+W、Alt+F4 等组合键。但这些只是“次要功能”。他们正在使用 RegisterHotkey 方法工作。
我真正想要实现的是,如果可能的话,能够以任何方式禁用 Ctrl+鼠标左键单击,同时按住 Alt +鼠标左键单击和Shift+鼠标左键单击。
实现它的方法也应该最好“解开”它们并在程序关闭时再次启用它们。
这里是当前代码的相关sn-p:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace HotKeyBlocker
{
public partial class HotkeyBlocker : Form
{
//Credits to: http://www.codeproject.com/KB/cs/Kiosk_CS.aspx?display=Print
//And: http://support.microsoft.com/kb/318804
#region Dynamic Link Library Imports for Hotkeys
[DllImport("user32.dll")]
private static extern int FindWindow(string cls, string wndwText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int cmd);
[DllImport("user32.dll")]
private static extern long SHAppBarMessage(long dword, int cmd);
[DllImport("user32.dll")]
private static extern int RegisterHotKey(IntPtr hwnd, int id, int
fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern int UnregisterHotKey(IntPtr hwnd, int id);
#endregion
#region Modifier Constants and Variables
// Constants for modifier keys
private const int USE_NONE = 0;
private const int USE_ALT = 1;
private const int USE_CTRL = 2;
private const int USE_SHIFT = 4;
private const int USE_WIN = 8;
// Hot key ID tracker
short mHotKeyId = 0;
#endregion
public HotkeyBlocker()
{
InitializeComponent();
// Related browser window key combinations
// -- Some things that you may want to disable --
//CTRL+A Select all
//CTRL+B Organize favorites
//CTRL+C Copy
//CTRL+F Find
//CTRL+H View history
//CTRL+L Open locate
//CTRL+N Open new browser window
//CTRL+O Open locate
//CTRL+P Print
//CTRL+R Refresh
//CTRL+S Save
//CTRL+V Paste
//CTRL+W Close
//CTRL+X Cut
//ALT+F4 Close
// Disable ALT+F4 - exit
RegisterGlobalHotKey(Keys.F4, USE_ALT);
// Disable CTRL+F4 - close tab
RegisterGlobalHotKey(Keys.F4, USE_CTRL);
// Disable CTRL+W - exit
RegisterGlobalHotKey(Keys.W, USE_CTRL);
// Disable CTRL+N - new window
RegisterGlobalHotKey(Keys.N, USE_CTRL);
// Disable CTRL+S - save
RegisterGlobalHotKey(Keys.S, USE_CTRL);
// Disable CTRL+A - select all
RegisterGlobalHotKey(Keys.A, USE_CTRL);
// Disable CTRL+C - copy
RegisterGlobalHotKey(Keys.C, USE_CTRL);
// Disable CTRL+X - cut
RegisterGlobalHotKey(Keys.X, USE_CTRL);
// Disable CTRL+V - paste
RegisterGlobalHotKey(Keys.V, USE_CTRL);
// Disable CTRL+B - organize favorites
RegisterGlobalHotKey(Keys.B, USE_CTRL);
// Disable CTRL+F - find
RegisterGlobalHotKey(Keys.F, USE_CTRL);
// Disable CTRL+H - view history
RegisterGlobalHotKey(Keys.H, USE_CTRL);
// Disable CTRL+P - print
RegisterGlobalHotKey(Keys.P, USE_CTRL);
// Disable CTRL+Tab - tab through browser tabs
RegisterGlobalHotKey(Keys.Tab, USE_CTRL);
// Disable CTRL+T - new browser tab
RegisterGlobalHotKey(Keys.T, USE_CTRL);
// Disable CTRL+O - open
RegisterGlobalHotKey(Keys.O, USE_CTRL);
// Disable CTRL+D - Bookmarks
RegisterGlobalHotKey(Keys.D, USE_CTRL);
// Disable ALT+Esc - tab through open applications
RegisterGlobalHotKey(Keys.Escape, USE_ALT);
// Disable F1 Key - help in most applications
RegisterGlobalHotKey(Keys.F1, USE_NONE);
// Disable ALT+Tab - tab through open applications
//RegisterGlobalHotKey(Keys.Tab, USE_ALT); <-- Does not work on W8
// hide the task bar - not a big deal, they can
// still CTRL+ESC to get the start menu; for that
// matter, CTRL+ALT+DEL also works; if you need to
// disable that you will have to violate SAS and
// monkey with the security policies on the machine
//ShowWindow(FindWindow("Shell_TrayWnd", null), 0);
}
private void RegisterGlobalHotKey(Keys hotkey, int modifiers)
{
try
{
// increment the hot key value - we are just identifying
// them with a sequential number since we have multiples
mHotKeyId++;
if (mHotKeyId > 0)
{
// register the hot key combination
if (RegisterHotKey(this.Handle, mHotKeyId, modifiers,
Convert.ToInt16(hotkey)) == 0)
{
// tell the user which combination failed to register
// this is useful to you, not an end user; the user
// should never see this application run
MessageBox.Show("Error: " +
mHotKeyId.ToString() + " - " +
Marshal.GetLastWin32Error().ToString(),
"Hot Key Registration");
}
}
}
catch
{
// clean up if hotkey registration failed -
// nothing works if it fails
UnregisterGlobalHotKey();
}
}
private void UnregisterGlobalHotKey()
{
// loop through each hotkey id and
// disable it
for (int i = 0; i < mHotKeyId; i++)
{
UnregisterHotKey(this.Handle, i);
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// if the message matches,
// disregard it
const int WM_HOTKEY = 0x312;
if (m.Msg == WM_HOTKEY)
{
// Ignore the request or each
// disabled hotkey combination
}
}
private void HotkeyBlocker_FormClosed(object sender, FormClosedEventArgs e)
{
// unregister the hot keys
UnregisterGlobalHotKey();
// show the taskbar - does not matter really
//ShowWindow(FindWindow("Shell_TrayWnd", null), 1);
}
}
}
我知道它可能与一个名为 SetWindowsHookEx 的方法有关,但如果可以的话,我不知道如何使用它来实现它。
如果实现它的最佳方法不会与我拥有的现有代码冲突,并且可以与它一起工作,那将是最好的。
我还试图确保该程序可以与 Windows XP 及更高版本的所有 Windows 版本兼容,如果可能的话,包括 32 位和 64 位。我在 Windows 8 Professional 64 位计算机上使用 Visual Studio 2010 Professional。
我希望这足够具体吗?这是我第一次在这里发帖......(虽然我过去曾多次搜索过这个网站)
(我尝试过使用“RegisterGlobalHotKey(Keys.LButton, USE_CTRL)”、“RegisterGlobalHotKey(Keys.LButton, USE_ALT)”和“RegisterGlobalHotKey(Keys.LButton, USE_SHIFT)”,但它们根本不起作用。)
【问题讨论】:
-
在系统范围内这样做的目的是什么?您提到您不希望它发生在浏览器窗口中,所以如果这是针对某种信息亭式应用程序,为什么不直接对浏览器进行修改(在检查开源许可之后当然是浏览器)。
-
好吧,它将允许浏览器在他们希望的任何页面和浏览器上像一个信息亭风格的应用程序一样,我不想修改任何浏览器代码,以免我搞砸了。我希望这样做,以便用户可以使用他们最喜欢的浏览器运行它,并允许他们通过误按某些按钮来避免犯任何错误。我不想强迫用户使用他们可能不喜欢的自定义浏览器。