Automated 'PrtScn' keystroke acts like 'Alt+PrtScrn'
Asked Answered
L

2

8

We need a screenshot of our app for a unit test. CaptureScreen() and CopyFromScreen() somehow ignore the app and return pictures of an empty desktop. So we wrote this to fake a PrtScn keystroke:

public static Bitmap GetAltScreenshot()
{
    Clipboard.Clear();
    SendKeys.SendWait("{PRTSC}");
    while (!Clipboard.ContainsImage())
    {
        Thread.Sleep(500);
    }
    return new Bitmap(Clipboard.GetImage());
}

Alt isn't part of the keystroke, so this should return a bitmap of the entire screen. Yet somehow this snippet returns just the focused window. Which is fine, that solves our problem - but we don't understand how.

Why does this return a shot of just the focused window, instead of the entire monitor?

Latham answered 19/7, 2013 at 18:51 Comment(5)
You should look at: #5049622Sideshow
@GeorgeJohnston Believe me we've tried using g.CopyFromScreen before, as well as every other capture method we could find. They all performed fine on my machine, but gave weird results on other machines within the company.Latham
Could it possibly be that the SendKeys class just interprets the "{PRTSC}" key string to mean the Alt + PrtScn key combination? Also, you might be able to just manually set the focus to be the whole screeen? link at the first note.Insinuating
Have you tried doing a SendKeys.Flush() before sending the {PRTSC}, it's kind of a long shot but just to be sure you don't have other messages in the queue it might at least be worth ruling out.Pillory
@Brian SendKeys.Flush() has no effect. The code works perfectly with or without it.Latham
P
8

There is in fact a "reason", turn to the MSDN Library article that documents the key abbreviations you can use. Note the entry for PRINT SCREEN:

{PRTSC} (reserved for future use)

The is a somewhat clumsy way of saying "We know it doesn't work, maybe will fix that some day". That day hasn't yet arrived. So you are probably testing the failure mode of this key and actually like the way it works. This is of course not healthy, they may actually fix the problem some day and break your program.

Do note the Note about the <appSettings> entry that you can add to your .config file, further down that same MSDN page. I suspect, but do not know for a fact, that the SendInput method is more reliable.

Pascual answered 22/7, 2013 at 16:28 Comment(2)
Yes, I am aware of that "reason" (In fact, I think an old post from you pointed me there) but I was more or less curious to know the inner workings of this mystery. I suppose this is as much as Microsoft is willing to let us know at this.Latham
I really don't know the underlying reason. The journaling hook they use without the appsetting is, erm, quirky.Pascual
W
0

Try using "^{PRTSC}"

Weksler answered 13/9, 2023 at 8:8 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Agriculturist

© 2022 - 2025 — McMap. All rights reserved.