2016年4月11日月曜日

Logicool M560をMacOS X + Karabinerで使う

Mac Mini(MacOS X 10.10/Yosemite)でLogicool M560
http://www.logicool.co.jp/ja-jp/product/wireless-mouse-m560
を使おうとした時のことです。ボタンのカスタマイズソフトウェアであるLogicool Control Center for Macintosh OS X
http://support.logicool.co.jp/ja_jp/software/logitech-control-center-for-macintosh-os-x
でボタンをカスタマイズしようとしたのですが、このマウスには対応してない言われ、カスタマイズすることができません。M560の中央のスクロールホイールボタンの左右、もしくは右親指あたりにある「進む」・「戻る」ボタンで、WWWブラウザの履歴操作(進む・戻る)をしたかったのです。

調べてみると、スクロールホイールボタンの左右については、これらはマウスの第4、第5ボタンになっているらしく、これをなんとか設定すればよいようです。
Mac OS XでMouse Button4/5をブラウザの戻る/進むにマップする
http://memo.digitune.org/2014/1129/
私は、すでにKarabina
https://pqrs.org/osx/karabiner/index.html.ja
は使っているので設定をするだけです(私は英語キーボードを使っていますが、日本語キーボードだと設定が違うかもしれません)。

KarabinerのPreference→Change Keyにある「Pointing Device→Button to Key→Use Button4 and Button5 as Back/Foward」の「Set 1」を選びます。
試してみると動きます。
ついでにもう少し調べてみると、
logitech_m560.xml 
https://github.com/tekezo/Karabiner/blob/master/src/core/server/Resources/include/checkbox/devices/logitech_m560.xml
というKarabinerのM560用の設定が見つかります。Preferenceから見てみると、
For Logitech Devicesの下の方に「Logitech M560 mouse Support for extra buttons」というがあります。
さっそく有効にしてみましたが、うまく動きません。

Preferenceの Misc&Uninstallタブにある「Launch EventViewer」を使ってみてみると、ボタンのイベントがブラウザの履歴の進む・戻るのショートカットキーに変換されていません。

KarabinerのM560の設定を見てみました。
  1. アプリケーションフォルダのKarabierを右クリックし、「パッケージの内容」を選ぶ
  2. /Applications/Karabiner.app/Contents/Resources/include/checkbox/devices/logitech_m560.xmlを参照する
すると
<device_only>DeviceVendor::LOGITECH, DeviceProduct::LOGITECH_USB_RECEIVER</device_only>
      <autogen>
        __KeyToKey__
        KeyCode::COMMAND_R,
        KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_L
      </autogen>
とあるように、確かにCmd-RキーをCmd-L+→に変換しています。

よく見ると、
<device_only>DeviceVendor::LOGITECH, DeviceProduct::LOGITECH_USB_RECEIVER</device_only>
で、キーが発生したデバイスを限定していますが、これがうまくないのかのしれません。ファイル
/Applications/Karabiner.app/Contents/Resources/deviceproductdef.xml
で定義されているDeviceProductを見てみます。
  <deviceproductdef>
    <productname>LOGITECH_USB_RECEIVER</productname>
    <productid>0xc52b</productid>
  </deviceproductdef>

0xc52bというデバイス番号のようです。 

先ほど使った「Launch EventViewer」のDevicesタブで見てみると、このUSB ReceiverのIDの他にUnifing DeviceというIDもあります。
そこで、Karabinerのカスタマイズ用xmlファイルである
/Users/<ユーザー名>/Application Support/Karabiner/private.xml
に先ほどの以下のようなlogitech_m560.xmlを少し改変したものを入れていみると、うまく動くようになりました。
<?xml version="1.0"?>
<root>
  <deviceproductdef>
    <productname>M560x</productname>
    <productid>0x402D</productid>
  </deviceproductdef>

  <item>
    <name>Logitech M560x mouse</name>
    <appendix>Support for extra buttons</appendix>
    <item>
      <name>Forward Button to Browser Go Forward</name>
      <appendix>Button on the left side of M560x</appendix>
      <identifier>device.logitech.m560x.browser.goforward</identifier>
      <device_only>DeviceVendor::LOGITECH, DeviceProduct::M560x</device_only>
      <autogen>
        __KeyToKey__
        KeyCode::COMMAND_R,
        KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_L
      </autogen>
    </item>

    <item>
      <name>Back Button to Browser Go Back</name>
      <appendix>Button on the left side of M560x</appendix>
      <identifier>device.logitech.m560x.browser.goback</identifier>
      <device_only>DeviceVendor::LOGITECH, DeviceProduct::M560x</device_only>
      <autogen>
        __KeyToKey__
        KeyCode::D, ModifierFlag::COMMAND_L,
        KeyCode::CURSOR_LEFT, ModifierFlag::COMMAND_L
      </autogen>
    </item>

    <item>
      <name>Home button (under the wheel) to Mission Control</name>
      <appendix>Button in the middle of M560x under the wheel</appendix>
      <identifier>device.logitech.m560x.osx.missioncontrol</identifier>
      <device_only>DeviceVendor::LOGITECH, DeviceProduct::M560x</device_only>
      <autogen>
        __KeyToKey__
        KeyCode::OPTION_L, KeyCode::COMMAND_L,
        KeyCode::CURSOR_UP, ModifierFlag::CONTROL_L
      </autogen>
    </item>
  </item>
</root>