Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 162893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:28:17+00:00 2026-05-11T11:28:17+00:00

I reference to this post; http://www.pinvoke.net/default.aspx/user32/RegisterHotKey.html #region fields public static int MOD_ALT = 0x1;

  • 0

I reference to this post;

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     } 

My question is, how does RegisterHotKey api know that 1, 2, 4, 8 are windows keys? Because the key codes for ctrl, shift, and menu (alt) give back totally different values for the keys? And what exactly is going on in the RegisterHotKey function where it’s checking for:

if ((key & Keys.Control) == Keys.Control)                 modifiers = modifiers | WindowsShell.MOD_CONTROL; 

and what is it doing here?

Keys k = key & ~Keys.Control & ~Keys.Shift & ~Keys.Alt; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:28:18+00:00Added an answer on May 11, 2026 at 11:28 am

    MOD_ALT, MOD_CONTROL, etc don’t have any relationship to the keycodes of the associated keys.

    You’re seeing an enum type used to represent a set of flags. This is a particularly compact way of representing states that represent combinations of things (like modifier keys being simultaneously pressed, file access permissions, etc)

    When enums are used this way, each bit of a variable of the enumeration type can be used to indicate that a particular ‘flag’ is set.

    // Note that powers of 2 are used; each value has only a single bit set public static int MOD_ALT = 0x1;     // If bit 0 is set, Alt is pressed public static int MOD_CONTROL = 0x2; // If bit 1 is set, Ctrl is pressed public static int MOD_SHIFT = 0x4;   // If bit 2 is set, Shift is pressed  public static int MOD_WIN = 0x8;     // If bit 3 is set, Win is pressed  // If we wanted to represent a combination of keys: int altAndControl = MOD_ALT | MOD_CONTROL; // == 3 int controlAndShift = MOD_CONTROL | MOD_SHIFT; // == 6 

    This has two advantages:

    • The API doesn’t have to take an individual boolean parameter for every modifier key
    • The API can be expanded to include additional modifier keys without changing the interface

    Bitwise &s and |s can be used to determine which flags are set in a value, and to set or unset a flag in a value.

    The code you’ve asked about does exactly this:

    if ((key & Keys.Control) == Keys.Control)     modifiers = modifiers | WindowsShell.MOD_CONTROL 

    is saying ‘If the key has the Control bit set, then set the control bit in modifiers‘

    Keys k = key & ~Keys.Control & ~Keys.Shift & ~Keys.Alt; 

    is saying ‘k is assigned key with the Control, Shift and Alt flags cleared’

    I’m not sure why the contributors to pinvoke chose to use constants; you can just as easily use a proper enum:

    [Flags] public enum Modifiers {    None = 0,    Alt = 1,    Control = 2,    // ... } 

    My answer to a similar question has some more details on how flags work, and more examples.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to this http://www.cplusplus.com/reference/clibrary/csignal/signal.html SIGINT is generally used/cause by the user. How do i
This post reference to the One Definition Rule. Wikipedia is pretty bad on explaining
With the reference of this answer , what is Theta (tight bound)? Omega is
I'm writing this question with reference to this one which I wrote yesterday. After
I'm currently creating an explicit reference to this in the outer class so that
Comment on Duplicate Reference: Why would this be marked duplicate when it was asked
This is in reference to my other question Auto Clearing Textbox . If I
This is in reference to a question I asked earlier. Aside from viewing the
This is in reference to the question previously asked The problem here is, each
This question builds off of a previously asked question: Pass by reference multidimensional array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.