/// <summary> /// Attaches this adorner to the adorned element /// </summary> public void Attach() { if (attached) { return; } Log("Attach begin"); if (!oneOfAssociatedElements.IsLoaded) { // Delay attaching oneOfAssociatedElements.Loaded += OnDelayAttach; return; } // Focus current adorned element // Keyboard.Focus(adornedElement); focusedElement = Keyboard.FocusedElement; if (focusedElement != null) { Log("Focus Attached to " + focusedElement.ToString()); focusedElement.LostKeyboardFocus += OnFocusLost; focusedElement.PreviewKeyDown += OnPreviewKeyDown; focusedElement.PreviewKeyUp += OnPreviewKeyUp; } else { Log("[!] Focus Setup Failed"); } GetTopLevelElement(oneOfAssociatedElements).PreviewMouseDown += OnInputActionOccured; // Show this adorner adornerLayer = GetAdornerLayer(oneOfAssociatedElements); adornerLayer.Add(this); // Clears previous user input enteredKeys = ""; FilterKeyTips(); // Hookup window activation attachedHwndSource = ((HwndSource)PresentationSource.FromVisual(oneOfAssociatedElements)); if (attachedHwndSource != null) { attachedHwndSource.AddHook(WindowProc); } // Start timer to track focus changing if (timerFocusTracking == null) { timerFocusTracking = new DispatcherTimer(DispatcherPriority.ApplicationIdle, Dispatcher.CurrentDispatcher); timerFocusTracking.Interval = TimeSpan.FromMilliseconds(50); timerFocusTracking.Tick += OnTimerFocusTrackingTick; } timerFocusTracking.Start(); attached = true; Log("Attach end"); }
private void MyMouseDown(object sender, MouseEventArgs e) { IInputElement sourceElement = e.MouseDevice.Captured; if (sourceElement != null) { captureButton.Content = sourceElement.ToString(); } else { captureButton.Content = "NULL"; } }
public void GetFocusedElement() { //<SnippetFocusGetSetFocusedElement> // Sets the focused element in focusScope1 // focusScope1 is a StackPanel. FocusManager.SetFocusedElement(focusScope1, button2); // Gets the focused element for focusScope 1 IInputElement focusedElement = FocusManager.GetFocusedElement(focusScope1); //</SnippetFocusGetSetFocusedElement> MessageBox.Show(focusedElement.ToString()); }
private void MouseMoveHighlightCapture(object sender, MouseEventArgs e) { IInputElement sourceElement = e.MouseDevice.Captured; if (sourceElement != null) { captureTarget.Content = sourceElement.ToString(); } else { captureTarget.Content = "NULL"; } Control sourceControl = sourceElement as Control; if (sourceControl != null) { sourceControl.Background = Brushes.Blue; } // captureTarget.Content = "BLAH"; }
public static bool DataGridHasFocus(DependencyObject instance) { //how to fibnd out whether a datagrid has focus or not to handle key previewers IInputElement focusedControl = FocusManager.GetFocusedElement(instance); if (focusedControl == null) { return(true); } string compare = focusedControl.ToString(); if (compare.ToUpper( ).Contains("DATAGRID")) { return(true); } else { return(false); } }
void OnFocusLost(object sender, RoutedEventArgs e) { if (attached) { Log("Focus Lost"); IInputElement previousFocusedElementElement = focusedElement; focusedElement.LostKeyboardFocus -= OnFocusLost; focusedElement.PreviewKeyDown -= OnPreviewKeyDown; focusedElement.PreviewKeyUp -= OnPreviewKeyUp; focusedElement = Keyboard.FocusedElement; if (focusedElement != null) { Log("Focus Changed from " + previousFocusedElementElement.ToString() + " to " + focusedElement.ToString()); focusedElement.LostKeyboardFocus += OnFocusLost; focusedElement.PreviewKeyDown += OnPreviewKeyDown; focusedElement.PreviewKeyUp += OnPreviewKeyUp; } else { Log("Focus Not Restored"); } } }
private void OnFocusLost(object sender, RoutedEventArgs e) { if (attached) { Log("Focus Lost"); var previousFocusedElementElement = focusedElement; focusedElement.LostKeyboardFocus -= OnFocusLost; focusedElement.PreviewKeyDown -= OnPreviewKeyDown; focusedElement.PreviewTextInput -= this.OnFocusedElementPreviewTextInput; focusedElement = Keyboard.FocusedElement; if (focusedElement != null) { Log("Focus Changed from " + previousFocusedElementElement.ToString() + " to " + focusedElement.ToString()); focusedElement.LostKeyboardFocus += OnFocusLost; focusedElement.PreviewKeyDown += OnPreviewKeyDown; focusedElement.PreviewTextInput += this.OnFocusedElementPreviewTextInput; } else { Log("Focus Not Restored"); } } }
/// <summary> /// Attaches this adorner to the adorned element /// </summary> public void Attach() { if (attached) return; Log("Attach begin"); if (!oneOfAssociatedElements.IsLoaded) { // Delay attaching oneOfAssociatedElements.Loaded += OnDelayAttach; return; } adornerLayer = GetAdornerLayer(oneOfAssociatedElements); if (adornerLayer == null) return; // Focus current adorned element // Keyboard.Focus(adornedElement); focusedElement = Keyboard.FocusedElement; if (focusedElement != null) { Log("Focus Attached to " + focusedElement.ToString()); focusedElement.LostKeyboardFocus += OnFocusLost; focusedElement.PreviewKeyDown += OnPreviewKeyDown; focusedElement.PreviewTextInput += this.OnFocusedElementPreviewTextInput; } else Log("[!] Focus Setup Failed"); GetTopLevelElement(oneOfAssociatedElements).PreviewMouseDown += OnInputActionOccured; // Show this adorner adornerLayer.Add(this); // Clears previous user input enteredKeys = ""; FilterKeyTips(); // Hookup window activation attachedHwndSource = ((HwndSource)PresentationSource.FromVisual(oneOfAssociatedElements)); if (attachedHwndSource != null) attachedHwndSource.AddHook(WindowProc); // Start timer to track focus changing if (timerFocusTracking == null) { timerFocusTracking = new DispatcherTimer(DispatcherPriority.ApplicationIdle, Dispatcher.CurrentDispatcher); timerFocusTracking.Interval = TimeSpan.FromMilliseconds(50); timerFocusTracking.Tick += OnTimerFocusTrackingTick; } timerFocusTracking.Start(); attached = true; Log("Attach end"); }
internal static void SetFocus(ManagedContent m, Func <IInputElement> activeChildFunc, bool forceSetFocus = false) { m.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action( delegate { // ensure that condition for FocusContent() is still fulfilled // (necessary to avoid focus switching loops when changing layouts) if (!forceSetFocus && !(m.IsActiveContent && !m.IsKeyboardFocusWithin)) { WpfWorkbench.FocusDebug("{0} - not moving focus (IsActiveContent={1}, IsKeyboardFocusWithin={2})", m.Title, m.IsActiveContent, m.IsKeyboardFocusWithin); return; } IInputElement activeChild = activeChildFunc(); WpfWorkbench.FocusDebug("{0} - moving focus to: {1}", m.Title, activeChild != null ? activeChild.ToString() : "<null>"); if (activeChild != null) { Keyboard.Focus(activeChild); } })); }
void OnFocusLost(object sender, RoutedEventArgs e) { if (attached) { Log("Focus Lost"); IInputElement previousFocusedElementElement = focusedElement; focusedElement.LostKeyboardFocus -= OnFocusLost; focusedElement.PreviewKeyDown -= OnPreviewKeyDown; focusedElement.PreviewKeyUp -= OnPreviewKeyUp; focusedElement = Keyboard.FocusedElement; if (focusedElement != null) { Log("Focus Changed from " + previousFocusedElementElement.ToString() + " to " + focusedElement.ToString()); focusedElement.LostKeyboardFocus += OnFocusLost; focusedElement.PreviewKeyDown += OnPreviewKeyDown; focusedElement.PreviewKeyUp += OnPreviewKeyUp; } else Log("Focus Not Restored"); } }