Skip to content

Instantly share code, notes, and snippets.

@wesleyel
Created October 30, 2021 04:24
Show Gist options
  • Save wesleyel/2674f7380fa981e18082e448c4d352e7 to your computer and use it in GitHub Desktop.
Save wesleyel/2674f7380fa981e18082e448c4d352e7 to your computer and use it in GitHub Desktop.
Capture window with shortcuts on windows 10
# -*- coding:utf-8 -*-
#
# Author : magicwenli
# Date : 2021-10-30 11:54:32
# LastEditTime : 2021-10-30 11:54:32
# Useful links :
#
# Virtual Keys List:
# https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
# RegisterHotKey function:
# https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
import os
import win32gui
import win32con
import win32api
import ctypes
import ctypes.wintypes
import threading
import time
from PIL import ImageGrab
from ctypes import windll
WINTITLE = "League of Legends (TM) Client"
SAVEPATH = './save/'
# Ctrl + Q
shortcut_capture = (win32con.MOD_CONTROL, 0x51)
# Ctrl + W
shortcut_exit = (win32con.MOD_CONTROL, 0x52)
def getGameWindow():
window = win32gui.FindWindow(None, WINTITLE)
if not window:
return -1
win32gui.SetForegroundWindow(window)
try:
f = ctypes.windll.dwmapi.DwmGetWindowAttribute
except WindowsError:
f = None
if f: # Vista & 7 stuff
rect = ctypes.wintypes.RECT()
DWMWA_EXTENDED_FRAME_BOUNDS = 9
f(ctypes.wintypes.HWND(window),
ctypes.wintypes.DWORD(DWMWA_EXTENDED_FRAME_BOUNDS),
ctypes.byref(rect),
ctypes.sizeof(rect)
)
# size = (rect.right - rect.left, rect.bottom - rect.top)
pos = (rect.left,rect.top,rect.right,rect.bottom)
else:
pos = win32gui.GetWindowRect(window)
print("Window at " + str(pos))
return pos
class myScreenShot(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
print("\n***Start of "+str(self.name)+"***\n")
sreenShotMain()
print("\n***End of "+str(self.name)+"***\n")
class myHotKey(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
print("\n***Start of "+str(self.name)+"***\n")
hotKeyMain()
print("\n***End of "+str(self.name)+"***\n")
def sreenShotMain():
global shotControl_command
global exitControl_command
while(True):
if exitControl_command == True:
exitControl_command = False
print("Exit this program!")
return
if shotControl_command == True:
pos = getGameWindow()
if pos == -1:
print('Get window fail: {}'.format(WINTITLE))
else:
name = SAVEPATH + str(int(time.time()))+'.jpg'
pos = [int(x) for x in list(pos)]
img = ImageGrab.grab(bbox=pos)
img.save(name)
print("Save to ", name)
shotControl_command = False
def hotKeyMain():
global shotControl_command
global exitControl_command
user32 = ctypes.windll.user32
while(True):
# win+f9=screenshot
if not user32.RegisterHotKey(None, 98, shortcut_capture[0],shortcut_capture[1]):
print("Unable to register id", 98)
# alt+1=exit program
if not user32.RegisterHotKey(None, 99, shortcut_exit[0],shortcut_exit[1]):
print("Unable to register id", 99)
try:
msg = ctypes.wintypes.MSG()
if user32.GetMessageA(ctypes.byref(msg), None, 0, 0) != 0:
if msg.message == win32con.WM_HOTKEY:
if msg.wParam == 99:
exitControl_command = True
return
elif msg.wParam == 98:
shotControl_command = True
user32.TranslateMessage(ctypes.byref(msg))
user32.DispatchMessageA(ctypes.byref(msg))
finally:
del msg
user32.UnregisterHotKey(None, 98)
user32.UnregisterHotKey(None, 99)
if __name__ == "__main__":
# Make program aware of DPI scaling
user32 = windll.user32
user32.SetProcessDPIAware()
shotControl_command = False
exitControl_command = False
thread_screenShot = myScreenShot("thread_screenShot")
thread_hotKey = myHotKey("thread_hotKey")
thread_screenShot.start()
thread_hotKey.start()
thread_hotKey.join()
thread_screenShot.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment