0

Description

I'm encountering the problem with third party app which is somehow recognising my activities. I's not allowing to do screenshot of the app window.

For some reason when i use keyboard, pyautogui, button press simulation the app is preventing to do screen shot of the window. The most important is that when im using keyboard button PrtScr or On-window keyboard button then the app is allowing screenshot and save it in api folder "screenshots" - it is made automatically. The only one need is to have active window of the app.

Already used methods 1)keyboard keyboard.press('print_screen') keyboard.release('print_screen') 2) pyautogui.screenshot() 3)

import ctypes
from ctypes import wintypes
import time

user32 = ctypes.WinDLL('user32', use_last_error=True)

INPUT_MOUSE    = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2

KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP       = 0x0002
KEYEVENTF_UNICODE     = 0x0004
KEYEVENTF_SCANCODE    = 0x0008

MAPVK_VK_TO_VSC = 0

VK_TAB  = 0x09
VK_MENU = 0x12
VK_SNAPSHOT = 0x2C
key_3 = 0x33


wintypes.ULONG_PTR = wintypes.WPARAM

class MOUSEINPUT(ctypes.Structure):
    _fields_ = (("dx",          wintypes.LONG),
                ("dy",          wintypes.LONG),
                ("mouseData",   wintypes.DWORD),
                ("dwFlags",     wintypes.DWORD),
                ("time",        wintypes.DWORD),
                ("dwExtraInfo", wintypes.ULONG_PTR))

class KEYBDINPUT(ctypes.Structure):
    _fields_ = (("wVk",         wintypes.WORD),
                ("wScan",       wintypes.WORD),
                ("dwFlags",     wintypes.DWORD),
                ("time",        wintypes.DWORD),
                ("dwExtraInfo", wintypes.ULONG_PTR))

    def __init__(self, *args, **kwds):
        super(KEYBDINPUT, self).__init__(*args, **kwds)
        # some programs use the scan code even if KEYEVENTF_SCANCODE
        # isn't set in dwFflags, so attempt to map the correct code.
        if not self.dwFlags & KEYEVENTF_UNICODE:
            self.wScan = user32.MapVirtualKeyExW(self.wVk,
                                                 MAPVK_VK_TO_VSC, 0)

class HARDWAREINPUT(ctypes.Structure):
    _fields_ = (("uMsg",    wintypes.DWORD),
                ("wParamL", wintypes.WORD),
                ("wParamH", wintypes.WORD))

class INPUT(ctypes.Structure):
    class _INPUT(ctypes.Union):
        _fields_ = (("ki", KEYBDINPUT),
                    ("mi", MOUSEINPUT),
                    ("hi", HARDWAREINPUT))
    _anonymous_ = ("_input",)
    _fields_ = (("type",   wintypes.DWORD),
                ("_input", _INPUT))

LPINPUT = ctypes.POINTER(INPUT)

def _check_count(result, func, args):
    if result == 0:
        raise ctypes.WinError(ctypes.get_last_error())
    return args

user32.SendInput.errcheck = _check_count
user32.SendInput.argtypes = (wintypes.UINT, # nInputs
                             LPINPUT,       # pInputs
                             ctypes.c_int)  # cbSize

def PressKey(hexKeyCode):
    x = INPUT(type=INPUT_KEYBOARD,
              ki=KEYBDINPUT(wVk=hexKeyCode))
    user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))

def ReleaseKey(hexKeyCode):
    x = INPUT(type=INPUT_KEYBOARD,
              ki=KEYBDINPUT(wVk=hexKeyCode,
                            dwFlags=KEYEVENTF_KEYUP))
    user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))

def Press_function():
    PressKey(key_3)
    time.sleep(0.1)
    ReleaseKey(key_3)
    time.sleep(0.1)
    PressKey(VK_SNAPSHOT)
    time.sleep(0.1)
    ReleaseKey(VK_SNAPSHOT)

if __name__ == "__main__":
    time.sleep(1)
    Press_function()



Looking for solution I would like to have the script which allow me to do the screenshot even in this dedicated folder but without my additional action "pressing the button"

Looks liek that app is somehow recognizing only button "trigger" but i'm not sure why my 3rd solution didn't work. Because for button "3" it worked →

PressKey(key_3) time.sleep(0.1) ReleaseKey(key_3)

[Screen of obscures window][1] [1]: https://i.stack.imgur.com/uqS6W.png

Maciek
  • 1
  • 1
  • 1
    Does this answer your question? [Get screenshot on Windows with Python?](https://stackoverflow.com/questions/2846947/get-screenshot-on-windows-with-python) – BoppreH Jun 19 '23 at 17:10
  • tried all hints and not working – Maciek Jun 20 '23 at 16:08
  • When you say it doesn't allow a screenshot to be taken, do you mean that no image file is created or that the app obscures itself with the image? Do any of the methods you have tried throw an error? Can you take a screenshot if the app in question does not currently have focus? – nigh_anxiety Jun 20 '23 at 16:44
  • File is created but the app obscures itself with the black image. It allow to creat screenshot only when the window of teh app is active. What is more it save the screenshot in dedicated app folder. No errors for now when i used mentioned methods. *Screenshot added to main post – Maciek Jun 20 '23 at 17:12

1 Answers1

0

Tried with this and still not working:

#1
from PIL import ImageGrab
snapshot = ImageGrab.grab()
save_path = "C:\\Users\\YourUser\\Desktop\\MySnapshot.jpg"
snapshot.save(save_path)
#2
import PrtSc.PrtSc as Screen
screenshot=PrtSc.PrtSc(True,'filename.png')
#3
from mss import mss
with mss() as sct:
    sct.shot()
#4
import wx
app = wx.App()  # Need to create an App instance before doing anything
screen = wx.ScreenDC()
size = screen.GetSize()
bmp = wx.Bitmap(size[0], size[1])
mem = wx.MemoryDC(bmp)
mem.Blit(0, 0, size[0], size[1], screen, 0, 0)
del mem  # Release bitmap
bmp.SaveFile('screenshot.png', wx.BITMAP_TYPE_PNG)
Maciek
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 21 '23 at 21:56