CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2010
    Posts
    7

    Question What is this SHIFTED ?

    #define SHIFTED 0x8000

    .....some long-and-hard-to-understand code.....
    //Message processing
    if(GetKeyState(VK_SHIFT) & SHIFTED)
    {
    //dosomething
    }


    Someone please tell me what is that SHIFTED's role in the if condition ?

    Thank you

  2. #2
    Join Date
    Jul 2009
    Posts
    154

    Re: What is this SHIFTED ?

    GetKeyState returns an unsigned short (=16 bits)

    The high order bit is set to 1 if the key is being pressed, the return value would be 0x8000
    ... and 0x8000 & 0x8000 = 0x8000 which would make the if statement TRUE

    And the function also has the low order bit 1 if the key is toggled 1 or 0. But that value is ignored with this "& SHIFTED" part, so the if statement is only true if the key is pressed, not toggled.

    So if the key is toggled but not pressed its 0x0001 & 0x8000 = 0x0000 (FALSE)
    If the key is toggled and pressed its 0x8001 & 0x8000 = 0x8000 (TRUE)

    See http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    Atleast that's how I see it

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: What is this SHIFTED ?

    In the examples quoted, SHIFTED is defined as 0x8000 and is used with the return value of GetKeyState(). GetKeyState() returns a SHORT (16 bit) so 0x8000 references the high order bit which is set when the key is pressed.

    IMO a better name than SHIFTED would be PRESSED. Tests for shift, ctrl etc can then be done something like (not tried)
    Code:
    #define PRESSED 0x8000
    
    if (GetKeyState(VK_SHIFT) & PRESSED) 
    //Shift key pressed
    
    if (GetKeyState(VK_CONTROL) & PRESSED)
    //Control key pressed
    ...
    See https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx for more details.

    The possible virtual keys that can be tested are

    VK_LSHIFT
    VK_RSHIFT
    VK_LCONTROL
    VK_RCONTROL
    VK_LMENU
    VK_RMENU
    VK_SHIFT
    VK_CONTROL
    VK_MENU
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    May 2014
    Posts
    205

    Re: What is this SHIFTED ?

    Yes, thank you. I read more:

    "In all four of the keyboard messages discussed so far, the wParam parameter contains the virtual-key code of the key. The lParam parameter contains some miscellaneous information packed into 32 bits. You typically do not need the information in lParam. One flag that might be useful is bit 30, the "previous key state" flag, which is set to 1 for repeated key-down messages."
    https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx
    Good link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured