magmax / python-readchar

Python library to read characters and key strokes
MIT License
153 stars 46 forks source link

Fix Unicode Input Issue and OSError in Test Script #119

Closed sakkyoi closed 2 months ago

sakkyoi commented 2 months ago

I tested it on macOS without any issues. The original problem seems to occur only on Windows.

Cube707 commented 2 months ago

So you tested with relevant characters on MacOS? But not on Windows?

sakkyoi commented 2 months ago

I apologize for my incomplete explanation. I initially noticed this issue on Windows, so I fixed it. However, I also noticed that Windows handles this differently compared to Linux and macOS. I tested it on macOS, and it showed that macOS can natively handle Unicode character input perfectly.

Cube707 commented 2 months ago

Took me a while to get the Japanese keyboard to work on windows, but here is what I found:

This seems to mostly work as expected. Linux is fine as is, as you have found with Mac: grafik

windows work for all characters I tested, but breaks on emojis for some reason: grafik

sakkyoi commented 2 months ago

After my investigation, it appears that some emojis are encoded using UTF-8 surrogates, which may appear as two characters in the terminal (you can observe this if you select them with your cursor). This is related to the issue of "surrogates not allowed." Perhaps you could use a try-except block to handle UnicodeEncodeError, as this error is raised by getwchar().

sakkyoi commented 2 months ago

I might have found a solution, so please stay tuned.

Cube707 commented 2 months ago

After my investigation, it appears that some emojis are encoded using UTF-8 surrogates, which may appear as two characters in the terminal 

Still strang that it works fine on linux

sakkyoi commented 2 months ago

Linux works fine without this issue.

Cube707 commented 2 months ago

this seems to be a good solution, but I would prefer that code in the readkey() function, as it handles identically to the combined key-presses. Surrogates are also made up of two chars that don't make sense on their own.

I will implement that now

coveralls commented 2 months ago

Coverage Status

coverage: 94.904% (+0.07%) from 94.839% when pulling 050dd5bc0b7b3bb20cb67966e89cf86648619c8a on sakkyoi:master into a3e9b0b4030b93c862ee7ac99df744804bd10093 on magmax:master.