I have a small tray application that registers a hotkey for the entire system. When a user selects text anywhere in any application and presses this hotkey, I want to be able to capture the selected text. I am currently doing this using AutomationElements:
//Using FocusedElement (since the focused element should be the control with the selected text?) AutomationElement ae = AutomationElement.FocusedElement; AutomationElement txtElement = ae.FindFirst(TreeScope.Subtree,Condition.TrueCondition); if(txtElement == null) return; TextPattern tp; try { tp = txtElement.GetCurrentPattern(TextPattern.Pattern) as TextPattern; } catch(Exception ex) { return; } TextPatternRange[] trs; if (tp.SupportedTextSelection == SupportedTextSelection.None) { return; } else { trs = tp.GetSelection(); string selectedText = trs[0].GetText(-1); MessageBox.Show(selectedText ); }
This works for some applications (such as notepads, visual studio editing windows, etc.), but not for all (e.g. Word, FireFox, Chrome, etc.)
Anyone here with any ideas on how to be able to pull out the selected text in ANY program?
nelshh
source share