You are here:

C++/Problems adding shift key to left mouse button function

Advertisement


Question
QUESTION: Hi again, Zlatko,

I'm creating a graph with Visual C++ 6 that both analyzes and changes certain parts of a file.

I've found a way to use the left mouse button to make the changes to the graph, (OnLButtonDown) but I'd like to also use the Shift key in conjunction with the left mouse button to avoid mistakes.

Looking through MSDN, I've found: OnLButtonDown(UINT MK_SHIFT, CPOINT point) is suggested but draws errors. Using OnLButtonDown(UINT mk_shift, CPOINT point) with small type, draws no errors but is ineffective since the operation still works without the shift key.

In every reference to OnLButtonDown (and OnLButtonMove and OnLButtonUp) I've added UINT mk_shift, though I haven't added any code for the shift key within the function itself other than the OnLButtonDown(mk_shift, point) at the end of each function. Could you give me an example of some code to add to the function that would help me with my problem?

Thanks in advance for reading, analizing and answering my question,
-Neil

ANSWER: Hello Neil.

The MK_SHIFT in all caps is defined as a macro. That is why you need an alternate case. Are you sure you have code to examine the value of mk_shift and take action based on its value ? I'm guessing you assumed that by calling your variable MK_SHIFT that the OnLButtonDown would accept clicks only when the shift is pressed.

You need code like this in your OnLButtonDown

if (mk_shift == MK_SHIFT) // Do shift part
else if (mk_shift == MK_CONTROL) // Do ctrl part
and so on.

It makes more sense to rename mk_shift to "flags", like in
http://msdn.microsoft.com/en-us/library/c293as9k%28VS.80%29.aspx

---------- FOLLOW-UP ----------

QUESTION: Hi again, Zlatko,

Sadly, your suggestion:
...::OnLButtonDown(UINT flags, CPoint point)
  if( flags==MK_SHIFT )
  {
   ...
  didn't seem to work.

Placing the cursor on the line 'if( flags==MK_SHIFT )' and using 'debug: Run To Cursor', I started my program.

Clicking my mouse on the graph while holding down either shift key successfully stopped the program at that line; clicking (debug)'Step Into' made the cursor pass all the way through the code and stop at the last line of the function, 'CWnd::OnLButtonDown( flags, point ).

I did see that 'flags' had a integer value of '5' while the cursor was on the 'if( flags==MK_SHIFT )' line.

Whereas when when I changed your code to 'if( flags=MK_SHIFT )' to get the actual value of 'MK_SHIFT', flags had a integer value of '4', which explains why it didn't work.

I could get the line to work if I changed the code to: 'if( flags==(MK_SHIFT+1) )' but that just didn't seem right...

Can you please suggest where I went wrong?

Thanks in advance for reading, analizing and answering my question,
-Neil

Answer
Hello Neil.

Sorry for my error, the flags is a bit field, meaning you need to look at individual bits to get the information you're interested in. Below is sample code. I could not get the "ALT" key to be noticed. I don't know why. The MK_LBUTTON bit is always on, because its the OnLButtonDown function. That's what led you to MK_SHIFT+1

void CMessageBoxDlg::OnLButtonDown(UINT flags, CPoint point)
{
   CString text;
   text.Format("OnLButtonDown %d ", flags);

   if (flags & MK_SHIFT)
   {
       text.Append("SHIFT ");
   }
   if (flags & MK_CONTROL)
   {
       text.Append("CTRL ");
   }
   if (flags & MK_ALT)
   {
       text.Append("ALT ");
   }
   if (flags & MK_LBUTTON)
   {
       text.Append("MK_LBUTTON ");
   }
   MessageBox(text);
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.