Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Base / System / Windows / Input / KeyConverter.cs / 1305600 / KeyConverter.cs
//---------------------------------------------------------------------------- // // File: KeyConverter.cs // // Description: // // KeyConverter : Converts a key string to the *Type* that the string represents and vice-versa // // Features: // // History: // 05/28/2003 created: Chandrasekhar Rentachintala // // Copyright (C) 2003 by Microsoft Corporation. All rights reserved. // //--------------------------------------------------------------------------- using System; using System.ComponentModel; // for TypeConverter using System.Globalization; // for CultureInfo using System.Reflection; using System.Windows; using System.Windows.Input; using System.Windows.Markup; using System.Security.Permissions; using MS.Utility; using MS.Internal.WindowsBase; namespace System.Windows.Input { ////// Key Converter class for converting between a string and the Type of a Key /// ///public class KeyConverter : TypeConverter { /// /// CanConvertFrom() /// /// /// ////// public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) { return true; } else { return false; } } /// /// TypeConverter method override. /// /// ITypeDescriptorContext /// Type to convert to ///true if conversion is possible public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { // We can convert to a string. // We can convert to an InstanceDescriptor or to a string. if (destinationType == typeof(string)) { // When invoked by the serialization engine we can convert to string only for known type if (context != null && context.Instance != null) { Key key = (Key)context.Instance; return ((int)key >= (int)Key.None && (int)key <= (int)Key.DeadCharProcessed); } } return false; } ////// ConvertFrom() /// /// /// /// ////// public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source) { if (source is string) { string fullName = ((string)source).Trim(); object key = GetKey(fullName, CultureInfo.InvariantCulture); if (key != null) { return ((Key)key); } else { throw new NotSupportedException(SR.Get(SRID.Unsupported_Key, fullName)); } } throw GetConvertFromException(source); } /// /// ConvertTo() /// /// /// /// /// ////// public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) throw new ArgumentNullException("destinationType"); if (destinationType == typeof(string) && value != null) { Key key = (Key)value; if (key == Key.None) { return String.Empty; } if (key >= Key.D0 && key <= Key.D9) { return Char.ToString((char)(int)(key - Key.D0 + '0')); } if (key >= Key.A && key <= Key.Z) { return Char.ToString((char)(int)(key - Key.A + 'A')); } String strKey = MatchKey(key, culture); if (strKey != null && (strKey.Length != 0 || strKey == String.Empty)) { return strKey; } } throw GetConvertToException(value, destinationType); } private object GetKey(string keyToken, CultureInfo culture) { if (keyToken == String.Empty) { return Key.None; } else { keyToken = keyToken.ToUpper(culture); if (keyToken.Length == 1 && Char.IsLetterOrDigit(keyToken[0])) { if (Char.IsDigit(keyToken[0]) && (keyToken[0] >= '0' && keyToken[0] <= '9')) { return ((int)(Key)(Key.D0 + keyToken[0] - '0')); } else if (Char.IsLetter(keyToken[0]) && (keyToken[0] >= 'A' && keyToken[0] <= 'Z')) { return ((int)(Key)(Key.A + keyToken[0] - 'A')); } else { throw new ArgumentException(SR.Get(SRID.CannotConvertStringToType, keyToken, typeof(Key))); } } else { Key keyFound = (Key)(-1); switch (keyToken) { case "ENTER": keyFound = Key.Return; break; case "ESC": keyFound = Key.Escape; break; case "PGUP": keyFound = Key.PageUp; break; case "PGDN": keyFound = Key.PageDown; break; case "PRTSC": keyFound = Key.PrintScreen; break; case "INS": keyFound = Key.Insert; break; case "DEL": keyFound = Key.Delete; break; case "WINDOWS": keyFound = Key.LWin; break; case "WIN": keyFound = Key.LWin; break; case "LEFTWINDOWS": keyFound = Key.LWin; break; case "RIGHTWINDOWS": keyFound = Key.RWin; break; case "APPS": keyFound = Key.Apps; break; case "APPLICATION": keyFound = Key.Apps; break; case "BREAK": keyFound = Key.Cancel; break; case "BACKSPACE": keyFound = Key.Back; break; case "BKSP": keyFound = Key.Back; break; case "BS": keyFound = Key.Back; break; case "SHIFT": keyFound = Key.LeftShift; break; case "LEFTSHIFT": keyFound = Key.LeftShift; break; case "RIGHTSHIFT": keyFound = Key.RightShift; break; case "CONTROL": keyFound = Key.LeftCtrl; break; case "CTRL": keyFound = Key.LeftCtrl; break; case "LEFTCTRL": keyFound = Key.LeftCtrl; break; case "RIGHTCTRL": keyFound = Key.RightCtrl; break; case "ALT": keyFound = Key.LeftAlt; break; case "LEFTALT": keyFound = Key.LeftAlt; break; case "RIGHTALT": keyFound = Key.RightAlt; break; case "SEMICOLON": keyFound = Key.OemSemicolon; break; case "PLUS": keyFound = Key.OemPlus; break; case "COMMA": keyFound = Key.OemComma; break; case "MINUS": keyFound = Key.OemMinus; break; case "PERIOD": keyFound = Key.OemPeriod; break; case "QUESTION": keyFound = Key.OemQuestion; break; case "TILDE": keyFound = Key.OemTilde; break; case "OPENBRACKETS": keyFound = Key.OemOpenBrackets; break; case "PIPE": keyFound = Key.OemPipe; break; case "CLOSEBRACKETS": keyFound = Key.OemCloseBrackets; break; case "QUOTES": keyFound = Key.OemQuotes; break; case "BACKSLASH": keyFound = Key.OemBackslash; break; case "FINISH": keyFound = Key.OemFinish; break; case "ATTN": keyFound = Key.Attn; break; case "CRSEL": keyFound = Key.CrSel; break; case "EXSEL": keyFound = Key.ExSel; break; case "ERASEEOF": keyFound = Key.EraseEof; break; case "PLAY": keyFound = Key.Play; break; case "ZOOM": keyFound = Key.Zoom; break; case "PA1": keyFound = Key.Pa1; break; default: keyFound = (Key)Enum.Parse(typeof(Key), keyToken, true); break; } if ((int)keyFound != -1) { return keyFound; } return null; } } } private static string MatchKey(Key key, CultureInfo culture) { if (key == Key.None) return String.Empty; else { switch (key) { case Key.Back: return "Backspace"; case Key.LineFeed: return "Clear"; case Key.Escape: return "Esc"; } } if ((int)key >= (int)Key.None && (int)key <= (int)Key.DeadCharProcessed) return key.ToString(); else return null; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //---------------------------------------------------------------------------- // // File: KeyConverter.cs // // Description: // // KeyConverter : Converts a key string to the *Type* that the string represents and vice-versa // // Features: // // History: // 05/28/2003 created: Chandrasekhar Rentachintala // // Copyright (C) 2003 by Microsoft Corporation. All rights reserved. // //--------------------------------------------------------------------------- using System; using System.ComponentModel; // for TypeConverter using System.Globalization; // for CultureInfo using System.Reflection; using System.Windows; using System.Windows.Input; using System.Windows.Markup; using System.Security.Permissions; using MS.Utility; using MS.Internal.WindowsBase; namespace System.Windows.Input { /// /// Key Converter class for converting between a string and the Type of a Key /// ///public class KeyConverter : TypeConverter { /// /// CanConvertFrom() /// /// /// ////// public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) { return true; } else { return false; } } /// /// TypeConverter method override. /// /// ITypeDescriptorContext /// Type to convert to ///true if conversion is possible public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { // We can convert to a string. // We can convert to an InstanceDescriptor or to a string. if (destinationType == typeof(string)) { // When invoked by the serialization engine we can convert to string only for known type if (context != null && context.Instance != null) { Key key = (Key)context.Instance; return ((int)key >= (int)Key.None && (int)key <= (int)Key.DeadCharProcessed); } } return false; } ////// ConvertFrom() /// /// /// /// ////// public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source) { if (source is string) { string fullName = ((string)source).Trim(); object key = GetKey(fullName, CultureInfo.InvariantCulture); if (key != null) { return ((Key)key); } else { throw new NotSupportedException(SR.Get(SRID.Unsupported_Key, fullName)); } } throw GetConvertFromException(source); } /// /// ConvertTo() /// /// /// /// /// ////// public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) throw new ArgumentNullException("destinationType"); if (destinationType == typeof(string) && value != null) { Key key = (Key)value; if (key == Key.None) { return String.Empty; } if (key >= Key.D0 && key <= Key.D9) { return Char.ToString((char)(int)(key - Key.D0 + '0')); } if (key >= Key.A && key <= Key.Z) { return Char.ToString((char)(int)(key - Key.A + 'A')); } String strKey = MatchKey(key, culture); if (strKey != null && (strKey.Length != 0 || strKey == String.Empty)) { return strKey; } } throw GetConvertToException(value, destinationType); } private object GetKey(string keyToken, CultureInfo culture) { if (keyToken == String.Empty) { return Key.None; } else { keyToken = keyToken.ToUpper(culture); if (keyToken.Length == 1 && Char.IsLetterOrDigit(keyToken[0])) { if (Char.IsDigit(keyToken[0]) && (keyToken[0] >= '0' && keyToken[0] <= '9')) { return ((int)(Key)(Key.D0 + keyToken[0] - '0')); } else if (Char.IsLetter(keyToken[0]) && (keyToken[0] >= 'A' && keyToken[0] <= 'Z')) { return ((int)(Key)(Key.A + keyToken[0] - 'A')); } else { throw new ArgumentException(SR.Get(SRID.CannotConvertStringToType, keyToken, typeof(Key))); } } else { Key keyFound = (Key)(-1); switch (keyToken) { case "ENTER": keyFound = Key.Return; break; case "ESC": keyFound = Key.Escape; break; case "PGUP": keyFound = Key.PageUp; break; case "PGDN": keyFound = Key.PageDown; break; case "PRTSC": keyFound = Key.PrintScreen; break; case "INS": keyFound = Key.Insert; break; case "DEL": keyFound = Key.Delete; break; case "WINDOWS": keyFound = Key.LWin; break; case "WIN": keyFound = Key.LWin; break; case "LEFTWINDOWS": keyFound = Key.LWin; break; case "RIGHTWINDOWS": keyFound = Key.RWin; break; case "APPS": keyFound = Key.Apps; break; case "APPLICATION": keyFound = Key.Apps; break; case "BREAK": keyFound = Key.Cancel; break; case "BACKSPACE": keyFound = Key.Back; break; case "BKSP": keyFound = Key.Back; break; case "BS": keyFound = Key.Back; break; case "SHIFT": keyFound = Key.LeftShift; break; case "LEFTSHIFT": keyFound = Key.LeftShift; break; case "RIGHTSHIFT": keyFound = Key.RightShift; break; case "CONTROL": keyFound = Key.LeftCtrl; break; case "CTRL": keyFound = Key.LeftCtrl; break; case "LEFTCTRL": keyFound = Key.LeftCtrl; break; case "RIGHTCTRL": keyFound = Key.RightCtrl; break; case "ALT": keyFound = Key.LeftAlt; break; case "LEFTALT": keyFound = Key.LeftAlt; break; case "RIGHTALT": keyFound = Key.RightAlt; break; case "SEMICOLON": keyFound = Key.OemSemicolon; break; case "PLUS": keyFound = Key.OemPlus; break; case "COMMA": keyFound = Key.OemComma; break; case "MINUS": keyFound = Key.OemMinus; break; case "PERIOD": keyFound = Key.OemPeriod; break; case "QUESTION": keyFound = Key.OemQuestion; break; case "TILDE": keyFound = Key.OemTilde; break; case "OPENBRACKETS": keyFound = Key.OemOpenBrackets; break; case "PIPE": keyFound = Key.OemPipe; break; case "CLOSEBRACKETS": keyFound = Key.OemCloseBrackets; break; case "QUOTES": keyFound = Key.OemQuotes; break; case "BACKSLASH": keyFound = Key.OemBackslash; break; case "FINISH": keyFound = Key.OemFinish; break; case "ATTN": keyFound = Key.Attn; break; case "CRSEL": keyFound = Key.CrSel; break; case "EXSEL": keyFound = Key.ExSel; break; case "ERASEEOF": keyFound = Key.EraseEof; break; case "PLAY": keyFound = Key.Play; break; case "ZOOM": keyFound = Key.Zoom; break; case "PA1": keyFound = Key.Pa1; break; default: keyFound = (Key)Enum.Parse(typeof(Key), keyToken, true); break; } if ((int)keyFound != -1) { return keyFound; } return null; } } } private static string MatchKey(Key key, CultureInfo culture) { if (key == Key.None) return String.Empty; else { switch (key) { case Key.Back: return "Backspace"; case Key.LineFeed: return "Clear"; case Key.Escape: return "Esc"; } } if ((int)key >= (int)Key.None && (int)key <= (int)Key.DeadCharProcessed) return key.ToString(); else return null; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PartManifestEntry.cs
- CompositeCollection.cs
- RootBrowserWindowAutomationPeer.cs
- MissingMethodException.cs
- ValueProviderWrapper.cs
- TableHeaderCell.cs
- SplineKeyFrames.cs
- DataGridViewCellFormattingEventArgs.cs
- DesignerVerb.cs
- SerialPort.cs
- ContainerSelectorGlyph.cs
- FontWeight.cs
- SymmetricAlgorithm.cs
- MsdtcClusterUtils.cs
- ReadOnlyDataSource.cs
- PeerApplication.cs
- MasterPageParser.cs
- SafeHandles.cs
- SafeRightsManagementQueryHandle.cs
- StorageConditionPropertyMapping.cs
- WebPartsSection.cs
- SecurityChannel.cs
- SamlConstants.cs
- PolyBezierSegment.cs
- CallContext.cs
- XmlCharacterData.cs
- TextMessageEncodingBindingElement.cs
- NameValueCollection.cs
- PrivilegedConfigurationManager.cs
- SafeSecurityHandles.cs
- GenericEnumerator.cs
- MdiWindowListStrip.cs
- TypeBuilderInstantiation.cs
- EdmProperty.cs
- ConsoleEntryPoint.cs
- DbConnectionHelper.cs
- CodeSubDirectory.cs
- LinkAreaEditor.cs
- CommandExpr.cs
- InputLangChangeRequestEvent.cs
- SystemResourceKey.cs
- FlowDocument.cs
- HtmlMobileTextWriter.cs
- ToolStripGripRenderEventArgs.cs
- TimerEventSubscriptionCollection.cs
- XmlHierarchicalDataSourceView.cs
- HttpTransportManager.cs
- WpfXamlLoader.cs
- FrameworkElement.cs
- FormatConvertedBitmap.cs
- HijriCalendar.cs
- ProfileProvider.cs
- ContentPathSegment.cs
- ContextMarshalException.cs
- SecureConversationSecurityTokenParameters.cs
- DataBoundLiteralControl.cs
- EntitySetBaseCollection.cs
- RoutedEventValueSerializer.cs
- ConnectionPoint.cs
- SocketPermission.cs
- TableRowCollection.cs
- PairComparer.cs
- SafeMarshalContext.cs
- SecondaryIndexDefinition.cs
- WebPartConnectionCollection.cs
- RijndaelManaged.cs
- Range.cs
- CompoundFileDeflateTransform.cs
- NamedPipeChannelFactory.cs
- XmlValidatingReaderImpl.cs
- SymmetricSecurityProtocolFactory.cs
- TcpClientCredentialType.cs
- EllipseGeometry.cs
- PrimarySelectionAdorner.cs
- ResourcesGenerator.cs
- BrowsableAttribute.cs
- TrustLevel.cs
- Mappings.cs
- HttpListener.cs
- XamlSerializer.cs
- SystemIPv4InterfaceProperties.cs
- Calendar.cs
- PerfService.cs
- JsonServiceDocumentSerializer.cs
- EntityDataSourceQueryBuilder.cs
- MobileUITypeEditor.cs
- Crypto.cs
- TextAdaptor.cs
- QuaternionRotation3D.cs
- ResponseStream.cs
- IndexerNameAttribute.cs
- QuaternionRotation3D.cs
- WebProxyScriptElement.cs
- SqlRecordBuffer.cs
- AmbientProperties.cs
- Trustee.cs
- CollectionViewGroupInternal.cs
- PluralizationServiceUtil.cs
- mda.cs
- PolyBezierSegment.cs