Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / April 2005

Tip: Looking for answers? Try searching our database.

"to determine which modifier key was pressed" question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SolarCoder - 08 Apr 2005 16:27 GMT
In the .NET 2003 docs, it says to use the following code for an event handler
to determine which modifier key was pressed when an event is fired:

Private Sub Button1_KeyPress(ByVal sender As Object, ByVal e As _
System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress
  If (Control.ModifierKeys And Keys.Shift) = Keys.Shift Then
     MessageBox.Show("Pressed " & Keys.Shift)
  End If
End Sub

My question: Why did MS elect to use a shared member of the Control class
(Control.ModifierKeys) instead of adding a member of the same name to the
KeyPressEventArgs class to hold this information for the time the event was
fired? To me, this approach would have been more encapsulated. More
importantly, won't Control.ModifierKeys hold incorrect information if the
user changes the state of Control.ModifierKeys before the event handler
fires? Unlikely sure, but I would think it is possible.

Any insight would be appreciated.
Thanks!
SolarCoder
Morten Wennevik - 08 Apr 2005 18:38 GMT
Hi SolarCoder,

The Control.ModifierKeys is useful when dealing with any events,  
especially mouse events, and not just key events.  Since KeyPress isn't  
actually a key event but more like a character event I suppose they opted  
to not have a ModifierKeys property inside the KeyPress event since you  
can just as well use Control.ModifierKeys.

Signature

Happy Coding!
Morten Wennevik [C# MVP]

AMercer - 08 Apr 2005 20:15 GMT
You are correct, and your observation applies to mouse up/down events where
access to modifier keys is also by shared Control.ModifierKeys.  In practice,
I have not been inconvenienced by this MS oversight.

> In the .NET 2003 docs, it says to use the following code for an event handler
> to determine which modifier key was pressed when an event is fired:
[quoted text clipped - 17 lines]
> Thanks!
> SolarCoder
SolarCoder - 11 Apr 2005 20:09 GMT
Thanks to Morten Wennevik & AMercer!

Looks like this is one I will have to put in the "I don't why they did it
that way" category, but it is something I can certainly live with as long as
the behavior is deterministic.

Just one of those things that bugged me more than my code.
Thanks!
-SC

> You are correct, and your observation applies to mouse up/down events where
> access to modifier keys is also by shared Control.ModifierKeys.  In practice,
[quoted text clipped - 21 lines]
> > Thanks!
> > SolarCoder

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.