kcsaff / getkey

MIT License
41 stars 18 forks source link

Does not capture Unicode chars under Windows #11

Open malversan opened 4 years ago

malversan commented 4 years ago

Currently in Windows the capture is made using msvcrt.getch(), which only captures one-byte at a time. This dramatically reduces the amount of characters that can be captured (along with producing bytes instead of chars, see issue #10).

Using msvcrt.getwch() instead produces multibyte captures and also returns chars instead of bytes.

So the code in PlatformWindows.getchars():

    def getchars(self, blocking=True):
        """Get characters on Windows."""
        if blocking:
            yield self.msvcrt.getch()
        while self.msvcrt.kbhit():
            yield self.msvcrt.getch()

should be substituted with this:

    def getchars(self, blocking=True):
        """Get characters on Windows."""
        def getchsequence():
            c = self.msvcrt.getwch()
            # Iteration is needed to capture full escape sequences with msvcrt.getwch()
            while c and c in self.keys.escapes:
                c += self.msvcrt.getwch()
            return c
        if blocking:
            yield getchsequence()
        while self.msvcrt.kbhit():
            yield getchsequence()

Tested in: Windows 8.1, Python 3.6.1