2017-05-22 94 views
-1

嗨,感謝您花時間閱讀。我的問題是我創建了一個類文件,從研究和發現禁用我的Windows密鑰的一些幫助,我已經實現這個代碼到一個複選框的值如果是true。一旦複選框已被選中,它會運行我創建的類文件,但是我需要禁用此代碼以再次啓用Windows鍵,一旦我取消選中該複選框。我承認這段代碼在我頭腦之上有點多,而且我已經多次閱讀了這段代碼,仍然試圖弄清楚它的結構和工作原理。從禁用Windows密鑰類重新啓用Windows密鑰

class EnableDisableKeys 
{ 
    // Structure contain information about low-level keyboard input event 
    [StructLayout(LayoutKind.Sequential)] 
    private struct KBDLLHOOKSTRUCT 
    { 
     public Keys key; 
     public int scanCode; 
     public int flags; 
     public int time; 
     public IntPtr extra; 
    } 

    //System level functions to be used for hook and unhook keyboard input 
    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern bool UnhookWindowsHookEx(IntPtr hook); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp); 
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr GetModuleHandle(string name); 
    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    private static extern short GetAsyncKeyState(Keys key); 


    //Declaring Global objects 
    private IntPtr ptrHook; 
    private LowLevelKeyboardProc objKeyboardProcess; 

    public void KeyHook() 
    { 
     ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule; 
     objKeyboardProcess = new LowLevelKeyboardProc(captureKey); 
     ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0); 
    } 

    private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp) 
    { 
     if (nCode >= 0) 
     { 
      KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT)); 

      if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin) // Disabling Windows keys 
      { 
       return (IntPtr)1; 
      } 
     } 
     return CallNextHookEx(ptrHook, nCode, wp, lp); 
    } 


} 

而在我的Winform上。

public partial class ParentalSettingsForm : Form 
{ 
    EnableDisableKeys ed = new EnableDisableKeys(); 
    public ParentalSettingsForm() 
    { 

     InitializeComponent(); 
    } 

    private void ParentalSettingsForm_Load(object sender, EventArgs e) 
    { 

    } 

    private void checkBoxDisableWindowsKey_CheckedChanged(object sender, EventArgs e) 
    { 
     if(checkBoxDisableWindowsKey.Checked == true) 
     { 
      ed.KeyHook(); 
     } 

    } 
} 

現在我知道它可能是一個小設置,我需要改變某處禁用,但似乎無法找到它。直到我得到一個答案,我將繼續在這個解決方案上工作,但就像我提到的關於禁用或重新啓用密鑰的事情對我來說完全是新事物。謝謝。

+0

也許這會是一個更好的解決方案? http://stackoverflow.com/questions/32516128/enable-disable-windows-key –

+0

感謝您的答覆,我已經看過這個解決方案,但它需要一個系統重新啓動它發生,我不想這樣。如果可以禁用此代碼中的Win鍵,則必須可以重新啓用該功能,但是要感謝您的建議:) – PaulD1983

回答

1

在你的班級裏設置一個布爾標誌來告訴它是否禁止win鍵。在你的captureKey()方法中檢查它。在這裏,我設置的默認壓制的關鍵:

public bool SuppressWinKey = true; 

    private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp) 
    { 
     if (nCode >= 0) 
     { 
      KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT)); 

      if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin) // Disabling Windows keys 
      { 
       if (SuppressWinKey) 
       { 
        return (IntPtr)1; 
       } 
      } 
     } 
     return CallNextHookEx(ptrHook, nCode, wp, lp); 
    } 

在您的形式,設置掛鉤加載窗體時(不響應複選框):

EnableDisableKeys ed = new EnableDisableKeys(); 

private void ParentalSettingsForm_Load(object sender, EventArgs e) 
{ 
    ed.KeyHook(); 
} 

現在切換該標誌當複選框被改變時:

private void checkBoxDisableWindowsKey_CheckedChanged(object sender, EventArgs e) 
{ 
    ed.SuppressWinKey = checkBoxDisableWindowsKey.Checked; 
}