Hammerspoon で右 ⌘ を識別する

:point_down_tone2: のようなマッピングを実現したかったのですが、

| 入力 | 結果 |
| --- | --- |
| 右 <kbd>⌘</kbd> + <kbd>A</kbd> | <kbd>1</kbd> |
| 右 <kbd>⌘</kbd> + <kbd>S</kbd> | <kbd>2</kbd> |
| 右 <kbd>⌘</kbd> + <kbd>D</kbd> | <kbd>3</kbd> |
| 右 <kbd>⌘</kbd> + <kbd>F</kbd> | <kbd>4</kbd> |
| 右 <kbd>⌘</kbd> + <kbd>G</kbd> | <kbd>5</kbd> |
| 右 <kbd>⌘</kbd> + <kbd>H</kbd> | <kbd>6</kbd> |
| 右 <kbd>⌘</kbd> + <kbd>J</kbd> | <kbd>7</kbd> |
| 右 <kbd>⌘</kbd> + <kbd>K</kbd> | <kbd>8</kbd> |
| 右 <kbd>⌘</kbd> + <kbd>L</kbd> | <kbd>9</kbd> |
| 右 <kbd>⌘</kbd> + <kbd>;</kbd> | <kbd>0</kbd> |
| 右 <kbd>⌘</kbd> + <kbd>:</kbd> | <kbd>-</kbd> |

[A question related to nested event · Issue #1128 · Hammerspoon/hammerspoon](https://github.com/Hammerspoon/hammerspoon/issues/1128) (追記: と、頂いた hetima さんからのコメント) を参考に :point_down_tone2: のようにやったらできました。

```lua
rightCommandKeyCode = 54
rightCommandFlag    = 16
releasedFlag        = 256

local module = {}

local rightCommandHandler = function(e)
  local watchFor    = { ["a"] = "1", ["s"] = "2", ["d"] = "3", ["f"] = "4", ["g"] = "5", ["h"] = "6", ["j"] = "7", ["k"] = "8", ["l"] = "9", [";"] = "0", [":"] = "-" }
  local actualKey   = e:getCharacters(true)
  local replacement = watchFor[actualKey:lower()]

  if replacement then
    local isDown           = e:getType() == hs.eventtap.event.types.keyDown
    local replacementEvent = hs.eventtap.event.newKeyEvent({}, replacement, isDown)

    if isDown then
      replacementEvent:setProperty(hs.eventtap.event.properties.keyboardEventAutorepeat, e:getProperty(hs.eventtap.event.properties.keyboardEventAutorepeat))
    end

    return true, { replacementEvent }
  end
end

module.modifierListener = hs.eventtap.new({hs.eventtap.event.types.flagsChanged}, function(event)
  if event:getKeyCode() == rightCommandKeyCode then
    local eventData = event:getRawEventData().NSEventData
    if (eventData.modifierFlags & rightCommandFlag) == rightCommandFlag then
      module.keyListener = hs.eventtap.new({hs.eventtap.event.types.keyDown, hs.eventtap.event.types.keyUp}, rightCommandHandler):start()
    elseif module.keyListener and (eventData.modifierFlags & releasedFlag) == releasedFlag then
      module.keyListener:stop()
      module.keyListener = nil
    end
  end
end):start()
```

---

:pencil: 1 ~ 2 つの Modifier キーを同時押しした時の NSEventData.modifierFlags の値を地道に調べてみた

**10進数バージョン**

|            | 左 control | 左 shift | 左 option | 左 command | 右 command | 右 fn   | 右 shift | (なし)  |
| ---        | ---        | ---      | ---       | ---        | ---        | ---     | ---      | ---     |
| 左 control | -          | 393475   | 786721    | 1310985    | 1310993    | 8650753 | 393477   | 262401  |
| 左 shift   | -          | -        | 655650    | 1179914    | 1179922    | 8519682 | 131334   | 131330  |
| 左 option  | -          | -        | -         | 1573160    | 1573168    | 8912928 | 655652   | 524576  |
| 左 command | -          | -        | -         | -          | 1048856    | 9437192 | 1179916  | 1048840 |
| 右 command | -          | -        | -         | -          | -          | 9437200 | 1179924  | 1048848 |
| 右 fn      | -          | -        | -         | -          | -          | -       | 131332   | 8388608 |
| 右 shift   | -          | -        | -         | -          | -          | -       | -        | 131332  |

**2進数バージョン**

|            | 左 control | 左 shift                 | 左 option                | 左 command               | 右 command               | 右 fn                    | 右 shift                 | (なし)                   |
| ---        | ---        | ---                      | ---                      | ---                      | ---                      | ---                      | ---                      | ---                      |
| 左 control | -          | 000001100000000100000011 | 000011000000000100100001 | 000101000000000100001001 | 000101000000000100010001 | 100001000000000000000001 | 000001100000000100000101 | 000001000000000100000001 |
| 左 shift   | -          | -                        | 000010100000000100100010 | 000100100000000100001010 | 000100100000000100010010 | 100000100000000000000010 | 000000100000000100000110 | 000000100000000100000010 |
| 左 option  | -          | -                        | -                        | 000110000000000100101000 | 000110000000000100110000 | 100010000000000000100000 | 000010100000000100100100 | 000010000000000100100000 |
| 左 command | -          | -                        | -                        | -                        | 000100000000000100011000 | 100100000000000000001000 | 000100100000000100001100 | 000100000000000100001000 |
| 右 command | -          | -                        | -                        | -                        | -                        | 100100000000000000010000 | 000100100000000100010100 | 000100000000000100010000 |
| 右 fn      | -          | -                        | -                        | -                        | -                        | -                        | 000000100000000100000100 | 100000000000000000000000 |
| 右 shift   | -          | -                        | -                        | -                        | -                        | -                        | -                        | 000000100000000100000100 |