環境
- Mint 19.10
- Python 3.7.5
はじめに
curses にマウスのホイール判定が用意されていなかったので自作した。
これを読む前に前提となっているこの記事を読んでほしい。
症状
ホイール判定をとれない
原因
curses には、マウスのクリック判定などが用意されているが不十分で、ホイール判定。特に下にスクロールするときの判定が用意されていない。
解決策
判定部分のコメントは日本語入力の記事を参照。擬似コードです。windowオブジェクトをきちんと宣言していません
import curses
window = curses.stdscr()
key = window.getch()
text_pool = [key]
if 0x00 <= key <= 0x7f:
     pass
elif 0x80 <= key <= 0xbf:
     print(key)
     exit(1)
elif 0xc0 <= key <= 0xdf:
     text_pool.append(self.window.getch())
     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:
     for _ in range(2):
         text_pool.append(self.window.getch())
         a, b, c = text_pool
         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:
     for _ in range(3):
         text_pool.append(self.window.getch())
         a, b, c ,d = text_pool
         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:
    pass
# 実際の値
WHEEL_UP = 65536
WHEEL_DOWN = 2097152
if key == curses.KEY_MOUSE:
    # ここにマウスによって入力された整数が格納されている
    wheel = curses.getmouse()[4]
    if wheel == self.WHEEL_UP:
        wheel_up_process()
    elif wheel == self.WHEEL_DOWN:
        wheel_down_process()
else:
    print(chr(key)