Skip to content

Instantly share code, notes, and snippets.

@raku-cat
Last active July 22, 2025 22:54
Show Gist options
  • Save raku-cat/8015592fc2dca0c318943ecf7d988608 to your computer and use it in GitHub Desktop.
Save raku-cat/8015592fc2dca0c318943ecf7d988608 to your computer and use it in GitHub Desktop.
Made quick and dirty using chatgpt
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk, GLib, Gdk
import time
class MouseTimer(Gtk.Application):
def __init__(self):
super().__init__()
self.press_time = None
def do_activate(self):
window = Gtk.ApplicationWindow(application=self)
window.set_title("Mouse Hold Timer")
window.set_default_size(300, 100)
label = Gtk.Label(label="Click and hold the mouse")
window.set_child(label)
self.label = label
# GestureClick to capture press/release
gesture = Gtk.GestureClick()
gesture.set_button(Gdk.BUTTON_PRIMARY)
gesture.connect("pressed", self.on_pressed)
gesture.connect("released", self.on_released)
# Attach gesture to the window or label
window.add_controller(gesture)
window.present()
def on_pressed(self, gesture, n_press, x, y):
self.press_time = time.perf_counter()
def on_released(self, gesture, n_press, x, y):
if self.press_time:
elapsed = time.perf_counter() - self.press_time
self.label.set_label(f"Mouse held for {elapsed:.3f} seconds")
print(f"Mouse held for {elapsed:.3f} seconds")
self.press_time = None
app = MouseTimer()
app.run()
import sys
import time
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt5.QtCore import Qt
class MouseTimer(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Mouse Hold Timer")
self.setGeometry(100, 100, 300, 100)
self.label = QLabel("Click and hold the mouse", self)
self.label.setAlignment(Qt.AlignCenter)
layout = QVBoxLayout()
layout.addWidget(self.label)
self.setLayout(layout)
self.press_time = None
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.press_time = time.perf_counter()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self.press_time:
elapsed = time.perf_counter() - self.press_time
self.label.setText(f"Mouse held for {elapsed:.3f} seconds")
print(f"Mouse held for {elapsed:.3f} seconds")
self.press_time = None
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MouseTimer()
window.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment