CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    Detecting if 'Control' or 'Shift' key was down when key pressed

    I have been looking into detecting key stuff, it works great, I am trying to figure out if control or shift was pressed, I read in some MSDN documentation (I use Visual C++), that lParam is 0 if ctrl or shift isn't down, or 1 if they are, but how do I tell which one?

    I may be wrong, it had a table structure saying stuff like:

    0-15 repeat count
    16-18 reserved, do not use

    19-23 = something

    24 = if the right hand shift or control key is down on an extended 101-102 key keyboard.. returns 1 if it is, otherwise returns 0

    My code for keys:
    Code:
    CASE WM_KEYDOWN:
        {
        if (LOWORD(lParam) == 1)
        {
            MessageBox(hWnd, _T("Ctrl Or Shift down"), _T("CAP"), 0);
        }
        int iKey = LOWORD(wParam);
        if (iKey == 37) //LEFT ARROW
        {
            MessageBox(hWnd, _T("LA"), _T("CAP"), 0);
        }
        else if (iKey == 39) //RIGHT ARROW
        {
            MessageBox(hWnd, _T("RA"), _T("CAP"), 0);
        }
        }
    I get the message Ctrl Or Shift down, when I hold either of them down, but I don't if I don't hold them down...

    How do I tell which one was pressed? Thanks

  2. #2
    Join Date
    Aug 2004
    Posts
    294

    Re: Detecting if 'Control' or 'Shift' key was down when key pressed

    Your code does not make much sense to me but if you want to check the state of other keys, such as Ctrl, Alt, or Shift while processing WM_KEYUP/DOWN, you can use GetKeyState API function.
    Boris Karadjov
    Brainbench MVP for Visual C++
    http://www.brainbench.com/

  3. #3
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    Re: Detecting if 'Control' or 'Shift' key was down when key pressed

    Hi Icyculyr,

    I have been reading information about keyboard messages recently, at one point, I was exposed to the topic you are investigating.

    Like Boris K K suggested, you can use GetKeyState( virtual key) to discover if a shift key, such as SHIFT, CTRL, and ALT, is pressed.
    For example,
    Code:
    nRet = GetKeyState ( VK_SHIFT ) ;
    You can obtain a more detailed usage on MSDN GetKeyState () ;

    However, according to the book I was reading, it mentioned a flaw of using GetKeyState. GetKeyState does not check keyboard status in real-time.
    It refects the keyboard status up to and including the current message being processed.
    If this is not what you want, you can also check VK_SHIFT at WM_KEYDOWN

    Hope this helps.

    kab

  4. #4
    Join Date
    Aug 2004
    Posts
    294

    Re: Detecting if 'Control' or 'Shift' key was down when key pressed

    Actually it is not a flow. You are interested if Ctrl was down at the time the other key was pressd, not whether it is down or up at the moment you are processing the message.
    Boris Karadjov
    Brainbench MVP for Visual C++
    http://www.brainbench.com/

  5. #5
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    Re: Detecting if 'Control' or 'Shift' key was down when key pressed

    Hi Boris K K,

    I was confused with GetKeyState after I was reading the book. After reading your post, I guess I am still confused

    Here is my understanding of GetKeyState:
    Let's say we are using GetKeyState to determine if a letter should be displayed as capitalized or not
    Let's say the message queue contains ['a']['p']['p']['shift']['l']['e'], so we know the user types 'appLe'
    If we hold 'shift' key while the message queue is being processed, we will get 'APPLE' instead of 'appLe'

    I believe your statement
    Quote Originally Posted by Boris K K
    not whether it is down or up at the moment you are processing the message.
    is targeting this case I just described.

    The lesson that I got out of this is that "it is not a good practice to use GetKeyState to determine if a letter is capitalized or not."


    Question:
    Could you give me an example of when you will want to use GetKeyState?

    kab

  6. #6
    Join Date
    May 2005
    Posts
    4,948

    Re: Detecting if 'Control' or 'Shift' key was down when key pressed

    You can use ::GetAsyncKeyState().

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  7. #7
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    Re: Detecting if 'Control' or 'Shift' key was down when key pressed

    Hi all, thanks for your input,

    I have read the MSDN documentation on this a little while ago,

    GetKeyState() checks if the key was pressed at the same time as the WM_KEYDOWN message came,


    GetAsyncKeyState() checks if it is still down...

    (As far as I know)

    Thanks alot guys

  8. #8
    Join Date
    Aug 2004
    Posts
    294

    Re: Detecting if 'Control' or 'Shift' key was down when key pressed

    Quote Originally Posted by kabilius
    ...Let's say the message queue contains ['a']['p']['p']['shift']['l']['e'], so we know the user types 'appLe'
    If we hold 'shift' key while the message queue is being processed, we will get 'APPLE' instead of 'appLe'

    I believe your statement
    is targeting this case I just described.

    The lesson that I got out of this is that "it is not a good practice to use GetKeyState to determine if a letter is capitalized or not."
    No, using GetAsyncKeyState (is Shift down right now) may result in incorrect capitalization. GetKeyState (was Shift down when 'l' was pressed; was Shift down when 'e' was pressed, etc.) should be OK.
    Boris Karadjov
    Brainbench MVP for Visual C++
    http://www.brainbench.com/

  9. #9
    Join Date
    Oct 2009
    Posts
    1

    Re: Detecting if 'Control' or 'Shift' key was down when key pressed

    void CMousepresentView::OnDraw(CDC* pDC)
    {
    int shiftValue=::GetKeyState(VK_SHIFT);
    if(!shiftValue)
    pDC->TextOut(0,50,"Shift not pressed");
    else
    pDC->TextOut(0,50,"Shift pressed");

    int ctrlValue=::GetKeyState(VK_CONTROL);
    if(!ctrlValue)
    pDC->TextOut(0,100,"Ctrl not pressed");
    else
    pDC->TextOut(0,100,"Ctrl pressed");
    }
    In this program when I run the program output is
    shift pressed

    ctrl not pressed

    Why "shift pressed" comes even I don't press shift.

  10. #10
    Join Date
    Aug 2009
    Posts
    46

    Re: Detecting if 'Control' or 'Shift' key was down when key pressed

    Like for Ctrl+A, Try
    Code:
     if (GetKeyState(VK_CONTROL) != 1)
    		{
    				switch(wParam)
    				{
    				case 65:
    					//statements
    				}
    		}

  11. #11
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Detecting if 'Control' or 'Shift' key was down when key pressed

    If this is in your OnDraw routine, you'll need to invalidate your drawing area to get a repaint. Try handling WM_KEYDOWN, and call Invalidate() from it's handler.

+ Reply to Thread

Bookmarks

Posting Permissions

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



HTML5 Development Center

Click Here to Expand Forum to Full Width