How to sense shift being held?

  • Thread starter Thread starter Daniel Carlsson
  • Start date Start date
D

Daniel Carlsson

Hello

I have a bit of a problem, Im building my own component which shows a
specialized list of data. Its used to allow you to select patterns from the
data. Each cell is its own control and stored in the parent control.

The problem is that I want to be able to select an area when shift is held
and you click one of the cells. And I cant find any information of how you
can sense the state of the keyboard, or the shift button. Using OnKeyDown/Up
might work if I make all the cells and the container components trigger on
those, but if the user presses shift when the component is not focused I
dont think it will work, and Im not so sure shift triggers unless you press
another key anyways (havent checked that yet).

Does anyone have any tips of how I can check if shift is being pressed when
the user clicks a component?

Thanks in advance,
Dan
 
Daniel,

I had just posted a different question regarding the keyboard when I saw
your post.

If you register a KeyUp/KeyDown event you will receive the events for the
SHIFT key even if it is the only key pressed. I'm not sure how to handle
situations when your control is not in focus. Maybe you could listen for
keyboard events from you main window. Upon a SHIFT press, figure out if the
mouse pointer is hovering over on of your controls and go from there.

Why do you need to hold the shift key AND use the mouse? Why not just handle
mouse clicks? I guess one reason is that you have multiple behaviors: one
behavior for normal mouse clicks and also one or more other things that
might happen to one of your controls. If that's the case, maybe you could
use a right mouse click. If someone right-clicks on your control, pop-up an
menu with all of the different things that can happen.

-cwineman
 
Yes you can select areas by holding down shift, or will be able to,
hopefully.

A context menu could work in worst case I suppose, but it would slow down
the users.

Im going to test how it works if I put key listeners on all of the special
components subcomponents tomorrow. Perhaps it will work well enough. Thanks
for the input.

/Dan
 
Hi Daniel,

Based on my understanding, you want to determine the child control's shift
key state when this control is clicked.

For this, we may hook child control's MouseDown event, in this event, use
Control.ModifierKeys property to determine the shift key state. Event the
child control do not have focus before you clicked, it can determine the
shift key state. Code like this:
private void textBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(Control.ModifierKeys==Keys.Shift)
{
Console.WriteLine("You pressed shift key!");
}
}
If I misunderstand you, please feel free to point out. Thanks

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
You are welcome! If you need further help, please feel free to post in this
group, I will work with you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top