Click here to Skip to main content
Licence 
First Posted 14 Feb 2003
Views 46,235
Bookmarked 15 times

KeyState Struct Objectifies KeyState Property

By | 14 May 2003 | Article
A KeyState struct to objectify the KeyState property of DragEventArgs.

Introduction

In order to make programs more readable and therefore more maintainable, I am always on the lookout for ways to hide messy implementation details. While adding drag and drop functionality to a TreeView, I found such an opportunity. The DragEventArgs (or QueryContinueDragEventArgs) parameter to the OnDrag... event methods, exposes the state of the control keys and mouse buttons as bits packed into a hard-working int. While this is a clever way to pack a lot of info into one property, cleverness seldom wins maintainability awards. Unless one programs these events often or has a prodigious memory, they will not be able to remember which bit is the Shift bit or middle mouse button bit. This led me to write a small struct to encapsulate this naked bit-bashing and add what probably should have been included in the framework.

Background

I won't bore the reader with which bit is which (say that three times fast: which bit's which) a quick search of the help file for KeyState will yield that. This KeyState struct does what would normally be done in event code:

protected override void OnDragOver(DragEventArgs e)
    // If the Shift key is pressed...
    if (e.KeyState & 4)
    {
        ...
    }

And removes those details to the struct innards, to be replaced with code which is more self-documenting:

protected override void OnDragOver(DragEventArgs e)
    KeyState keyState = new KeyState (e.KeyState)
    if (keyState.Shift)
    {
       ...
    }

Or, the static methods (overloaded for both DragEventArgs and QueryContinueDragEventArgs may be employed:

protected override void OnDragOver(DragEventArgs e)
    if (KeyState.IsShiftPressed (e))
    {
        ...
    }

Using the code

The struct is a snap to use. It receives an integer in its constructor which is a KeyState property from a DragEventArgs object. Then all the key states packed into the value are exposed as properties.

protected override void OnDragOver(DragEventArgs e)
{
    KeyState keyState = new KeyState (e.KeyState);

    if (keyState.Shift)
        e.AllowedEffect = DragDropEffects.Move;
    else if (keyState.Ctrl)
        e.AllowedEffect = DragDropEffects.Copy;
    else
        e.AllowedEffect = DragDropEffects.Scroll;
}

Alternatively, using the static methods:

protected override void OnDragOver(DragEventArgs e)
{
    if (KeyState.IsShiftPressed (e))
        e.AllowedEffect = DragDropEffects.Move;
    else if (KeyState.IsCtrlPressed (e))
        e.AllowedEffect = DragDropEffects.Copy;
    else
        e.AllowedEffect = DragDropEffects.Scroll;
}

History

  • May 15, 2003: Thanks to the excellent suggestion of allancto, I added support for static methods.
  • February 16, 2003: After submission, I felt the article inadequate and needed elaboration.
  • February 15, 2003: Submitted.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jim Stewart

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionGood idea-- but why not go the extra mile? Pinmemberallancto6:54 15 May '03  
AnswerRe: Good idea-- but why not go the extra mile? PinmemberJim Stewart11:28 15 May '03  
GeneralRe: Good idea-- but why not go the extra mile? Pinmemberallancto12:46 15 May '03  
GeneralRe: Good idea-- but why not go the extra mile? PinmemberJim Stewart15:11 15 May '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 May 2003
Article Copyright 2003 by Jim Stewart
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid