iT邦幫忙

0

MacOS讀取藍牙搖桿訊號,利用python修改pynput程式碼實現 - 3.修改pynput

  • 分享至 

  • xImage
  •  

請參考之前的文章:第二篇

5. 修改pynput

if event_type == Quartz.NSSystemDefined:
    sys_event = Quartz.NSEvent.eventWithCGEvent_(event)
    if sys_event.subtype() == kSystemDefinedEventMediaKeysSubtype:
        # The key in the special key dict; True since it is a media
        # key
        key = ((sys_event.data1() & 0xffff0000) >> 16, True)
        if key in self._SPECIAL_KEYS:
            flags = sys_event.data1() & 0x0000ffff
            is_press = ((flags & 0xff00) >> 8) == 0x0a
            if is_press:
                self.on_press(self._SPECIAL_KEYS[key])
            else:
                self.on_release(self._SPECIAL_KEYS[key])

我們可以發現pyput對data1做以下處理:

key = ((sys_event.data1() & 0xffff0000) >> 16, True)

可以參考 菜鳥教程
& 0xffff0000的作用是將二進制的最後16個數字變為0,>> 16的作用是將二進制的最後16個數字刪除
結果如下:

原始數據 轉二進制                  & 0xffff0000處理        >>16處理 轉回十進制
1051392 0b100000000101100000000 0b100000000000000000000 0b10000 16
1051136 0b100000000101000000000 0b100000000000000000000 0b10000 16

1182208 0b100100000101000000000 0b100100000000000000000 0b10010 18
1182464 0b100100000101100000000 0b100100000000000000000 0b10010 18

1116672 0b100010000101000000000 0b100010000000000000000 0b10001 17
1116928 0b100010000101100000000 0b100010000000000000000 0b10001 17

1313280 0b101000000101000000000 0b101000000000000000000 0b10100 20
1313536 0b101000000101100000000 0b101000000000000000000 0b10100 20

1247744 0b100110000101000000000 0b100110000000000000000 0b10011 19
1248000 0b100110000101100000000 0b100110000000000000000 0b10011 19

接下來

flags = sys_event.data1() & 0x0000ffff
is_press = ((flags & 0xff00) >> 8) == 0x0a

經過處理後:

原始數據 轉二進制                 &0x0000ffff處理 >>8處理 轉回十進制
1051392 0b100000000101100000000 0b101100000000 0b1011 11
1051136 0b100000000101000000000 0b101000000000 0b1010 10
1182208 0b100100000101000000000 0b101000000000 0b1010 10
1182464 0b100100000101100000000 0b101100000000 0b1011 11
1116672 0b100010000101000000000 0b101000000000 0b1010 10
1116928 0b100010000101100000000 0b101100000000 0b1011 11
1313280 0b101000000101000000000 0b101000000000 0b1010 10
1313536 0b101000000101100000000 0b101100000000 0b1011 11
1247744 0b100110000101000000000 0b101000000000 0b1010 10
1248000 0b100110000101100000000 0b101100000000 0b1011 11

如果轉換後為10,表示== 0x0ais_pressTrue

根據pynput的代碼,會去調用self._SPECIAL_KEYS,我曾經嘗試去Key那邊增加20和19,但是這會引發一系列錯誤,所以我直接在原本的程式碼前面多加幾行if,結果如下:

elif event_type == Quartz.NSSystemDefined:
    sys_event = Quartz.NSEvent.eventWithCGEvent_(event)
    if sys_event.subtype() == kSystemDefinedEventMediaKeysSubtype:
        # The key in the special key dict; True since it is a media
        # key
        key = ((sys_event.data1() & 0xffff0000) >> 16, True)
#--------------------------以下是我加入的程式碼--------------------------
        if key == (19, True): #right
            if ((sys_event.data1() & 0x0000ffff) >> 8) == 0x0a:
                self.on_press("Key.media_right")
        elif key == (20, True): #left
            if ((sys_event.data1() & 0x0000ffff) >> 8) == 0x0a:
                self.on_press("Key.media_left")
#--------------------------以上是我加入的程式碼--------------------------
        elif key in self._SPECIAL_KEYS:
            flags = sys_event.data1() & 0x0000ffff
            is_press = ((flags & 0xff00) >> 8) == 0x0a
            if is_press:
                self.on_press(self._SPECIAL_KEYS[key])
            else:
                self.on_release(self._SPECIAL_KEYS[key])

如果處理過後的數字為19或20,則分別回傳"Key.media_right"和"Key.media_left"

最後我們在寫操控程式的時候要注意到,回傳的為文字,而不是原來的物件了

from pynput.keyboard import Key, Listener, Controller
keyboard = Controller()

def on_press(key):
    if key == Key.media_previous: #上
        keyboard.press(Key.up)
    elif key == Key.media_next:  #下
        keyboard.press(Key.down)
    elif key == "Key.media_right": #右
        keyboard.press(Key.right)
    elif key == "Key.media_left": #左
        keyboard.press(Key.left)
    listener.stop()

if __name__ == '__main__':
    while True: 
        with Listener(on_press = on_press_j) as listener:
            listener.join()

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言