KeyEvent.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / WinForms / Managed / System / WinForms / KeyEvent.cs / 1 / KeyEvent.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

/* 
 */ 
namespace System.Windows.Forms {
 
    using System.Diagnostics;

    using System;
    using System.Drawing; 
    using System.ComponentModel;
    using Microsoft.Win32; 
    using System.Diagnostics.CodeAnalysis; 

    ///  
    /// 
    ///    
    ///       Provides data for the  or 
    ///       event. 
    ///    
    ///  
    [System.Runtime.InteropServices.ComVisible(true)] 
    public class KeyEventArgs : EventArgs {
 
        /// 
        /// 
        ///     Contains key data for KeyDown and KeyUp events.  This is a combination
        ///     of keycode and modifer flags. 
        /// 
        private readonly Keys keyData; 
 
        /// 
        ///  
        ///     Determines if this event has been handled by a handler.  If handled, the
        ///     key event will not be sent along to Windows.  If not handled, the event
        ///     will be sent to Windows for default processing.
        ///  
        private bool handled = false;
 
        ///  
        /// 
        ///  
        private bool suppressKeyPress = false;

        /// 
        ///  
        ///    
        ///       Initializes a new 
        ///       instance of the  class. 
        ///    
        ///  
        public KeyEventArgs(Keys keyData) {
            this.keyData = keyData;
        }
 
        /// 
        ///  
        ///     
        ///       Gets a value indicating whether the ALT key was pressed.
        ///     
        /// 
        public virtual bool Alt {
            get {
                return (keyData & Keys.Alt) == Keys.Alt; 
            }
        } 
 
        /// 
        ///  
        ///    
        ///       Gets a value indicating whether the CTRL key was pressed.
        ///    
        ///  
        public bool Control {
            get { 
                return (keyData & Keys.Control) == Keys.Control; 
            }
        } 

        /// 
        /// 
        ///     
        ///       Gets or sets a value
        ///       indicating whether the event was handled. 
        ///     
        /// 
        // 
        public bool Handled {
            get {
                return handled;
            } 
            set {
                handled = value; 
            } 
        }
 
        /// 
        /// 
        ///    
        ///       Gets the keyboard code for a  or  
        ///       event.
        ///     
        ///  
        //subhag : changed the behaviour of the KeyCode as per the new requirements.
        public Keys KeyCode { 
            [
                // Keys is discontiguous so we have to use Enum.IsDefined.
                SuppressMessage("Microsoft.Performance", "CA1803:AvoidCostlyCallsWherePossible")
            ] 
            get {
                Keys keyGenerated =  keyData & Keys.KeyCode; 
 
                // since Keys can be discontiguous, keeping Enum.IsDefined.
                if (!Enum.IsDefined(typeof(Keys),(int)keyGenerated)) 
                    return Keys.None;
                else
                    return keyGenerated;
            } 
        }
 
        ///  
        /// 
        ///     
        ///       Gets the keyboard value for a  or 
        ///       event.
        ///    
        ///  
        //subhag : added the KeyValue as per the new requirements.
        public int KeyValue { 
            get { 
                return (int)(keyData & Keys.KeyCode);
            } 
        }

        /// 
        ///  
        ///    
        ///       Gets the key data for a  or  
        ///       event. 
        ///    
        ///  
        public Keys KeyData {
            get {
                return keyData;
            } 
        }
 
        ///  
        /// 
        ///     
        ///       Gets the modifier flags for a  or  event.
        ///       This indicates which modifier keys (CTRL, SHIFT, and/or ALT) were pressed.
        ///    
        ///  
        public Keys Modifiers {
            get { 
                return keyData & Keys.Modifiers; 
            }
        } 

        /// 
        /// 
        ///     
        ///       Gets
        ///       a value indicating whether the SHIFT key was pressed. 
        ///     
        /// 
        public virtual bool Shift { 
            get {
                return (keyData & Keys.Shift) == Keys.Shift;
            }
        } 

        ///  
        ///  
        /// 
        // 
        public bool SuppressKeyPress {
            get {
                return suppressKeyPress;
            } 
            set {
                suppressKeyPress = value; 
                handled = value; 
            }
        } 

    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK