// This method ensures that whenever focus is not acquired // but MenuMode has been pushed with the expection, a // corresponding PopMenu is performed. private static void OnKeyboardInputProviderAcquireFocus(object sender, KeyboardInputProviderAcquireFocusEventArgs e) { MenuBase menu = (MenuBase) sender; if (!menu.IsKeyboardFocusWithin && !e.FocusAcquired && menu.IsAcquireFocusMenuMode) { Debug.Assert(menu.HasPushedMenuMode); // The input provider did not acquire focus. So we will not // succeed in setting focus to the desired element within the // menu. menu.PopMenuMode(); } }
// This method ensures that whenever focus is given to an element // within a menu, that the menu enters menu mode. This can't be // done with a simple IsFocusWithin changed handler because we // need to actually enter menu mode before focus changes. private static void OnPreviewKeyboardInputProviderAcquireFocus(object sender, KeyboardInputProviderAcquireFocusEventArgs e) { MenuBase menu = (MenuBase) sender; // If we haven't already pushed menu mode, we need to do it before // focus enters the menu for the first time if (!menu.IsKeyboardFocusWithin && !menu.HasPushedMenuMode) { // Call PushMenuMode just before focus enters the menu... menu.PushMenuMode(/*isAcquireFocusMenuMode*/ true); } }
private void TryChangeFocus(DependencyObject newFocus, IKeyboardInputProvider keyboardInputProvider, bool askOld, bool askNew, bool forceToNullIfFailed) { bool changeFocus = true; int timeStamp = Environment.TickCount; DependencyObject oldFocus = _focus; // This is required, used below to see if focus has been delegated if (newFocus != _focus) { // If requested, and there is currently something with focus // Send the PreviewLostKeyboardFocus event to see if the object losing focus want to cancel it. // - no need to check "changeFocus" here as it was just previously unconditionally set to true if (askOld && _focus != null) { KeyboardFocusChangedEventArgs previewLostFocus = new KeyboardFocusChangedEventArgs(this, timeStamp, (IInputElement)_focus, (IInputElement)newFocus); previewLostFocus.RoutedEvent = Keyboard.PreviewLostKeyboardFocusEvent; previewLostFocus.Source = _focus; if (_inputManager != null) { _inputManager.Value.ProcessInput(previewLostFocus); } // is handled the right indication of canceled? if (previewLostFocus.Handled) { changeFocus = false; } } // If requested, and there is an object to specified to take focus // Send the PreviewGotKeyboardFocus event to see if the object gaining focus want to cancel it. // - must also check "changeFocus", no point in checking if the "previewLostFocus" event // above already cancelled it if (askNew && changeFocus && newFocus != null) { KeyboardFocusChangedEventArgs previewGotFocus = new KeyboardFocusChangedEventArgs(this, timeStamp, (IInputElement)_focus, (IInputElement)newFocus); previewGotFocus.RoutedEvent = Keyboard.PreviewGotKeyboardFocusEvent; previewGotFocus.Source = newFocus; if (_inputManager != null) { _inputManager.Value.ProcessInput(previewGotFocus); } // is handled the right indication of canceled? if (previewGotFocus.Handled) { changeFocus = false; } } // If we are setting the focus to an element, see if the InputProvider // can take focus for us. if (changeFocus && newFocus != null) { if (keyboardInputProvider != null && Keyboard.IsFocusable(newFocus)) { // Tell the element we are about to acquire focus through // the input provider. The element losing focus and the // element receiving focus have all agreed to the // transaction. This is used by menus to configure the // behavior of focus changes. KeyboardInputProviderAcquireFocusEventArgs acquireFocus = new KeyboardInputProviderAcquireFocusEventArgs(this, timeStamp, changeFocus); acquireFocus.RoutedEvent = Keyboard.PreviewKeyboardInputProviderAcquireFocusEvent; acquireFocus.Source = newFocus; if (_inputManager != null) { _inputManager.Value.ProcessInput(acquireFocus); } // Acquire focus through the input provider. changeFocus = keyboardInputProvider.AcquireFocus(false); // Tell the element whether or not we were able to // acquire focus through the input provider. acquireFocus = new KeyboardInputProviderAcquireFocusEventArgs(this, timeStamp, changeFocus); acquireFocus.RoutedEvent = Keyboard.KeyboardInputProviderAcquireFocusEvent; acquireFocus.Source = newFocus; if (_inputManager != null) { _inputManager.Value.ProcessInput(acquireFocus); } } else { changeFocus = false; } } // If the ChangeFocus operation was cancelled or the AcquireFocus operation failed // and the "ForceToNullIfFailed" flag was set, we set focus to null if (!changeFocus && forceToNullIfFailed && oldFocus == _focus /* Focus is not delegated */) { // focus might be delegated (e.g. during PreviewGotKeyboardFocus) // without actually changing, if it was already on the delegated // element. We can't test for this directly, // but if focus is within the desired element we'll assume this // is what happened. IInputElement newFocusElement = newFocus as IInputElement; if (newFocusElement == null || !newFocusElement.IsKeyboardFocusWithin) { newFocus = null; changeFocus = true; } } // If both the old and new focus elements allowed it, and the // InputProvider has acquired it, go ahead and change our internal // sense of focus to the desired element. if (changeFocus) { ChangeFocus(newFocus, timeStamp); } } }
private void TryChangeFocus(DependencyObject newFocus, IKeyboardInputProvider keyboardInputProvider, bool askOld, bool askNew, bool forceToNullIfFailed) { bool changeFocus = true; int timeStamp = Environment.TickCount ; DependencyObject oldFocus = _focus; // This is required, used below to see if focus has been delegated if(newFocus != _focus) { // If requested, and there is currently something with focus // Send the PreviewLostKeyboardFocus event to see if the object losing focus want to cancel it. // - no need to check "changeFocus" here as it was just previously unconditionally set to true if(askOld && _focus != null) { KeyboardFocusChangedEventArgs previewLostFocus = new KeyboardFocusChangedEventArgs(this, timeStamp, (IInputElement)_focus, (IInputElement)newFocus); previewLostFocus.RoutedEvent=Keyboard.PreviewLostKeyboardFocusEvent; previewLostFocus.Source= _focus; if(_inputManager != null) _inputManager.Value.ProcessInput(previewLostFocus); // if(previewLostFocus.Handled) { changeFocus = false; } } // If requested, and there is an object to specified to take focus // Send the PreviewGotKeyboardFocus event to see if the object gaining focus want to cancel it. // - must also check "changeFocus", no point in checking if the "previewLostFocus" event // above already cancelled it if(askNew && changeFocus && newFocus != null) { KeyboardFocusChangedEventArgs previewGotFocus = new KeyboardFocusChangedEventArgs(this, timeStamp, (IInputElement)_focus, (IInputElement)newFocus); previewGotFocus.RoutedEvent=Keyboard.PreviewGotKeyboardFocusEvent; previewGotFocus.Source= newFocus; if(_inputManager != null) _inputManager.Value.ProcessInput(previewGotFocus); // if(previewGotFocus.Handled) { changeFocus = false; } } // If we are setting the focus to an element, see if the InputProvider // can take focus for us. if(changeFocus && newFocus != null) { if (keyboardInputProvider != null && Keyboard.IsFocusable(newFocus)) { // Tell the element we are about to acquire focus through // the input provider. The element losing focus and the // element receiving focus have all agreed to the // transaction. This is used by menus to configure the // behavior of focus changes. KeyboardInputProviderAcquireFocusEventArgs acquireFocus = new KeyboardInputProviderAcquireFocusEventArgs(this, timeStamp, changeFocus); acquireFocus.RoutedEvent = Keyboard.PreviewKeyboardInputProviderAcquireFocusEvent; acquireFocus.Source= newFocus; if(_inputManager != null) _inputManager.Value.ProcessInput(acquireFocus); // Acquire focus through the input provider. changeFocus = keyboardInputProvider.AcquireFocus(false); // Tell the element whether or not we were able to // acquire focus through the input provider. acquireFocus = new KeyboardInputProviderAcquireFocusEventArgs(this, timeStamp, changeFocus); acquireFocus.RoutedEvent = Keyboard.KeyboardInputProviderAcquireFocusEvent; acquireFocus.Source= newFocus; if(_inputManager != null) _inputManager.Value.ProcessInput(acquireFocus); } else { changeFocus = false; } } // If the ChangeFocus operation was cancelled or the AcquireFocus operation failed // and the "ForceToNullIfFailed" flag was set, we set focus to null if( !changeFocus && forceToNullIfFailed && oldFocus == _focus /* Focus is not delegated */ ) { // focus might be delegated (e.g. during PreviewGotKeyboardFocus) // without actually changing, if it was already on the delegated // element (see bug 1794057). We can't test for this directly, // but if focus is within the desired element we'll assume this // is what happened. IInputElement newFocusElement = newFocus as IInputElement; if (newFocusElement == null || !newFocusElement.IsKeyboardFocusWithin) { newFocus = null; changeFocus = true; } } // If both the old and new focus elements allowed it, and the // InputProvider has acquired it, go ahead and change our internal // sense of focus to the desired element. if(changeFocus) { ChangeFocus(newFocus, timeStamp); } } }
private void button9_KeyboardInputProviderAcquireFocus(object sender, KeyboardInputProviderAcquireFocusEventArgs e) { Variables.Game = Variables.Game9; Variables.GameImage = Variables.ImageHandle9; Variables.GameArgs = Variables.G9Args; Storyboard button9Anim = (Storyboard)FindResource("button9Anim"); button9Anim.Begin(this); }