When I used getch () of curses, I couldn't receive Japanese (multibyte characters) well when encoding with UTF-8, so I made a processing part.
For example, if you type'a', it will be divided into 3 bytes of 0xe3 0x81 0x82 and will be input 3 times. However, this is inconvenient. (What I really want is 0x3042)
Since getch () looks at the input byte by byte, UTF-8 cannot correctly receive Japanese etc. which is 3 bytes. So, I will make my own UTF-8 encoder.
Looking at the first byte, the number of bytes in the character string is fixed, so conditional branching is performed as follows.
import curses
window = curses.stdscr()
key = window.getch()
#Processing of multi-byte characters
#In Japanese, it's 3 bytes, so you need to pool it. Look at the first byte
#Since the remaining vices and the number are fixed, the process is performed.
text_pool = [key]
if 0x00 <= key <= 0x7f:
#1B so you don't have to do anything
#ascii compatible area
pass
elif 0x80 <= key <= 0xbf:
#This should be after the second character, so it would be strange if it came in
print(key)
exit(1)
elif 0xc0 <= key <= 0xdf:
#2B characters with umlauts
text_pool.append(self.window.getch())
# text_pool => [0dAAA, 0dBBB]
# 110a aabb 10bb bbbb <=This is text_Contents of pool(Decimal version)
#0b00000aaa bbbbbbbb is taken out and char c= (char) (data[i] & 0xff);
#Convert to decimal number and assign to key
a, b = text_pool
tmp = map(lambda x: bin(x)[2:], [0b00011111 & a, 0b00111111 & b])
tmp = ''.join(item.zfill(6) for item in tmp)
key = int(tmp,2)
elif 0xe0 <= key <= 0xef:
#3B Japanese is here
for _ in range(2):
text_pool.append(self.window.getch())
a, b, c = text_pool
# 0b 1110xxxx 10xxyyyy 10yyyyyy
# 0d a b c
tmp = map(lambda x: bin(x)[2:], [0b00001111 & a, 0b00111111 & b, 0b00111111 & c])
tmp = ''.join([item.zfill(6) for item in tmp])
key = int(tmp,2)
elif 0xf0 <= key <= 0xff:
#4B I've never seen it, but bug fixing
for _ in range(3):
text_pool.append(self.window.getch())
a, b, c ,d = text_pool
# 11110xxx 10xxyyyy 10yyyyzz 10zzzzzz
tmp = map(lambda x: bin(x)[2:], [0b00000111 & a, 0b00111111 & b, 0b00111111 & c, 0b00111111 & d])
tmp = ''.join([item.zfill(6) for item in tmp])
key = int(tmp,2)
else:
#Special key
pass
print(chr(key))
About mouse wheel judgment https://qiita.com/t4t5u0/items/ae6d25e05b7f7094330e
https://seiai.ed.jp/sys/text/cs/mcodes/ucodeutf8.html
Recommended Posts