views:

495

answers:

2

How can I make my C# application the default "Print Screen" handler in Windows?

I wrote a screen capturing utility and I want to, ideally, have it replace the default print-screen handler, or otherwise have a unique key combination that would trigger it.

I know how to do it in C++ using global hooks, etc., but it's not clear to me how this can be done in .Net. Also, if there's a way of doing it without using a memory resident application that would be super.

+1  A: 

Why not use AutoHotKey (OpenSource, free) with the following script?

This script was taken from the newsgroup of AutoHotKey, it will map the printscreen to use IrfanView. You can use it to trigger your program.

~printscreen::
ifexist,%screenshot1%\irfanview.exe
    {
    run, "%screenshot1%\irfanview.exe" "/capture=0 /convert=%screenshots%\%A_now%_screenshot.jpg"
   sleep,500
   run,%screenshots%  ;open folder
   return
    }
return


~!printscreen::       ;copy only active window
ifexist,%screenshot1%\irfanview.exe
    {
Shay Erlichmen
A good suggestion. I do want to figure out how to do this in code before I insert a 3rd party dependency into my installer. But I'll definitely look into it.
Assaf Lavie
AutoHotKey is really a standalone application which I cannot customize and package as part of my application easily. It's also roughly 2MB in size. As a generic shortcut tool it's great, but it's not a shortcut plugin for existing applications.
Assaf Lavie
+1  A: 

As long as you are using a WM_KEYBOARD_LL hook to capture the PrintScreen key, there is no reason you can't do this in C#. It's one of the few global hooks that can be used in managed code because it doesn't require a DLL to be injected into every target process. Instead the context will switch back to the originating application for processing.

Adam's blog has a great post on how to setup such a hook in C#: http://www.seesharpdot.net/?tag=wh_keyboard_ll

JaredPar
I'll try it. But it does mean my program has to be running in order to capture the key-down, right?
Assaf Lavie
@Assaf, yes, your programm will need to be running.
JaredPar