C# – Prevent hotkey from being lost after a UAC prompt in Windows

chotkeysscreenshotuac

BACKGROUND

  • I wrote an app in C# that registers windows hotkeys (it takes a screenshot when I press PRINTSCREEN)
  • My code to register the hotkey, capture screen , etc all works. No issues there.

THE PROBLEM

  • IF my app is running (and successfully has registered the hotkey) and then I do something that causes a UAC prompt – usually this is caused when, for example, I launch some setup program – then the hotkey is lost. Lost = afterwards when I hit printscreen the event never comes back to my app. I have to close the app and restart.

THE QUESTIONS

  • Why is the hotkey lost?
  • What can and should I be doing different differently when registering the hotkey?

NOTES

  • I've seen this behavior with other applications that register hotkeys – running the app with the hotkey using admin privileges prevents the hotkey from being lost
  • What can and should I be doing different differently when registering the hotkey?
  • Avoiding apps like setup.exes that cause the UAC prompt is not a viable solution – the entire purpose of my taking screenshots is to document a setup experience over a number of applications
  • I have a workaround in that I can launch the app with admin rights – but I would rather avoid having to do make users do that

CODE

My code is very similar to this: http://www.codekeep.net/snippets/ea68475b-c33e-4a71-8008-90f63d7e918d.aspx

Best Answer

Given that Print Screen already takes a screenshot and puts it on the clipboard, have you considered monitoring the clipboard for changes instead?

Look at the SetClipboardViewer API function. For example.

Related Topic