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

Fix the problem of capslock on macOS #381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yrjyrj123
Copy link

Hi,

I am a macOS user.
I am using this module to do some interesting things, I think this module is very useful.
But, I found a problem.

If you check the option in the red box in macOS keyboard settings, the following error will appear when the capslock is pressed.
image

Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/Users/hero/venv/dazahui/lib/python3.7/site-packages/keyboard/init.py", line 294, in listen
_os_keyboard.listen(self.direct_callback)
File "/Users/hero/venv/dazahui/lib/python3.7/site-packages/keyboard/_darwinkeyboard.py", line 441, in listen
KeyEventListener(callback).run()
File "/Users/hero/venv/dazahui/lib/python3.7/site-packages/keyboard/_darwinkeyboard.py", line 375, in run
Quartz.CFRunLoopRunInMode(Quartz.kCFRunLoopDefaultMode, 5, False)
File "/Users/hero/venv/dazahui/lib/python3.7/site-packages/keyboard/_darwinkeyboard.py", line 394, in handler
if key_name.endswith("shift") and (flags & Quartz.kCGEventFlagMaskShift):
AttributeError: 'NoneType' object has no attribute 'endswith'

After debugging, I found the reason.
This line of code returned an invalid value 0xFF, I don't know why this happened, but it did happen.
scan_code = Quartz.CGEventGetIntegerValueField(event, Quartz.kCGKeyboardEventKeycode)
https://github.com/boppreh/keyboard/blob/master/keyboard/_darwinkeyboard.py#L375
This API is provided by Apple, but I can't find document about the return value 0xFF, maybe it stands for invalid.

I tried to deal with this invalid value, and now it works well on my macbook.

By the way, I am using macOS 10.15.5.

If you think this is valuable, please merge it.

Thx.

@HaujetZhao
Copy link

HaujetZhao commented Jun 1, 2023

One of my friends met this problem too!!!

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

Successfully merging this pull request may close these issues.

2 participants