Search code examples
c++imagescreenshot

Take screenshot using virtual keys using c++


I want to make a program which takes screenshot whenever it runs. For that, I thought of using virtual keys but i am not able to press two keys simultaneously. I am trying to do this in Microsoft windows 8.1 and trying to press Windows Key + Print Scrn simultaneously.


Solution

  • You can send multiple keys to the OS with keybd_event(). The first time you call it you will send the windows key and tell it to stay down. Then you will do the same with the printscreen button. After you do that you need to call the function again to lift each key in reverse order. You should be able to use:

    keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, 0);
    keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY, 0);
    keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);