LoginSignup
5
3

More than 5 years have passed since last update.

Hammerspoon で右 ⌘ を識別する

Last updated at Posted at 2016-12-24

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

入力 結果
+ A 1
+ S 2
+ D 3
+ F 4
+ G 5
+ H 6
+ J 7
+ K 8
+ L 9
+ ; 0
+ : -

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

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
5
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3