rumps icon indicating copy to clipboard operation
rumps copied to clipboard

Request: Right-Click support

Open jeremynz opened this issue 11 years ago • 8 comments

Is it possible to differentiate between Left and Right clicks?

jeremynz avatar Aug 16 '14 01:08 jeremynz

adding to to your question, is there "option/alt" key available to view additional options?

ghost avatar Dec 21 '15 11:12 ghost

Any idea when this is coming?

ghost avatar Aug 25 '18 04:08 ghost

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}')

brunns avatar May 24 '23 14:05 brunns

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.

brunns avatar May 25 '23 10:05 brunns