C# - Extend Key enumeration

By , 4/25/2015
(0 ratings)
The often used Key Enumeration in WPF projects become some extension methods with this snippet. These can categorize the type of the Key.

Similar can be implemented for Windows Forms.
Translate to VB
namespace System.Windows.Input
{
    public static class KeyExtensions
    {        
        public static bool IsNumberKey(this Key key)
        {
            return key >= Key.D0 && key <= Key.D9;
        }
        public static bool IsNumberPadNumberKey(this Key key)
        {
            return key >= Key.NumPad0 && key <= Key.NumPad9;
        }
        public static bool IsLatinLetterKey(this Key key)
        {
            return key >= Key.A && key <= Key.Z;
        }
        public static bool IsTextCursorControllerKey(this Key key)
        {
            return key == Key.Left || key == Key.Up || key == Key.Right || key == Key.Down
                || key == Key.End || key == Key.Prior || key == Key.Next;
        }
        public static bool IsTextContentControllerKey(this Key key)
        {
            return key == Key.Insert
                || key == Key.Delete || key == Key.Back;
        }
        public static bool IsFunctionKey(this Key key)
        {
            return key >= Key.F1 && key <= Key.F24;
        }
        public static bool IsMediaLey(this Key key)
        {
            return key == Key.MediaNextTrack
                || key == Key.MediaPlayPause
                || key == Key.MediaPreviousTrack
                || key == Key.MediaStop
                || key == Key.SelectMedia;
        }
    }
}
Tagged with Key, KeyDownKeyUp.

Comments

 

Log in, to comment!

Related Snippets