2014-03-28 67 views
1

我想製作一個程序來阻止某些Windows熱鍵和類似的功能,以幫助用戶避免做一個他們可能不想在瀏覽器中運行的Flash程序中做出的動作。如何在單擊鼠標左鍵時禁用按住Alt鍵,控制鍵和Shift鍵

我想做的程序將是一個.NET C#WinForms程序,它充當鍵盤鉤子。

目前,它可以阻止按鍵組合,如Ctrl + W,Alt + F4。但這些只是「次要功能」。他們正在使用RegisterHotkey方法工作。

我真的想實現的,是能夠禁用,如果可能的話,以任何方式,在保持按Ctrl +鼠標左鍵向下點擊,按住ALT +鼠標左鍵點擊 ,和Shift +鼠標左鍵點擊

實現它的方法還應該最好「解除」它們並在程序關閉時再次啓用它們。

下面是當前的代碼的相關片段:

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的所有版本兼容,如果可能的話,對於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)「,但他們根本不工作。)

+0

在全系統級別執行此操作的目的是什麼?你提到你不希望它發生在瀏覽器窗口中,所以如果這是針對某種類型的,例如亭式應用程序,爲什麼不直接對瀏覽器進行修改(在檢查開源許可之後瀏覽器,當然)。 – icabod

+0

那麼,它將允許瀏覽器像他們希望的任何頁面和瀏覽器上的自助終端風格的應用程序,我不希望修改任何瀏覽器代碼,以免我搞砸了。我希望使用戶能夠用自己喜歡的瀏覽器運行它,並通過錯誤地按下某些按鈕來避免犯錯。我不想強制用戶使用自定義瀏覽器,可能不符合他們的喜好。 – Kaitlyn

回答

1

我相信我終於找到了解決我的問題很久以前,也因爲它似乎沒有人知道如何解決它。

我沒有測試此我本人還沒有,但如果我結合鼠標點擊檢測功能,從http://www.codeproject.com/Articles/32556/Auto-Clicker-C,一起用下面的代碼...

[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(); 
} 

應該theorectically工作。到目前爲止,我已經單獨使用了兩部分,而沒有任何問題。

此外,對於從CodeProject代碼,用於一個特定的方法稱爲

private static void EnsureSubscribedToGlobalMouseEvents() 

有代碼在那裏的線必須被改變爲C#NET適當地使用4個構架和正確。

也就是說,代碼

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

應改爲這樣:

s_MouseHookHandle = SetWindowsHookEx(
    WH_MOUSE_LL, 
    s_MouseDelegate, 
    LoadLibrary("user32.dll"), 
    0); 

爲了使用調用LoadLibrary,你需要調用這個P /以下調用:

[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)] 
static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName); 

參考文獻:

  1. http://www.pinvoke.net/default.aspx/kernel32/LoadLibrary.html
  2. Gma.UserActivityMonitor & SetWindowsHookEx error 126
  3. http://www.codeproject.com/Articles/32556/Auto-Clicker-C
  4. 幾個我自己的項目的

如果你認爲你有更好的回答,請提供一個,因爲我非常希望看到它: )

相關問題