システム全体のホットキーを登録する小さなトレイ アプリケーションがあります。ユーザーがアプリケーションの任意の場所でテキストを選択してこのホットキーを押すと、選択したテキストをキャプチャできるようにしたいと考えています。現在、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 );
}
これは、一部のアプリ (メモ帳、ビジュアル スタジオの編集ボックスなど) では機能しますが、すべてのアプリ (Word、FireFox、Chrome など) では機能しません。
任意のアプリケーションで選択したテキストを取得できるようにする方法について何かアイデアを持っている人はいますか?