0

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.

AbyxDev
  • 1,363
  • 16
  • 30
  • Where would the "Current" selection be taken from? If it should be taken from the application that is currently in focus (which might not be your app) that might not be possible. – Alen Genzić Jul 20 '18 at 13:20
  • @AlenGenzić The current selection is always another app (there's no text displayed in my own app). As far as I know it is possible, using the method I mentioned in the question, but the application has to remain in focus when using that method, which is why a keyboard shortcut would be ideal since it doesn't draw focus away. – AbyxDev Jul 20 '18 at 13:27
  • @rs232 I mentioned in the question that I had already read the article linked to from that answer, and I've mentioned how it didn't solve my problem. I couldn't find the link to the original answer that led me to the article, though, so thanks for the link. I've edited the question. – AbyxDev Jul 20 '18 at 13:32
  • @KenHilton I see. I remove my comment. – rs232 Jul 20 '18 at 13:32
  • Sounds like you need a global keyboard hook. Please see [this article](https://www.codeproject.com/Articles/28064/Global-Mouse-and-Keyboard-Library) – Chris Dunaway Jul 20 '18 at 13:55
  • @ChrisDunaway either the library you linked is outdated or its keyboard detection is limited to within the app, neither of which accomplishes my purpose. – AbyxDev Jul 20 '18 at 14:33
  • The library is indeed older, but it still works. I just tested it using VS 2015. Did you look at the Hook Sample Application? It detects mouse and keyboard activities even when it does not have focus. It detected my keystrokes when I was in notepad just fine. – Chris Dunaway Jul 20 '18 at 15:21

1 Answers1

0

I ended up using the library that Chris Dunaway mentioned, MouseKeyboardLibrary.

As you may have seen in the comment thread where he brought it up, it initially didn't work for me. It turned out that the library didn't work for me because of an invalid module IntPtr, so I changed the library to use IntPtr.Zero where it previously used Marshal to get the module pointer, which made it work as expected.

AbyxDev
  • 1,363
  • 16
  • 30