CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2002
    Location
    DC Metro Area, USA
    Posts
    1,509

    Question Keyboard state on MouseDown?

    I may just be missing a simple call; but how do I find out if the shift, ctrl, and or alt keys are pressed when the mouse is clicked? For example, I may want to click on several objects on the screen, if the shift key is down I want to add the clicked object to the selection, but if not, I want to replace the selection with the newly clicked item. I've got everything I need to do this, but I don't know how to find out if the shift key is down when the click happens (without resorting to win32 api calls).
    bytz
    --This signature left intentionally blank--

  2. #2
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074
    Code:
    if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)

  3. #3
    Join Date
    Sep 2002
    Location
    DC Metro Area, USA
    Posts
    1,509
    Thanks, I appreciate the reply.

    I just found that after searching through reams of code...
    bytz
    --This signature left intentionally blank--

  4. #4
    Join Date
    Mar 2003
    Location
    Japan
    Posts
    120
    Hi,
    I just think if I want to check whether 2 control keys are pressed at the same time.
    how can I do then?

    if ((Control.ModifierKeys & Keys.Shift ) == Keys.Shift)
    the code is just checking one ModifierKeys.
    Should I need to do it in 2 if-loop? or any best way?

    if ((Control.ModifierKeys & Keys.Shift ) == Keys.Shift)
    {
    if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
    .....
    }

    Thanks
    Ee.... Sugoi Ne~~

  5. #5
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074
    Code:
    if ((Control.ModifierKeys & Keys.Shift ) == Keys.Shift)
    This is just checking that at least the Shift key is pressed not just the Shift key pressed.

    So similarly for "Shift + Control" you have a choice of "at least" or "just" those keys pressed.

    Code:
    if ((Control.ModifierKeys & (Keys.Shift | Keys.Control)) == (Keys.Shift | Keys.Control))
    OR
    Code:
    if (Control.ModifierKeys == (Keys.Shift | Keys.Control))

  6. #6
    Join Date
    Sep 2002
    Location
    DC Metro Area, USA
    Posts
    1,509
    I actually prefer to do this:

    Code:
    bool isCtrl = (Control.ModifierKeys & Keys.Control) == Keys.Control;
    bool isShift = (Control.ModifierKeys & Keys.Shift) == Keys.Shift;
    This allows you to ues the values later:
    Code:
     if (isCtrl && isShift)
     {
     }
     else
     {
        if (isShift)
     }
    bytz
    --This signature left intentionally blank--

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