pynput icon indicating copy to clipboard operation
pynput copied to clipboard

Not recognizing `¨` character in macOS 10.14.4

Open xiebruce opened this issue 5 years ago • 2 comments

I would like to set Global HotKey option + shift + u, but if I press u after keep pressing option and shift down, character u will transform into this ¨, and now the question is this ¨ character get from on_press() function is not the same as the one I wrote in the code, the one get from on_press() function is ¨'̈, not the same as ¨ that I wrote in the code, I've tried put ¨'̈ in the code, but it will throw an error, so this is the question, I don't know if I make myself understood.

macOS: 10.14.4 (18E226) python3: Python 3.7.7 pynput: 1.6.8

Here is my code snippet:

#!/usr/local/bin/python3
# -*- coding:utf-8  -*-

from pynput import keyboard


def get_key_combination():
    """ Get key combinations from config """
    tmp_list = []
    key_combinations = [
        ['alt', 'shift', '¨'],
    ]

    for items in key_combinations:
        s = set()
        for item in items:
            if len(item) == 1:
                ele = keyboard.KeyCode(char=item)
            else:
                ele = getattr(keyboard.Key, item)
            s.add(ele)
        tmp_list.append(s)
    return tmp_list


# The currently active modifiers
current = set()
# get key combination, COMBINATIONS value is like this: [{<Key.alt: <58>>, <Key.shift: <56>>, '¨'}]
COMBINATIONS = get_key_combination()


def on_press(key):
    """ Listen button press event  """
    print(key, 'is pressed.')
    if any([key in COMBO for COMBO in COMBINATIONS]):
        current.add(key)
        if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
            print('HotKey is pressed.')


def on_release(key):
    """ Listen button release event """
    print(key, 'is released.')
    if any([key in COMBO for COMBO in COMBINATIONS]):
        current.remove(key)


with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    """ start a keyboard listener """
    listener.join()

save the code to a file(say pynput-test.py), then run it:

python3 pynput-test.py

xiebruce avatar Mar 30 '20 16:03 xiebruce

Thank you for your report.

This is caused by a missing implementation of pynput.keyboard.Listener.canonical for macOS. This method is used to transform key events to their canonical representation, that is, whatever key event option + shift + u would produce into a representation for U.

My last mac is unfortunately no longer operational, so adding functionality for macOS is rather difficult---I have to rely on documentation and volunteers for testing.

In this case, I suspect the canonical value would be the CGKeyboardEventKeycode field of CGEvent. This is used as virtual key code in pynput. The difficulty lies in going from a character to its corresponding event code. A starting point would probably be pynput._util.darwin.get_unicode_to_keycode_map.

moses-palmer avatar Apr 09 '20 21:04 moses-palmer

Actually I am new in python, but I suggest you install a VMware(or virtualbox) and run macOS in the vm so you can test, but this requires a high-level configuration PC(laptop) or it will be slow.

Another way is , if you have another PC, you can install a Hacintosh to test this.

xiebruce avatar Apr 10 '20 04:04 xiebruce