Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Printscreen key and SFML  (Read 3645 times)

0 Members and 1 Guest are viewing this topic.

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Printscreen key and SFML
« on: July 26, 2015, 11:20:27 am »
Hi! :)
I have a few questions about the key in the title.

First of all, I know how to capture current frame from the fullscreen window and save it to file:
if (mEvent.key.code == sf::Keyboard::F12)
{
    sf::Image screenshot = mWindow.capture();
    screenshot.saveToFile("screenshot.png");
}
 

But I got soo used to in using the Printscreen key for making screenshots, since majority of games I played would support it, either by making the image file or by capturing the frame and then pasting it in a image editor program. It is kinda intuitive.  ;D

So, what is the reason that SFML doesn't support the printscreen key?
Also, is there a way to force a program into using the printscreen key the way I want (as code above)?

I was searching a bit and found this topic, but since I extremely lack the OS* inner workings knowledge, I have no idea how implement the code from that topic into my code.  :-\
Which means that the second question basically boils down to "can you write the code for me, pleasee?" ;D
Or just point me at right direction on how to implement it.  ;)

PS: I am a c++ and a programer noob, just so you know...

* OS is Windows 7.

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Printscreen key and SFML
« Reply #1 on: July 26, 2015, 12:09:39 pm »
Not sure if the code you linked to works but I think its intention is to copy the window into the clipboard. If you're just capturing the window and saving that capture, SFML's window.capture() is perfectly fine, just as you've used it.
However, to grab the Print Screen key (on Windows), as in the topic that you linked, is simply:
GetAsyncKeyState(VK_SNAPSHOT)
which tells you if the Print Screen key is being pressed at that time (similar to SFML's isKeyPressed()).
Be aware that using this code stops your code being portable and you should consider wrapping it to make sure it's only used on Windows, and also provide an option for other operating systems to use (maybe a different key).

#ifdef SFML_SYSTEM_WINDOWS
                if (GetAsyncKeyState(VK_SNAPSHOT))
                {
                        window.capture().saveToFile("screenshot.png");
                }
#endif // SFML_SYSTEM_WINDOWS
This is not an event - it's a realtime query - so it would need to be used outside of the event loop. This would constantly update the screenshot as quickly as it can (capture is pretty slow though) so you might want to delay this test after it's been activated but that might not be necessary.
« Last Edit: July 26, 2015, 12:12:40 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Re: Printscreen key and SFML
« Reply #2 on: July 26, 2015, 01:47:55 pm »
Quote
Not sure if the code you linked to works but I think its intention is to copy the window into the clipboard. If you're just capturing the window and saving that capture, SFML's window.capture() is perfectly fine, just as you've used it.

Actually, I was only interested in how to use printscreen key, and that topic was the only one I found that explained it to some extent. I was not interested what to do with the image afterwards in that topic. :)

Quote
#ifdef SFML_SYSTEM_WINDOWS
        if (GetAsyncKeyState(VK_SNAPSHOT))
        {
            window.capture().saveToFile("screenshot.png");
        }
#endif // SFML_SYSTEM_WINDOWS
I haven't thought it would be that simple. :D

Quote
This is not an event - it's a realtime query - so it would need to be used outside of the event loop. This would constantly update the screenshot as quickly as it can (capture is pretty slow though) so you might want to delay this test after it's been activated but that might not be necessary.

As you said, it behaves as SFML's "IsKeyPressed()"...
So, a simple boolean value should be enough to stop it from being called each frame.

Quote
Be aware that using this code stops your code being portable and you should consider wrapping it to make sure it's only used on Windows, and also provide an option for other operating systems to use (maybe a different key).

I guess that would also explain why SFML doesn't support it in the first place.
(also, I believe I got some things that already makes my code non-portable, but that is not the main point of this topic)

---------------------------------------------------------------

Already tested it and it works as intended!
Thanks a lot! :D
« Last Edit: July 26, 2015, 01:49:40 pm by Brax »

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Printscreen key and SFML
« Reply #3 on: July 26, 2015, 07:44:01 pm »
So, a simple boolean value should be enough to stop it from being called each frame.
Exactly, but I'm not sure it's necessary. Capturing is window is slow so the key would probably be released before it's finished saving. Even if it isn't, it would just capture again if the key is still pressed, which kind of makes sense anyway but it could also be useful to grab multiple frames (just change the filename each time - probably using a counter).

Already tested it and it works as intended!
Thanks a lot! :D
You're welcome  :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything