Click here to Skip to main content
15,890,438 members
Articles / Programming Languages / C#
Article

KeyState Struct Objectifies KeyState Property

Rate me:
Please Sign up or sign in to vote.
4.30/5 (10 votes)
14 May 20031 min read 60.3K   445   15   4
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:

C#
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:

C#
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:

C#
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.

C#
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:

C#
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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGood idea-- but why not go the extra mile? Pin
allancto15-May-03 6:54
allancto15-May-03 6:54 
AnswerRe: Good idea-- but why not go the extra mile? Pin
Jim Stewart15-May-03 11:28
Jim Stewart15-May-03 11:28 
GeneralRe: Good idea-- but why not go the extra mile? Pin
allancto15-May-03 12:46
allancto15-May-03 12:46 
GeneralRe: Good idea-- but why not go the extra mile? Pin
Jim Stewart15-May-03 15:11
Jim Stewart15-May-03 15:11 

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

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