mac でcontrol+wheelを使えるようにする。

macに移行してしばらく経つが、Windowsでおなじみのcontrol+wheelによるZoom機能がmacだと使えずにちょいと不便なので、Hammerspoonで実装してみた。
macの場合、magicpadが秀逸なので、慣れてくるとcontrol+wheelに頼ることも少なくなるような気がする。だけど、やっぱり体が覚えていてまたにホイールをコロコロ。。

というわけでそれを実現するluaが以下。
下記コードにはKarabinerによるキーバインド変更も含まれているので不要な場合は
— select keybind for Karabiner
部分を削除すればOK。

ちなみに、自分のmacはcaps->cmdにバインド変更しているため、Windowsキーバインドで言う所のcontrolがcmdになっているので、cmd+wheelでZoomが効くように実装してみた。
ここら辺も自分の環境に合わせて適宜変更すればOK。
appNameが日本語や英語名になっている部分はOSの言語設定に依存するので注意。

local home = os.getenv(“HOME”)
local appName

— application switch event
local function handleGlobalEvent(name, event, app)
    if event == hs.application.watcher.activated then
        appName = name
        print(“switch application : “, appName)
       
        — select keybind for Karabiner
        if (appName == “VNC Viewer”) then
            hs.execute(home .. ‘/.hammerspoon/karabiner-switcher 1’)
        else
            hs.execute(home .. ‘/.hammerspoon/karabiner-switcher 0’)
        end
       
    end
end
watcher = hs.application.watcher.new(handleGlobalEvent)
watcher:start()

— mouse wheel event
z = hs.eventtap.new({hs.eventtap.event.types.scrollWheel}, function(e)

    local whichFlags = e:getFlags()
   
    local wheel_x = e:getProperty(hs.eventtap.event.properties.scrollWheelEventDeltaAxis2)
    local wheel_y = e:getProperty(hs.eventtap.event.properties.scrollWheelEventDeltaAxis1)
    local wheel_val
   
    if (appName == “Notes”) or (appName == “Keynote”) then
   
        if whichFlags[‘cmd’] then
            print(wheel_x, wheel_y)
            if (wheel_x ~= 0) or (wheel_y ~= 0) then
                wheel_val = wheel_x + wheel_y
                –print(wheel_val)
                if wheel_val ~= 0 then
                    print(wheel_val)
                    if wheel_val > 0 then
                        print(“cmd: up”)
                        hs.eventtap.keyStroke({‘shift’,’cmd’}, ‘.’, 200)
                    else
                        print(“cmd: down”)
                       hs.eventtap.keyStroke({‘shift’,’cmd’}, ‘,’, 200)                  
                    end
                end
            end
        end
    end

    –if (appName == “Safari”) or (appName == “プレビュー”) or (appName == “メール”) or (appName == “CotEditor”) or (appName == “テキストエディット”)then
    if (appName == “Safari”) or (appName == “Preview”) or (appName == “Mail”) or (appName == “CotEditor”) or (appName == “TextEdit”)then
        if whichFlags[‘cmd’] then
            –print(wheel_x, wheel_y)
            if (wheel_x ~= 0) or (wheel_y ~= 0) then
                wheel_val = wheel_x + wheel_y
                –print(wheel_val)
                if wheel_val ~= 0 then
                    print(wheel_val)
                    if wheel_val > 0 then
                        print(“up”)
                        hs.eventtap.keyStroke({‘shift’,’cmd’}, ‘;’, 200)
                    else
                        print(“down”)
                        hs.eventtap.keyStroke({‘cmd’}, ‘-‘, 200)
                    end
                end
            end
        end
    end

end):start()

コメント

タイトルとURLをコピーしました