I'm trying to create an application that will process selected text when a keyboard shortcut is pressed. I used part of this method to get the currently selected text (and there seems to be no other way to get the selected text from any application:
string GetSelectedText()
{
var oldClip = Clipboard.GetText();
SendKeys.SendWait("^c");
string selection = Clipboard.GetText();
Clipboard.SetText(oldClip); // preserve clipboard
return selection;
}
My first thought was to use a system tray icon. When clicked, the icon would use the above method to get the current selection, and process it. However, using the clipboard like that requires the application to be in focus, whereas clicking a system tray icon brings the icon into focus before the method can run.
My next thought was to use a global keyboard shortcut. I came across this article about getting a global hotkey, and it could in fact listen to keyboard combinations, but only within one app, which is not what I want - I want to listen to the keyboard shortcut across any app.
I have already read this article (which came from this answer) which does manage to listen for keys across any app but only manages to listen to one key at a time (e.g. a single LControl, or Shift, or C, etc).
My question here is: How can I listen for a certain combination of keys, no matter what application is in focus, and execute the method above to get the current selection?
If there is a better way to retrieve the current selection, or if it can be done when the application is not in focus, answers explaning how it is possible are also appreciated; the main focus, however, is listening for a keyboard combination across all applications.