Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Core / CSharp / System / Windows / Input / TextServicesManager.cs / 1 / TextServicesManager.cs
//---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // // Description: Provides input to ImeProcessed promotion -- feeds keystrokes // to IMEs. // // History: // 07/23/2003 : benwest - Created // //--------------------------------------------------------------------------- using System.Windows.Threading; using MS.Internal; using MS.Win32; using System; using System.Security; using System.Security.Permissions; namespace System.Windows.Input { internal class TextServicesManager : DispatcherObject { //----------------------------------------------------- // // Constructors // //----------------------------------------------------- #region Constructors ////// Critical - Calls a critical method - PreProcessInput /// TreatAsSafe - Ok for us to register an event handler. Handler itself is critical. /// [SecurityCritical, SecurityTreatAsSafe] internal TextServicesManager(InputManager inputManager) { _inputManager = inputManager; _inputManager.PreProcessInput += new PreProcessInputEventHandler(PreProcessInput); _inputManager.PostProcessInput += new ProcessInputEventHandler(PostProcessInput); } #endregion Constructors //------------------------------------------------------ // // Public Methods // //----------------------------------------------------- //------------------------------------------------------ // // Public Properties // //------------------------------------------------------ //----------------------------------------------------- // // Public Events // //------------------------------------------------------ //----------------------------------------------------- // // Protected Methods // //----------------------------------------------------- //----------------------------------------------------- // // Internal Methods // //------------------------------------------------------ // Track the focus of KeyboardDevice. KeyboardDevice.ChangeFocus() this. internal void Focus(DependencyObject focus) { if (focus == null) { // Don't grab keyboard events from Text Services Framework without keyboard focus. this.Dispatcher.IsTSFMessagePumpEnabled = false; return; } // Grab keyboard events from Text Services Framework with keyboard focus. this.Dispatcher.IsTSFMessagePumpEnabled = true; if ((bool)focus.GetValue(InputMethod.IsInputMethodSuspendedProperty)) { // The focus is on the element that suspending IME's input (such as menu). // The document focus should remain. return; } InputMethod.Current.EnableOrDisableInputMethod((bool)focus.GetValue(InputMethod.IsInputMethodEnabledProperty)); } //----------------------------------------------------- // // Internal Properties // //------------------------------------------------------ //------------------------------------------------------ // // Internal Events // //----------------------------------------------------- //------------------------------------------------------ // // Private Methods // //----------------------------------------------------- #region Private Methods // Marks interesting KeyDown events as ImeInput. ////// Critical - directly pushes keys into the input stack /// [SecurityCritical] private void PreProcessInput(object sender, PreProcessInputEventArgs e) { TextServicesContext context; KeyEventArgs keyArgs; if (!TextServicesLoader.ServicesInstalled) return; if(e.StagingItem.Input.RoutedEvent != Keyboard.PreviewKeyDownEvent && e.StagingItem.Input.RoutedEvent != Keyboard.PreviewKeyUpEvent) { return; } // filter SysKey if (IsSysKeyDown()) return; // IMM32-IME handles the key event and we don't do anything. if (InputMethod.IsImm32ImeCurrent()) return; DependencyObject element = Keyboard.FocusedElement as DependencyObject; if ((element == null) || (bool)element.GetValue(InputMethod.IsInputMethodSuspendedProperty)) { // The focus is on the element that suspending IME's input (such as menu). // we don't do anything. return; } keyArgs = (KeyEventArgs)e.StagingItem.Input; if(!keyArgs.Handled) { context = TextServicesContext.DispatcherCurrent; if (context != null) { if (TextServicesKeystroke(context, keyArgs, true /* test */)) { keyArgs.MarkImeProcessed(); } } } } ////// Critical - directly pushes keys into the input stack /// [SecurityCritical] private void PostProcessInput(object sender, ProcessInputEventArgs e) { TextServicesContext context; KeyEventArgs keyArgs; if (!TextServicesLoader.ServicesInstalled) return; // IMM32-IME handles the key event and we don't do anything. if (InputMethod.IsImm32ImeCurrent()) return; DependencyObject element = Keyboard.FocusedElement as DependencyObject; if ((element == null) || (bool)element.GetValue(InputMethod.IsInputMethodSuspendedProperty)) { // The focus is on the element that suspending IME's input (such as menu). // we don't do anything. return; } if(e.StagingItem.Input.RoutedEvent == Keyboard.PreviewKeyDownEvent || e.StagingItem.Input.RoutedEvent == Keyboard.PreviewKeyUpEvent) { // filter SysKey if (IsSysKeyDown()) return; keyArgs = (KeyEventArgs)e.StagingItem.Input; if(!keyArgs.Handled && keyArgs.Key == Key.ImeProcessed) { context = TextServicesContext.DispatcherCurrent; if (context != null) { if (TextServicesKeystroke(context, keyArgs, false /* test */)) { keyArgs.Handled = true; } } } } else if(e.StagingItem.Input.RoutedEvent == Keyboard.KeyDownEvent || e.StagingItem.Input.RoutedEvent == Keyboard.KeyUpEvent) { keyArgs = (KeyEventArgs)e.StagingItem.Input; if(!keyArgs.Handled && keyArgs.Key == Key.ImeProcessed) { keyArgs.Handled = true; } } } ////// Critical - directly pushes keys into the input stack /// [SecurityCritical] private bool TextServicesKeystroke(TextServicesContext context, KeyEventArgs keyArgs, bool test) { TextServicesContext.KeyOp keyop; int wParam; int lParam; int scancode; // Cicero's Keystroke Manager and TIP does not recognize VK_RSHIFT or VK_LSHIFT. // We need to pass VK_SHIFT and the proper scancode. // switch (keyArgs.RealKey) { case Key.RightShift: wParam = NativeMethods.VK_SHIFT; scancode = 0x36; break; case Key.LeftShift: wParam = NativeMethods.VK_SHIFT; scancode = 0x2A; break; default: wParam = KeyInterop.VirtualKeyFromKey(keyArgs.RealKey); scancode = 0; break; } lParam = (int)(((uint)scancode << 16) | 1); if (keyArgs.RoutedEvent == Keyboard.PreviewKeyDownEvent/*keyArgs.IsDown*/) { keyop = test ? TextServicesContext.KeyOp.TestDown : TextServicesContext.KeyOp.Down; } else { // Previous key state and transition state always 1 for WM_KEYUP. lParam |= (1 << 31) | (1 << 30); keyop = test ? TextServicesContext.KeyOp.TestUp : TextServicesContext.KeyOp.Up; } return context.Keystroke(wParam, lParam, keyop); } private bool IsSysKeyDown() { if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt) || Keyboard.IsKeyDown(Key.F10)) return true; return false; } #endregion Private methods //----------------------------------------------------- // // Private Properties // //----------------------------------------------------- //------------------------------------------------------ // // Private Fields // //----------------------------------------------------- #region Private Fields ////// Critical - required elevations to create. /// [SecurityCritical] private readonly InputManager _inputManager; #endregion Private Fields } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // // Description: Provides input to ImeProcessed promotion -- feeds keystrokes // to IMEs. // // History: // 07/23/2003 : benwest - Created // //--------------------------------------------------------------------------- using System.Windows.Threading; using MS.Internal; using MS.Win32; using System; using System.Security; using System.Security.Permissions; namespace System.Windows.Input { internal class TextServicesManager : DispatcherObject { //----------------------------------------------------- // // Constructors // //----------------------------------------------------- #region Constructors ////// Critical - Calls a critical method - PreProcessInput /// TreatAsSafe - Ok for us to register an event handler. Handler itself is critical. /// [SecurityCritical, SecurityTreatAsSafe] internal TextServicesManager(InputManager inputManager) { _inputManager = inputManager; _inputManager.PreProcessInput += new PreProcessInputEventHandler(PreProcessInput); _inputManager.PostProcessInput += new ProcessInputEventHandler(PostProcessInput); } #endregion Constructors //------------------------------------------------------ // // Public Methods // //----------------------------------------------------- //------------------------------------------------------ // // Public Properties // //------------------------------------------------------ //----------------------------------------------------- // // Public Events // //------------------------------------------------------ //----------------------------------------------------- // // Protected Methods // //----------------------------------------------------- //----------------------------------------------------- // // Internal Methods // //------------------------------------------------------ // Track the focus of KeyboardDevice. KeyboardDevice.ChangeFocus() this. internal void Focus(DependencyObject focus) { if (focus == null) { // Don't grab keyboard events from Text Services Framework without keyboard focus. this.Dispatcher.IsTSFMessagePumpEnabled = false; return; } // Grab keyboard events from Text Services Framework with keyboard focus. this.Dispatcher.IsTSFMessagePumpEnabled = true; if ((bool)focus.GetValue(InputMethod.IsInputMethodSuspendedProperty)) { // The focus is on the element that suspending IME's input (such as menu). // The document focus should remain. return; } InputMethod.Current.EnableOrDisableInputMethod((bool)focus.GetValue(InputMethod.IsInputMethodEnabledProperty)); } //----------------------------------------------------- // // Internal Properties // //------------------------------------------------------ //------------------------------------------------------ // // Internal Events // //----------------------------------------------------- //------------------------------------------------------ // // Private Methods // //----------------------------------------------------- #region Private Methods // Marks interesting KeyDown events as ImeInput. ////// Critical - directly pushes keys into the input stack /// [SecurityCritical] private void PreProcessInput(object sender, PreProcessInputEventArgs e) { TextServicesContext context; KeyEventArgs keyArgs; if (!TextServicesLoader.ServicesInstalled) return; if(e.StagingItem.Input.RoutedEvent != Keyboard.PreviewKeyDownEvent && e.StagingItem.Input.RoutedEvent != Keyboard.PreviewKeyUpEvent) { return; } // filter SysKey if (IsSysKeyDown()) return; // IMM32-IME handles the key event and we don't do anything. if (InputMethod.IsImm32ImeCurrent()) return; DependencyObject element = Keyboard.FocusedElement as DependencyObject; if ((element == null) || (bool)element.GetValue(InputMethod.IsInputMethodSuspendedProperty)) { // The focus is on the element that suspending IME's input (such as menu). // we don't do anything. return; } keyArgs = (KeyEventArgs)e.StagingItem.Input; if(!keyArgs.Handled) { context = TextServicesContext.DispatcherCurrent; if (context != null) { if (TextServicesKeystroke(context, keyArgs, true /* test */)) { keyArgs.MarkImeProcessed(); } } } } ////// Critical - directly pushes keys into the input stack /// [SecurityCritical] private void PostProcessInput(object sender, ProcessInputEventArgs e) { TextServicesContext context; KeyEventArgs keyArgs; if (!TextServicesLoader.ServicesInstalled) return; // IMM32-IME handles the key event and we don't do anything. if (InputMethod.IsImm32ImeCurrent()) return; DependencyObject element = Keyboard.FocusedElement as DependencyObject; if ((element == null) || (bool)element.GetValue(InputMethod.IsInputMethodSuspendedProperty)) { // The focus is on the element that suspending IME's input (such as menu). // we don't do anything. return; } if(e.StagingItem.Input.RoutedEvent == Keyboard.PreviewKeyDownEvent || e.StagingItem.Input.RoutedEvent == Keyboard.PreviewKeyUpEvent) { // filter SysKey if (IsSysKeyDown()) return; keyArgs = (KeyEventArgs)e.StagingItem.Input; if(!keyArgs.Handled && keyArgs.Key == Key.ImeProcessed) { context = TextServicesContext.DispatcherCurrent; if (context != null) { if (TextServicesKeystroke(context, keyArgs, false /* test */)) { keyArgs.Handled = true; } } } } else if(e.StagingItem.Input.RoutedEvent == Keyboard.KeyDownEvent || e.StagingItem.Input.RoutedEvent == Keyboard.KeyUpEvent) { keyArgs = (KeyEventArgs)e.StagingItem.Input; if(!keyArgs.Handled && keyArgs.Key == Key.ImeProcessed) { keyArgs.Handled = true; } } } ////// Critical - directly pushes keys into the input stack /// [SecurityCritical] private bool TextServicesKeystroke(TextServicesContext context, KeyEventArgs keyArgs, bool test) { TextServicesContext.KeyOp keyop; int wParam; int lParam; int scancode; // Cicero's Keystroke Manager and TIP does not recognize VK_RSHIFT or VK_LSHIFT. // We need to pass VK_SHIFT and the proper scancode. // switch (keyArgs.RealKey) { case Key.RightShift: wParam = NativeMethods.VK_SHIFT; scancode = 0x36; break; case Key.LeftShift: wParam = NativeMethods.VK_SHIFT; scancode = 0x2A; break; default: wParam = KeyInterop.VirtualKeyFromKey(keyArgs.RealKey); scancode = 0; break; } lParam = (int)(((uint)scancode << 16) | 1); if (keyArgs.RoutedEvent == Keyboard.PreviewKeyDownEvent/*keyArgs.IsDown*/) { keyop = test ? TextServicesContext.KeyOp.TestDown : TextServicesContext.KeyOp.Down; } else { // Previous key state and transition state always 1 for WM_KEYUP. lParam |= (1 << 31) | (1 << 30); keyop = test ? TextServicesContext.KeyOp.TestUp : TextServicesContext.KeyOp.Up; } return context.Keystroke(wParam, lParam, keyop); } private bool IsSysKeyDown() { if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt) || Keyboard.IsKeyDown(Key.F10)) return true; return false; } #endregion Private methods //----------------------------------------------------- // // Private Properties // //----------------------------------------------------- //------------------------------------------------------ // // Private Fields // //----------------------------------------------------- #region Private Fields ////// Critical - required elevations to create. /// [SecurityCritical] private readonly InputManager _inputManager; #endregion Private Fields } } // 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
- HttpSysSettings.cs
- TimeSpanMinutesOrInfiniteConverter.cs
- XmlNamespaceManager.cs
- CompilationRelaxations.cs
- IndentedWriter.cs
- MemoryRecordBuffer.cs
- XmlLanguage.cs
- RuntimeCompatibilityAttribute.cs
- DataGridViewComboBoxColumn.cs
- ReflectEventDescriptor.cs
- BindingExpression.cs
- SchemaManager.cs
- SqlCaseSimplifier.cs
- StopRoutingHandler.cs
- FontWeightConverter.cs
- MaxMessageSizeStream.cs
- DataServiceRequestException.cs
- ParseNumbers.cs
- LicenseContext.cs
- HttpRuntime.cs
- KeyPullup.cs
- MatrixAnimationUsingKeyFrames.cs
- SqlDataRecord.cs
- AggregateException.cs
- Variable.cs
- BitmapDownload.cs
- ChunkedMemoryStream.cs
- AnnotationResource.cs
- HTTPNotFoundHandler.cs
- ProfilePropertySettingsCollection.cs
- SpoolingTaskBase.cs
- DataDocumentXPathNavigator.cs
- PaintValueEventArgs.cs
- MethodExpr.cs
- SchemaImporterExtensionElementCollection.cs
- _SslSessionsCache.cs
- HttpModuleCollection.cs
- StringSorter.cs
- StringStorage.cs
- ProxyWebPartManagerDesigner.cs
- TraceInternal.cs
- ItemCheckEvent.cs
- hresults.cs
- ComponentRenameEvent.cs
- ConfigurationPropertyCollection.cs
- IResourceProvider.cs
- Quad.cs
- HwndAppCommandInputProvider.cs
- ActivityExecutionContext.cs
- PeerCredentialElement.cs
- NoClickablePointException.cs
- ProtocolsConfigurationEntry.cs
- Timer.cs
- ObjectItemCollectionAssemblyCacheEntry.cs
- UshortList2.cs
- URLAttribute.cs
- XhtmlTextWriter.cs
- XamlReaderHelper.cs
- SelectionHighlightInfo.cs
- CapabilitiesState.cs
- ProxyManager.cs
- EventNotify.cs
- AggregateException.cs
- DocumentXPathNavigator.cs
- ObjectDataSourceSelectingEventArgs.cs
- AutoGeneratedField.cs
- ListViewInsertedEventArgs.cs
- DataBindingCollection.cs
- RootNamespaceAttribute.cs
- FileLoadException.cs
- AsyncDataRequest.cs
- EntityDataSourceReferenceGroup.cs
- PieceNameHelper.cs
- DnsPermission.cs
- ChangeConflicts.cs
- SaveFileDialog.cs
- cookieexception.cs
- IsolatedStorageException.cs
- Utils.cs
- hresults.cs
- TypeGeneratedEventArgs.cs
- OleDbPropertySetGuid.cs
- SoapAttributeOverrides.cs
- MetaDataInfo.cs
- FormViewCommandEventArgs.cs
- TimeoutHelper.cs
- ResXBuildProvider.cs
- ObjRef.cs
- SqlNodeAnnotation.cs
- FontNamesConverter.cs
- EntityContainerEntitySet.cs
- FilterableAttribute.cs
- TextFindEngine.cs
- RouteTable.cs
- CodeSnippetExpression.cs
- ConfigurationLockCollection.cs
- ActiveXContainer.cs
- OleServicesContext.cs
- HttpConfigurationSystem.cs
- HtmlControl.cs