如何在单击鼠标左键时禁用按住按住Alt键,Ctrl键和Shift键

如何在单击鼠标左键时禁用按住按住Alt键,Ctrl键和Shift键,第1张

概述如何在单击鼠标左键时禁用按住/按住Alt键,Ctrl键和Shift键

我试图做一个程序,阻止一些windows热键和类似的function,以帮助用户避免做一个动作,他们可能不想在一个浏览器中运行的Flash程序。

我希望做的程序将是一个.NET C#WinForms程序,作为一个键盘钩子。

目前,它可以阻止按键组合,如Ctrl + W,Alt + F4。 但这些只是“次要function”。 他们正在使用RegisterHotkey方法工作。

我真正想要实现的是,如果可能,以任何方式禁用Ctrl +鼠标左键单击 ,按住Alt +鼠标左键单击 , Shift +鼠标左键单击 。

在.NET中使用NTFS压缩来压缩文件夹

使用Thrift从.NET连接到Cassandra

如何确定当前的windows会话是否被locking?

windows窗体.net框架中的位移控件

如何强制windows重新连接到networking驱动器

实现它的方法还应该最好“解除”它们,并在程序closures时重新启用它们。

这是当前代码的相关片段:

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",1); } } }

我知道这可能与SetwindowsHookEx方法有关,但是我不知道如何使用它来实现它。

如果实现它的最好方法不会与我现有的代码冲突,并且可以和它一起工作。

我也试图确保这个程序可以兼容windows XP的所有版本,如果可能的话,可以同时使用32位和64位。 我在windows 8 Professional 64位计算机上使用Visual Studio 2010 Professional。

我希望这将是足够具体的? 这是我第一次在这里发帖…(尽pipe我已经在过去多次search过这个网站)

(我曾尝试使用“RegisterGlobalHotKey(Keys.Lbutton,USE_CTRL)”,“RegisterGlobalHotKey(Keys.Lbutton,USE_ALT)”和“RegisterGlobalHotKey(Keys.Lbutton,USE_SHIFT)”,但它们根本不起作用。

C# – 如何从静态主要方法调用一个方法

什么是实施托pipe属性处理程序shell扩展的正确方法?

最新单声道在centos上

DataGrIDVIEw CellFormatting事件不会触发

CLR在哪里安装?

我相信我很早以前就已经找到了解决我的问题的办法,也似乎没有人知道如何解决这个问题。

我还没有亲自测试过这个,但是如果我将http://www.codeproject.com/Articles/32556/auto-Clicker-C和下面的代码结合起来使用鼠标点击检测功能&#xFF0C;

[StructLayout(LayoutKind.Sequential)] public struct input { public sendinputEventType type; public KeybdinputUnion mkhi; } [StructLayout(LayoutKind.Explicit)] public struct KeybdinputUnion { [FIEldOffset(0)] public KEYBDinput ki; } [StructLayout(LayoutKind.Sequential)] public struct KEYBDinput { public ushort wVk; public ushort wScan; public uint DWFlags; public uint time; public IntPtr DWExtraInfo; } public enum sendinputEventType : int { inputKeyboard } public enum KeyCode : ushort { /// <summary> /// Right shift /// </summary> RSHIFT = 0xa1,/// <summary> /// Shift key /// </summary> SHIFT = 0x10,/// <summary> /// Right control /// </summary> RCONTRol = 0xa3,/// <summary> /// left control /// </summary> LCONTRol = 0xa2,/// <summary> /// left shift /// </summary> LSHIFT = 160,/// <summary> /// Ctlr key /// </summary> CONTRol = 0x11,/// <summary> /// Alt key /// </summary> ALT = 18,} [Dllimport("User32.dll",SetLastError = true)] static extern uint sendinput(uint ninputs,ref input pinputs,int cbSize); voID YourMethodHerelikeTimerTickOrWE() { if (Control.ModifIErKeys == Keys.Shift) { SendKeyUp(KeyCode.SHIFT); } if (Control.ModifIErKeys == Keys.Alt) { SendKeyUp(KeyCode.ALT); } if (Control.ModifIErKeys == Keys.Control) { SendKeyUp(KeyCode.CONTRol); } if (Control.ModifIErKeys == (Keys.Control | Keys.Shift)) { SendKeyUp(KeyCode.CONTRol); SendKeyUp(KeyCode.SHIFT); } if (Control.ModifIErKeys == (Keys.Control | Keys.Alt)) { SendKeyUp(KeyCode.CONTRol); SendKeyUp(KeyCode.ALT); } if (Control.ModifIErKeys == (Keys.Alt | Keys.Shift)) { SendKeyUp(KeyCode.ALT); SendKeyUp(KeyCode.SHIFT); } if (Control.ModifIErKeys == (Keys.Alt | Keys.Shift | Keys.Control)) { SendKeyUp(KeyCode.ALT); SendKeyUp(KeyCode.SHIFT); SendKeyUp(KeyCode.CONTRol); } } public static voID SendKeyUp(KeyCode keyCode) { input input = new input { type = sendinputEventType.inputKeyboard,}; input.mkhi.ki = new KEYBDinput(); input.mkhi.ki.wVk = (ushort)keyCode; input.mkhi.ki.wScan = 0; input.mkhi.ki.DWFlags = 2; input.mkhi.ki.time = 0; input.mkhi.ki.DWExtraInfo = IntPtr.Zero; //input[] inputs = new input[] { input }; if (sendinput(1,ref input,Marshal.SizeOf(typeof(input))) == 0) throw new Exception(); }

这应该是理论上的工作。 到目前为止,我已经单独使用了两个部分,没有任何问题。

此外,从CodeProject的代码,为一个特定的方法调用

private static voID EnsureSubscribedToGlobalMouseEvents()

在那里有一行代码必须被改变,才能在C#NET 4 Framework中正确使用。

即代码

s_MouseHookHandle = SetwindowsHookEx( WH_MOUSE_LL,s_MouseDelegate,Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().Getmodulees()[0]),0);

应该改成这样:

s_MouseHookHandle = SetwindowsHookEx( WH_MOUSE_LL,Loadlibrary("user32.dll"),0);

为了使用Loadlibrary,你需要在下面调用这个P / Invoke:

[Dllimport("kernel32",SetLastError = true,CharSet = CharSet.Ansi)] static extern IntPtr Loadlibrary([MarshalAs(UnmanagedType.LPStr)]string lpfilename);

参考文献:

http://www.pinvoke.net/default.aspx/kernel32/Loadlibrary.HTML

Gma.UserActivityMonitor&SetwindowsHookEx错误126

http://www.codeproject.com/Articles/32556/auto-Clicker-C

几个我自己的项目

如果你认为你有更好的答案,请提供一个,因为我非常想看到它:)

总结

以上是内存溢出为你收集整理的如何在单击鼠标左键时禁用按住/按住Alt键,Ctrl键和Shift键全部内容,希望文章能够帮你解决如何在单击鼠标左键时禁用按住/按住Alt键,Ctrl键和Shift键所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1274138.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存