[Python] I am building a teleprompter. i want my macos screen overlay to be invisible to...

Discussão em 'Python' iniciado por Stack, Abril 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am facing issue to make the screen invisible to screen sharing or while taking screenshot over the overlay. I am using python, Quartz, cocoa for accessing native MacOS API. I tried different methods but none seems working. PYQT and TKINTER are also very redundant in MACOS. I am not getting any error in displaying overlay but I am not able to make it invisible to screen sharing or screenshot. I am new to MacOS development and it seems it needs advance understanding of MacOS native API. Sample overlay code in python which I am working on to make invisible to share sharing.

    How to test if screen is invisible: 1) join any meeting solo/group and do entire screen share. It shouldn’t be visible in screen share. 2) take screenshots using shift+command+3, it shouldn’t be appearing in screenshot.

    ```python
    import objc
    import threading
    import sys
    import os

    import Quartz
    import base64
    from Cocoa import (
    NSApplication, NSApp, NSWindow, NSView, NSTextField, NSFont,
    NSColor, NSBackingStoreBuffered, NSBorderlessWindowMask,
    NSScreen, NSApplicationActivationPolicyRegular, NSApplicationActivationPolicyAccessory,
    NSEvent, NSEventMaskKeyDown, NSEventModifierFlagCommand, NSEventModifierFlagShift,
    NSMakeRect, NSImage, NSImageView, NSData
    )
    from Foundation import NSObject


    # Custom window level higher than screenshots
    NSWindowLevelScreenSaverPlus = 1001

    # Window collection behaviors
    NSWindowCollectionBehaviorCanJoinAllSpaces = 1 << 0
    NSWindowCollectionBehaviorStationary = 1 << 4
    NSWindowCollectionBehaviorFullScreenAuxiliary = 1 << 8

    class OverlayWindow(NSWindow):
    def canBecomeKeyWindow(self):
    return False

    def canBecomeMainWindow(self):
    return False

    class AppDelegate(NSObject):
    def applicationDidFinishLaunching_(self, notification):
    screen = NSScreen.mainScreen()
    screen_frame = screen.frame()
    frame = NSMakeRect(
    screen_frame.size.width * 0.25, # X: start 25% across
    screen_frame.size.height * 0.25, # Y: start 25% up
    screen_frame.size.width * 0.5, # Width: 50% of screen width
    screen_frame.size.height * 0.5 # Height: 50% of screen height
    )

    self.window = OverlayWindow.alloc().initWithContentRect_styleMask_backing_defer_(
    frame,
    NSBorderlessWindowMask,
    NSBackingStoreBuffered,
    False
    )

    self.window.setBackgroundColor_(NSColor.colorWithCalibratedWhite_alpha_(0, 0.4)) # Semi-transparent black
    self.window.setLevel_(NSWindowLevelScreenSaverPlus)
    self.window.setOpaque_(False)
    self.window.setIgnoresMouseEvents_(False)
    self.window.setCollectionBehavior_(
    NSWindowCollectionBehaviorCanJoinAllSpaces |
    NSWindowCollectionBehaviorStationary |
    NSWindowCollectionBehaviorFullScreenAuxiliary
    )

    # Add instructional text
    self.label = NSTextField.alloc().initWithFrame_(NSMakeRect(10, frame.size.height/2 - 50, frame.size.width - 20, 100))

    self.label.setStringValue_(
    "Shortcuts:\n⌘+⌥+S: Take Screenshot\n⌘+⌥+N: Toggle Overlay\n⌘+⌥+Q: Quit"
    )

    self.label.setFont_(NSFont.systemFontOfSize_(28))
    self.label.setTextColor_(NSColor.whiteColor())
    self.label.setBezeled_(False)
    self.label.setDrawsBackground_(False)
    self.label.setEditable_(False)
    self.label.setSelectable_(False)
    self.label.setAlignment_(1) # Center
    self.window.contentView().addSubview_(self.label)

    self.overlay_visible = True
    self.preview_image_view = None # Initialize preview field
    self.window.orderFrontRegardless()

    # Start local key listener
    NSEvent.addLocalMonitorForEventsMatchingMask_handler_(
    NSEventMaskKeyDown,
    self.handleKeyEvent_
    )

    # Start global key listener
    self.global_event_tap = Quartz.CGEventTapCreate(
    Quartz.kCGSessionEventTap,
    Quartz.kCGHeadInsertEventTap,
    Quartz.kCGEventTapOptionDefault,
    Quartz.CGEventMaskBit(Quartz.kCGEventKeyDown),
    globalEventCallback,
    None
    )

    self.run_loop_source = Quartz.CFMachPortCreateRunLoopSource(None, self.global_event_tap, 0)
    Quartz.CFRunLoopAddSource(
    Quartz.CFRunLoopGetCurrent(),
    self.run_loop_source,
    Quartz.kCFRunLoopCommonModes
    )
    Quartz.CGEventTapEnable(self.global_event_tap, True)

    def globalEventCallback(proxy, type_, event, refcon):
    # Look up the AppDelegate instance and call its handler
    app_delegate = NSApp().delegate()
    app_delegate.handleKeyEvent_(objc.lookUpClass('NSEvent').eventWithCGEvent_(event))
    return event

    def main():
    app = NSApplication.sharedApplication()
    delegate = AppDelegate.alloc().init()
    NSApp().setDelegate_(delegate)
    app.setActivationPolicy_(NSApplicationActivationPolicyAccessory)
    app.run()

    if __name__ == "__main__":
    main()

    Continue reading...

Compartilhe esta Página