rumps
rumps copied to clipboard
Request: Right-Click support
Is it possible to differentiate between Left and Right clicks?
adding to to your question, is there "option/alt" key available to view additional options?
Any idea when this is coming?
Re right vs. left click, it's possible already if your callback looks like this:
def on_click(self, sender):
event = AppKit.NSApplication.sharedApplication().currentEvent()
if event.type() == AppKit.NSEventTypeLeftMouseUp:
print(f'Left click on {sender.title}')
elif event.type() == AppKit.NSEventTypeRightMouseUp:
print(f'Right click on {sender.title}')
This utility function works for me, for both right vs. left clicks vs. keyword shortcuts, and for checking for shift/ctrl/option/command:
import AppKit
from dataclasses import dataclass
from enum import Enum, auto
class EventType(Enum):
left = auto()
right = auto()
key = auto()
@dataclass
class Event:
type: EventType
shift: bool
control: bool
option: bool
command: bool
@classmethod
def get_event(cls) -> "Event":
raw_event = AppKit.NSApplication.sharedApplication().currentEvent()
if raw_event.type() == AppKit.NSEventTypeLeftMouseUp:
click = EventType.left
elif raw_event.type() == AppKit.NSEventTypeRightMouseUp:
click = EventType.right
elif raw_event.type() == AppKit.NSEventTypeKeyDown:
click = EventType.key
else:
logger.warning("unknown event type", extra={"event": raw_event})
click = None
shift = bool(raw_event.modifierFlags() & AppKit.NSEventModifierFlagShift)
control = bool(raw_event.modifierFlags() & AppKit.NSEventModifierFlagControl)
option = bool(raw_event.modifierFlags() & AppKit.NSEventModifierFlagOption)
command = bool(raw_event.modifierFlags() & AppKit.NSEventModifierFlagCommand)
return cls(click, shift, control, option, command)
A callback can use it as follows:
def on_click(self, sender):
event = Event.get_event()
if event.type == EventType.left:
print(f"Left click on {sender.title}")
elif event.type == EventType.right:
print(f"Right click on {sender.title}")
if event.shift:
print("Shift held")
I don't know if it would be worth putting something like this into Rumps? I'm happy to submit a PR if so.