2

I'm not sure exactly how to start this project, but my requirements are as follows (for the parts that I'm unsure of how to implement):

  • Holding down the Tab key should display the overlay. Letting go of the tab key should hide the overlay. How would I capture this globally? What's the best way to check that the Tab key is still being held down?
  • The overlay should display over all other applications. It does not have to display over a full screened application, but it would be optimal to be able to do so.
  • Resolution scaling? Not as important, but it would be nice to have the size of the overlay scale based on the user's monitor resolution.

I haven't started this yet, but which project type would be best for this? I was thinking WPF over WindowsForms since it seems to offer a lot more flexibility in terms of presentation. Any advice on the above points would be greatly appreciated!

Lunyx
  • 3,164
  • 6
  • 30
  • 46
  • that sounds potentially malicious... what exactly is the goal of such an application ? – Yahia Sep 17 '12 at 14:22
  • You're asking lots of questions in one. Consider splitting each separate aspect into a different question. Also almost all of your 'sub questions' have duplicates here. Use the search function to find things like global hotkeys in C# and passing events through your window to the ones underneath. – PhonicUK Sep 17 '12 at 14:22
  • you better start this as a WPF project.. – techBeginner Sep 17 '12 at 14:24
  • @phonicUK, There is information concerning global hotkeys, but I haven't seen anything about how to best check if a key is still being held down or not. Is there some OnKeyRelease event that I can listen to? I'm unsure about how to implement this. Edit: Ok, on another search, I've found the answer to my second bullet point. Still need help on the others, though. – Lunyx Sep 17 '12 at 14:37

1 Answers1

4

In WPF, you can do it this way:

For the global TAB key handling, at the entry point of your application (usually App.xaml.cs), run this code:

EventManager.RegisterClassHandler(typeof(UIElement), UIElement.PreviewKeyUpEvent, new KeyEventHandler(OnPreviewKeyUp));

Use (Preview)Key(up/Down)Event as you need it, if you set e.Handled = true in a Preview event handler, the event will stop its routing and nobody else will be able to react to it.

In OnPreviewKeyUp, check that e.Key == Key.Tab, and show the overlay window if it is.


The signature of the event handler should look like:

private static void OnPreviewKeyUp(object source, KeyEventArgs e)


Now for the click-through part, make a Window and set the background of your overlay to null:

WindowStyle="None" AllowsTransparency="True" Background="{x:Null}"
Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88
  • Thanks for the quick response! I'll try that out sometime later this week when I have time to work on this. I'll accept your answer if it works. Could you explain a bit more on the global TAB key handling though? Would I have to register the OnPreviewKeyDown event as well? Also, do you mean to check e.Key == Key.Tab in the OnPreviewKeyDown event? I want the overlay shown only while the TAB key is pressed. – Lunyx Sep 17 '12 at 14:56
  • You could create the overlay window on the keydown event, and remove it on the keyup event. – Louis Kottmann Sep 17 '12 at 15:04
  • Ok, I have the click-through working, but I can't seem to get the visibility toggle to work. I am using `public static void OnPreviewKeyDown(object source, KeyEventArgs e) { if (e.Key == Key.Tab) ((Window1)source).Visibility = Visibility.Visible; } public static void OnPreviewKeyUp(object source, KeyEventArgs e) { if (e.Key == Key.Tab) ((Window1)source).Visibility = Visibility.Hidden; }` I registered the events in the main method of App.g.cs. I put breakpoints, but the debugger never hits them. – Lunyx Sep 17 '12 at 16:19
  • App.g.cs is auto-generated, don't touch it – Louis Kottmann Sep 17 '12 at 17:27