我想了解一個代碼,它阻止了鍵盤上的一些鍵,並且只允許一些退出路徑,與普通方法相比。擊鍵處理:理解IntPtr返回值
我已經能夠獲得它的大部分,但是這個部分我們實際上是在處理這個函數中的擊鍵。 下面的代碼:
public static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
bool Alt = (WinForms.Control.ModifierKeys & Keys.Alt) != 0;
bool Control = (WinForms.Control.ModifierKeys & Keys.Control) != 0;
//Prevent ALT-TAB and CTRL-ESC by eating TAB and ESC. Also kill Windows Keys.
int vkCode = Marshal.ReadInt32(lParam);
Keys key = (Keys)vkCode;
if (Alt && key == Keys.F4)
{
Application.Current.Shutdown();
return (IntPtr)1; //handled
}
if (key == Keys.LWin ||key == Keys.RWin) return (IntPtr)1; //handled
if (Alt && key == Keys.Tab) return (IntPtr)1; //handled
if (Alt && key == Keys.Space) return (IntPtr)1; //handled
if (Control && key == Keys.Escape)return (IntPtr)1;
if (key == Keys.None) return (IntPtr)1; //handled
if (key <= Keys.Back) return (IntPtr)1; //handled
if (key == Keys.Menu) return (IntPtr)1; //handled
if (key == Keys.Pause) return (IntPtr)1; //handled
if (key == Keys.Help) return (IntPtr)1; //handled
if (key == Keys.Sleep) return (IntPtr)1; //handled
if (key == Keys.Apps) return (IntPtr)1; //handled
if (key >= Keys.KanaMode && key <= Keys.HanjaMode) return (IntPtr)1; //handled
if (key >= Keys.IMEConvert && key <= Keys.IMEModeChange) return (IntPtr)1; //handled
if (key >= Keys.BrowserBack && key <= Keys.BrowserHome) return (IntPtr)1; //handled
if (key >= Keys.MediaNextTrack && key <= Keys.OemClear) return (IntPtr)1; //handled
Debug.WriteLine(vkCode.ToString() + " " + key);
}
return InterceptKeys.CallNextHookEx(_hookID, nCode, wParam, lParam);
}
是什麼賦予boolean
值與&
符號爲Alt這一行WinForms.Control.ModifierKeys & Keys.Alt
是什麼意思?
我們已經處理了按鍵,但是關於返回1的IntPtr
是什麼意思?
我早先在我的一個應用程序中使用了e.KeyChar == Keys.Alt在Keypress事件中。這有什麼區別嗎? – Cipher 2012-02-11 08:17:16
@Cipher:這是一種不同的用法,因爲當按下Alt鍵時你會捕捉到,而'ModifierKeys'屬性包含當前按下的修飾鍵。 – Guffa 2012-02-11 12:14:44