Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Core / CSharp / System / Windows / Input / Win32KeyboardDevice.cs / 1305600 / Win32KeyboardDevice.cs
using System.Collections; using System.Windows; using System.Security; using System.Security.Permissions; using MS.Internal; using MS.Internal.PresentationCore; // SecurityHelper using System.Windows.Media; using MS.Win32; // VK translation. using System; using SR=MS.Internal.PresentationCore.SR; using SRID=MS.Internal.PresentationCore.SRID; namespace System.Windows.Input { ////// The Win32KeyboardDevice class implements the platform specific /// KeyboardDevice features for the Win32 platform /// internal sealed class Win32KeyboardDevice : KeyboardDevice { ////// /// /// /// ////// Critical: This code creates critical data(_tsfManager,_textcompositionManager) and stores critical data (inputManager) /// TreatAsSafe: Although it creates critical data there are demand on the critical data and the constructor is safe /// [SecurityCritical,SecurityTreatAsSafe] internal Win32KeyboardDevice(InputManager inputManager) : base(inputManager) { } ////// Gets the current state of the specified key from the device from the underlying system /// /// /// Key to get the state of /// ////// The state of the specified key /// ////// Critical: Makes calls to UnsafeNativeMethods (GetKeyState) /// TreatAsSafe: We will not expose the state of keys in /// partially trusted AppDomains unless a WPF /// window in this AppDomain has focus. An /// exception is made for modifier keys. /// [SecurityCritical, SecurityTreatAsSafe] protected override KeyStates GetKeyStatesFromSystem(Key key) { KeyStates keyStates = KeyStates.None; bool getKeyStatesFromSystem = false; if(IsActive) { // Our keyboard device is only active if some WPF window in // this AppDomain has focus. It is always safe to return // the state of keys. getKeyStatesFromSystem = true; } else if (SecurityHelper.AppDomainGrantedUnrestrictedUIPermission) { // This is a trusted AppDomain, so we are willing to expose // the state of keys regardless of whether or not a WPF // window has focus. This is important for child HWND // hosting scenarios. getKeyStatesFromSystem = true; } else { // Security Mitigation: // No WPF window has focus in this AppDomain, and this is a // partially-trusted AppDomain, so we do not generally want // to expose the state of keys. However, we make an exception // for modifier keys, as they are considered safe. switch (key) { case Key.LeftAlt: case Key.RightAlt: case Key.LeftCtrl: case Key.RightCtrl: case Key.LeftShift: case Key.RightShift: getKeyStatesFromSystem = true; break; } } if (getKeyStatesFromSystem) { int virtualKeyCode = KeyInterop.VirtualKeyFromKey(key); int nativeKeyState; nativeKeyState = UnsafeNativeMethods.GetKeyState(virtualKeyCode); if( (nativeKeyState & 0x00008000) == 0x00008000 ) keyStates |= KeyStates.Down; if( (nativeKeyState & 0x00000001) == 0x00000001 ) keyStates |= KeyStates.Toggled; } return keyStates; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System.Collections; using System.Windows; using System.Security; using System.Security.Permissions; using MS.Internal; using MS.Internal.PresentationCore; // SecurityHelper using System.Windows.Media; using MS.Win32; // VK translation. using System; using SR=MS.Internal.PresentationCore.SR; using SRID=MS.Internal.PresentationCore.SRID; namespace System.Windows.Input { ////// The Win32KeyboardDevice class implements the platform specific /// KeyboardDevice features for the Win32 platform /// internal sealed class Win32KeyboardDevice : KeyboardDevice { ////// /// /// /// ////// Critical: This code creates critical data(_tsfManager,_textcompositionManager) and stores critical data (inputManager) /// TreatAsSafe: Although it creates critical data there are demand on the critical data and the constructor is safe /// [SecurityCritical,SecurityTreatAsSafe] internal Win32KeyboardDevice(InputManager inputManager) : base(inputManager) { } ////// Gets the current state of the specified key from the device from the underlying system /// /// /// Key to get the state of /// ////// The state of the specified key /// ////// Critical: Makes calls to UnsafeNativeMethods (GetKeyState) /// TreatAsSafe: We will not expose the state of keys in /// partially trusted AppDomains unless a WPF /// window in this AppDomain has focus. An /// exception is made for modifier keys. /// [SecurityCritical, SecurityTreatAsSafe] protected override KeyStates GetKeyStatesFromSystem(Key key) { KeyStates keyStates = KeyStates.None; bool getKeyStatesFromSystem = false; if(IsActive) { // Our keyboard device is only active if some WPF window in // this AppDomain has focus. It is always safe to return // the state of keys. getKeyStatesFromSystem = true; } else if (SecurityHelper.AppDomainGrantedUnrestrictedUIPermission) { // This is a trusted AppDomain, so we are willing to expose // the state of keys regardless of whether or not a WPF // window has focus. This is important for child HWND // hosting scenarios. getKeyStatesFromSystem = true; } else { // Security Mitigation: // No WPF window has focus in this AppDomain, and this is a // partially-trusted AppDomain, so we do not generally want // to expose the state of keys. However, we make an exception // for modifier keys, as they are considered safe. switch (key) { case Key.LeftAlt: case Key.RightAlt: case Key.LeftCtrl: case Key.RightCtrl: case Key.LeftShift: case Key.RightShift: getKeyStatesFromSystem = true; break; } } if (getKeyStatesFromSystem) { int virtualKeyCode = KeyInterop.VirtualKeyFromKey(key); int nativeKeyState; nativeKeyState = UnsafeNativeMethods.GetKeyState(virtualKeyCode); if( (nativeKeyState & 0x00008000) == 0x00008000 ) keyStates |= KeyStates.Down; if( (nativeKeyState & 0x00000001) == 0x00000001 ) keyStates |= KeyStates.Toggled; } return keyStates; } } } // 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
- ProfileParameter.cs
- InstancePersistenceEvent.cs
- BaseContextMenu.cs
- __ConsoleStream.cs
- InvalidCommandTreeException.cs
- XPathNavigatorKeyComparer.cs
- TreeNodeEventArgs.cs
- ProfileEventArgs.cs
- OletxVolatileEnlistment.cs
- ErrorLog.cs
- XmlSchemaComplexContentRestriction.cs
- Privilege.cs
- Renderer.cs
- CodeIndexerExpression.cs
- Cell.cs
- Event.cs
- ApplicationSettingsBase.cs
- PageCanvasSize.cs
- NamespaceListProperty.cs
- UnicastIPAddressInformationCollection.cs
- SecurityPolicySection.cs
- SystemParameters.cs
- TextSelection.cs
- GradientBrush.cs
- _IPv6Address.cs
- Baml2006KnownTypes.cs
- DbConnectionStringBuilder.cs
- ResourceKey.cs
- SoapDocumentMethodAttribute.cs
- InfiniteIntConverter.cs
- SpecialFolderEnumConverter.cs
- PropertyGrid.cs
- GlyphInfoList.cs
- DropShadowEffect.cs
- XslVisitor.cs
- SupportingTokenDuplexChannel.cs
- TextAnchor.cs
- ListDataBindEventArgs.cs
- FormsAuthenticationModule.cs
- UrlMappingsModule.cs
- DeadCharTextComposition.cs
- DataGridView.cs
- QilParameter.cs
- SamlConditions.cs
- OutputScopeManager.cs
- SiteMapPath.cs
- VisualTreeHelper.cs
- DeferredReference.cs
- ObjectParameterCollection.cs
- SubclassTypeValidatorAttribute.cs
- complextypematerializer.cs
- SessionEndingEventArgs.cs
- PolyBezierSegment.cs
- SystemIPv6InterfaceProperties.cs
- HwndKeyboardInputProvider.cs
- GlyphTypeface.cs
- DockPanel.cs
- ExpressionNormalizer.cs
- CompoundFileStreamReference.cs
- TreeNodeClickEventArgs.cs
- SocketAddress.cs
- LowerCaseStringConverter.cs
- VirtualDirectoryMapping.cs
- Stroke2.cs
- HostDesigntimeLicenseContext.cs
- MarkerProperties.cs
- XhtmlBasicImageAdapter.cs
- StringWriter.cs
- ExpressionVisitor.cs
- TimeEnumHelper.cs
- Point3DAnimation.cs
- DebuggerAttributes.cs
- QuaternionRotation3D.cs
- EntityDataSourceMemberPath.cs
- rsa.cs
- HttpDictionary.cs
- SimpleExpression.cs
- HostingEnvironmentException.cs
- HintTextConverter.cs
- DataGridParentRows.cs
- WsrmMessageInfo.cs
- DataBinding.cs
- DataServiceClientException.cs
- HostDesigntimeLicenseContext.cs
- CodeSnippetTypeMember.cs
- PropertyConverter.cs
- DateTimeStorage.cs
- HMACRIPEMD160.cs
- RepeatButton.cs
- ContainerVisual.cs
- DockPanel.cs
- DataColumnSelectionConverter.cs
- filewebrequest.cs
- DesignerCategoryAttribute.cs
- DeleteHelper.cs
- Constraint.cs
- InputMethod.cs
- xsdvalidator.cs
- IteratorFilter.cs
- ReflectionHelper.cs