Skip to content

Instantly share code, notes, and snippets.

@downthecrop
Created December 21, 2024 05:54
Show Gist options
  • Save downthecrop/f30e2ff6146d72884008b191e8e7148c to your computer and use it in GitHub Desktop.
Save downthecrop/f30e2ff6146d72884008b191e8e7148c to your computer and use it in GitHub Desktop.
Creates a transparent, draggable overlay window using pyglet and implements a global key listener with pynput to detect keypresses. The overlay is designed for macOS, featuring always-on-top behavior and adjustable transparency. Keypress events, like the Enter key, are logged with timestamps using the logging module. Accessibility permissions ar…
import sys
import pyglet
import pyglet.graphics
import pyglet.shapes
from pynput import keyboard as pynput_keyboard
import threading
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
class TransparentOverlay(pyglet.window.Window):
def __init__(self, width, height):
super().__init__(
width=width,
height=height,
style=pyglet.window.Window.WINDOW_STYLE_BORDERLESS,
caption='Transparent Overlay',
vsync=True,
resizable=True
)
self.set_minimum_size(100, 100)
self._set_window_transparent()
self._make_always_on_top()
screen_width = 1920
screen_height = 1080
self.set_location(screen_width // 2 - width // 2, screen_height // 2 - height // 2)
self.background = pyglet.shapes.Rectangle(0, 0, width, height, (51, 51, 51))
self.background.opacity = 128
self.label = pyglet.text.Label(
'Drag me around!',
font_name='Arial',
font_size=24,
x=width // 2,
y=height // 2,
anchor_x='center',
anchor_y='center',
color=(255, 255, 255, 255)
)
self.dragging = False
self.drag_offset_x = 0
self.drag_offset_y = 0
def _set_window_transparent(self):
if sys.platform == 'darwin':
view = self.context.canvas.nsview
view.setOpaque_(False)
window = view.window()
window.setBackgroundColor_(0)
window.setAlphaValue_(0.5)
def _make_always_on_top(self):
if sys.platform.startswith('win'):
from ctypes import windll
HWND_TOPMOST = -1
SWP_NOMOVE = 0x0002
SWP_NOSIZE = 0x0001
SWP_NOACTIVATE = 0x0010
SetWindowPos = windll.user32.SetWindowPos
SetWindowPos(
self._hwnd,
HWND_TOPMOST,
0, 0,
0, 0,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE
)
elif sys.platform == 'darwin':
from ctypes import cdll, c_int
appkit = cdll.LoadLibrary('/System/Library/Frameworks/AppKit.framework/AppKit')
appkit.CGWindowLevelForKey.restype = c_int
appkit.CGWindowLevelForKey.argtypes = [c_int]
NSFloatingWindowLevelKey = 5
level = appkit.CGWindowLevelForKey(NSFloatingWindowLevelKey)
ns_window = self.context.canvas.nsview.window()
ns_window.setLevel_(level)
ns_window.setCollectionBehavior_(1 << 7)
else:
pass
def on_draw(self):
self.clear()
self.background.width = self.width
self.background.height = self.height
self.background.draw()
self.label.draw()
def on_mouse_press(self, x, y, button, modifiers):
if button == pyglet.window.mouse.LEFT:
self.dragging = True
self.drag_offset_x = x
self.drag_offset_y = y
def on_mouse_release(self, x, y, button, modifiers):
if button == pyglet.window.mouse.LEFT:
self.dragging = False
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
if self.dragging:
current_x, current_y = self.get_location()
new_x = current_x + dx
new_y = current_y - dy
self.set_location(int(new_x), int(new_y))
def listen_for_keys():
def on_press(key):
if key == pynput_keyboard.Key.enter: # Check if the key is Enter
logging.info("Enter was pressed (global)")
with pynput_keyboard.Listener(on_press=on_press) as listener:
listener.join()
def main():
listener_thread = threading.Thread(target=listen_for_keys, daemon=True)
listener_thread.start()
window = TransparentOverlay(400, 300)
pyglet.app.run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment