'is there a way to permanently draw on desktop screen without looping using win32gui?

I am trying to make an annotation program using python and was wondering if there is a more efficient and effective way to draw on the desktop screen. As you can see, the code below make use of while loops and nested for loops to draw a rectangle at the cursor location when 'a' key is pressed that stays on the screen for 3 seconds and disappears before 3 seconds if the user pressed 'a' key again. This program quits when '.' key is pressed. I have run this program through cmd as admin to make it work properly.

This program has made other programs slightly slower and I am trying to find a better way to carry out what I am doing. An example would be like epic pen where the drawings stays on screen and doesn't blink due to the for loop but epic pen doesn't work for me because I want to annotate immediately using the 'a' key, with epic pen I have to press 3 keys (the shortcut key to draw) and a mouse click for a dot to appear and then another 3 keys to exit drawing mode.

Can someone give me some guidance on this, perhaps I have to use another programming language to do this?

import win32gui, win32ui, win32api
from win32api import GetSystemMetrics
from threading import Timer
import keyboard
import time

itsNot3Sec = True

lifted = False
lifted2 = False


def makeItFalse():
    global itsNot3Sec
    itsNot3Sec = False




dc = win32gui.GetDC(0)
dcObj = win32ui.CreateDCFromHandle(dc)
hwnd = win32gui.WindowFromPoint((0, 0))
monitor = (0, 0, GetSystemMetrics(0), GetSystemMetrics(1))
red = win32api.RGB(255, 255, 255)


yes = 0
lifted = False
lifted2 = False
while True:
    if keyboard.is_pressed('.'):
        break
    itsNot3Sec = True
    if not keyboard.is_pressed('a'):
        lifted2 = True
    if keyboard.is_pressed('a') and lifted2:
        lifted2 = False
        lifted = False
        m = list(win32gui.GetCursorPos())
        s = Timer(3.0, makeItFalse)
        s.start()
        while itsNot3Sec:
            for x in range(10):
                win32gui.SetPixel(dc, int(m[0] * 1.25) + x, int(m[1] * 1.25), red)
                win32gui.SetPixel(dc, int(m[0] * 1.25) + x, int(m[1] * 1.25) + 10, red)
                for y in range(10):
                    win32gui.SetPixel(dc, int(m[0] * 1.25), int(m[1] * 1.25) + y, red)
                    win32gui.SetPixel(dc, int(m[0] * 1.25) + 10, int(m[1] * 1.25) + y, red)
            if not keyboard.is_pressed('a'):
                lifted = True
            if keyboard.is_pressed('a') and lifted:
                s.cancel()
                lifted = False
                itsNot3Sec = False


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source