この投稿への参照。RegisterHotKey APIコールを使用すると、なぜそれが修飾子として1,2,4、および8を受け入れるのですか?
http://www.pinvoke.net/default.aspx/user32/RegisterHotKey.html
#region fields
public static int MOD_ALT = 0x1;
public static int MOD_CONTROL = 0x2;
public static int MOD_SHIFT = 0x4;
public static int MOD_WIN = 0x8;
public static int WM_HOTKEY = 0x312;
#endregion
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private static int keyId;
public static void RegisterHotKey(Form f, Keys key)
{
int modifiers = 0;
if ((key & Keys.Alt) == Keys.Alt)
modifiers = modifiers | WindowsShell.MOD_ALT;
if ((key & Keys.Control) == Keys.Control)
modifiers = modifiers | WindowsShell.MOD_CONTROL;
if ((key & Keys.Shift) == Keys.Shift)
modifiers = modifiers | WindowsShell.MOD_SHIFT;
Keys k = key & ~Keys.Control & ~Keys.Shift & ~Keys.Alt;
Func ff = delegate()
{
keyId = f.GetHashCode(); // this should be a key unique ID, modify this if you want more than one hotkey
RegisterHotKey((IntPtr)f.Handle, keyId, modifiers, (int)k);
};
f.Invoke(ff); // this should be checked if we really need it (InvokeRequired), but it's faster this way
}
私の質問は、どのようにRegisterHotKey
APIは1
、2
、4
、8
は、Windowsキーがあることを知っているんですか? ctrl、shift、およびmenu(alt)のキーコードはキーの値が全く異なるため、そして、まさにそれがためにチェックしていますRegisterHotKey
機能で起こっている:
if ((key & Keys.Control) == Keys.Control)
modifiers = modifiers | WindowsShell.MOD_CONTROL;
、それはここで何をしているのですか?など
Keys k = key & ~Keys.Control & ~Keys.Shift & ~Keys.Alt;
を使用するのと同じやり方で、この作品? [フラグ()] パブリック列挙KeyModifiers最も私を混乱させる {なし= 0、 アルト= 1、 コントロール= 2、 シフト= 4、 のWindows = 8 } 部分が改質であります引数。 fsModifiersは0x2 == CTRLキーをどのように知っていますか?それは内部機能ですか? DLLのAPIから? –
はい、APIの内部にあります。 –
はい、それは同じように動作するはずです。 –