Open jeremynz opened 10 years ago
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. keyboard 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() in {AppKit.NSEventTypeLeftMouseUp, AppKit.NSEventTypeLeftMouseDown}:
click = EventType.left
elif raw_event.type() in {AppKit.NSEventTypeRightMouseUp, AppKit.NSEventTypeRightMouseDown}:
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.
Can you elaborate a bit how to use and where this event should be placed? and also what are the libarries to install? it seems that is not something rumps has out of the box but rather something to add to it , right?
You should just be able to copy the Event
and EventType
classes into your script, and add any imports you don't already have. I don't think any additional dependencies are needed.
The on_click
method I show is just the normal Rumps MenuItem
clicked callback.
Let me know if you have trouble with this.
You should just be able to copy the
Event
andEventType
classes into your script, and add any imports you don't already have. I don't think any additional dependencies are needed.The
on_click
method I show is just the normal RumpsMenuItem
clicked callback.Let me know if you have trouble with this.
I did take piece of code and modified one of my funcion, the only problem that i faced was that i'd to change if event.type() == AppKit.NSEventTypeLeftMouseUp:
to something like if event.type() == 1
:
not sure why the value was not correct but in that fashion it works
You should be able to leave all the AppKit
code in the Event
class. You can see an example of my using a somewhat updated version of it here: https://github.com/brunns/github-actions-status-mac-menu-bar-spike/blob/main/status.py#L411
Is it possible to differentiate between Left and Right clicks?