magmax / python-readchar

Python library to read characters and key strokes
MIT License
143 stars 43 forks source link

Pls add support shift+tab #80

Closed fleytman closed 2 years ago

fleytman commented 2 years ago

I want use shift tab in inquirer. But readchar not read this keyevent

    print("Press a key")
    key = readchar.readkey()
    print("Key is {0}".format(key.encode()))
>>>Press a key
>>>shift+tab
>>>Key is b'\t'
Cube707 commented 2 years ago

You didn't provide this crutial information, but from the fact that you only get a single TAB back, I assume you are on windows?

Windows doesn't modify the tab-key when pressing shift, it always just reads TAB. So it is imposible to use TAB+SHIFT on windows, this has nothing to do with readchar() but is simply a windows limitation.

We could add it to the Linux side, but I would still not recomend using it as it is not portable...

fleytman commented 2 years ago

I'm use macos. It’s a pity that it won’t work for Windows, of course I want a cross-platform implementation. But support for *nix would also be nice. If you think about support in windows, is it possible to have a solution for a power shell or a windows terminal? As a last resort, there is wsl or cygwin.

Cube707 commented 2 years ago

For *nix support

This will have to wait but will probably come at some point. For now the project maintainer is not responding and untill he does I am just a powerless contributer myself. You can however, for the ime beeing, patch the key-module for unix systems with this line after your readchar import:

from readchar import readkey, key
key.SHIFT_TAB = "\x1b\x5b\x5a"

Now you can use it as expected:

while True:
    k = readkey()
    if k == key.SHIFT_TAB:
        print("got macro shortcut!!!!!")

You can get the codes for other unsported keys by running my testing script, it will print you the codes for all pressed keys.

For windows support:

readchar relies on the standart libary msvcrt to do the actuall terminal interaction. If you take this exapmple:

from msvcrt import getch

while True:
    k = getch()
    print(k)

you will see that both pressing TAB and SHIFT+TAB result in the same output of b'\t'. Windows doesn't provide this keycombination to python so it is imposible to handle it.

fleytman commented 2 years ago

Thanks for your answer. I found why I can't use readchar.readkey(), it's https://github.com/magmax/python-readchar/issues/11 problem. Pycharm terminal != system terminal. In system macos term i can read shift+tab.

fleytman commented 2 years ago

It's work with inquirer https://github.com/magmax/python-inquirer/pull/170