LoginSignup
6
3

More than 5 years have passed since last update.

Macのhキーのチャタリングをhammerspoonでソフトウェア的に解決する

Last updated at Posted at 2018-04-05

はじめに

三ヶ月くらい前にMacBookPro(2016 Late)を買ったのですが,hキーが一回押しただけなのに二回押されてしまうことが頻出してきました.
具体的には this と打ったつもりがthihs になったり thhis になったりしてとてもじゃないけどプログラミングできない環境になりました.
自然に治る気配もないので修理にだすのも考えたのですが,ハードウェア的な問題ですしもしかしたら修理費がかさむかもしれないのは嫌です.
というわけで,キーボードイベントを監視し何でも出来るhammerspoonくんを使ってソフトウェア的にこの問題を解決しました.

ソースコード

-- 0.2秒程度以内に二回以上押されると無視するようにする
local hPressed = false
function blockDoublePressedHKey(e)
    -- h キーは 4 番なのでそれ以外は無視
    if e:getKeyCode() ~= 4 then return false end
    if not hPressed then
        hPressed = true
        hs.timer.doAfter(0.18,function() hPressed = false end)
        return false
    else
        -- print("blocked H Key !!")
        return true
    end
end

hs.eventtap.new(
    {hs.eventtap.event.types.keyDown},
    blockDoublePressedHKey
):start()

Tips

hs.hotkey.bind を使わないところがミソで,この関数では書き換えた先が同じキーのイベントの時にhs.eventtap.event.newKeyEventを書き換えてしまい,不都合が生じます.

宣伝

https://github.com/Muratam/dotfiles/blob/master/home/.hammerspoon/init.lua
にてWindowsの「Win+上下左右」の窓移動をMacで再現するやつとかも置いてます.
MacのSplitWindowとかクソなんで…

参考

6
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
6
3