Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release reported as press for modifier keys with left/right variants in macOS #626

Open
slezica opened this issue Jan 26, 2025 · 0 comments

Comments

@slezica
Copy link

slezica commented Jan 26, 2025

Description
Holding both the left and right keys for a modifier at the same time confuses pynput as to whether subsequent events for these keys are presses or relases. Releases are reported as presses. See below for more details.

Platform and pynput version
MacOS, pynput 1.7.7

To Reproduce

  • Press down shift_l -- will be reported as press
  • Press down shift_r -- will be reported as press
  • Release shift_r -- will ALSO be reported as press

Probable Cause

This seems to stem from the way press/release events in macOS are distinguished. In _darwin:316:

# This is a modifier event---excluding caps lock---for which we
# must check the current modifier state to determine whether
# the key was pressed or released
flags = CGEventGetFlags(event)
is_press = flags & self._MODIFIER_FLAGS.get(key, 0)

In _MODIFIER_FLAGS, however, keys are mixed because there are no separate flags for each key. In _darwin:248:

_MODIFIER_FLAGS = {
    Key.alt: kCGEventFlagMaskAlternate,
    Key.alt_l: kCGEventFlagMaskAlternate,
    Key.alt_r: kCGEventFlagMaskAlternate,
    Key.cmd: kCGEventFlagMaskCommand,
    Key.cmd_l: kCGEventFlagMaskCommand,
    Key.cmd_r: kCGEventFlagMaskCommand,
    Key.ctrl: kCGEventFlagMaskControl,
    Key.ctrl_l: kCGEventFlagMaskControl,
    Key.ctrl_r: kCGEventFlagMaskControl,
    Key.shift: kCGEventFlagMaskShift,
    Key.shift_l: kCGEventFlagMaskShift,
    Key.shift_r: kCGEventFlagMaskShift}

This lack of distinct flags in Quartz seems to cause the mixup.

Note, however, that the key from event is actually the correct one, different for shift_l and shift_r. The information is available in the scope, though I don't know if there's a reliability problem there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant