apple-m1hammerspoon

Making the keyboard momentarily unresponsive in order to clean an M1 MacBook


I have been trying to find a way to clean the keyboard and body of my M1 MacBook Air. I used to just turn off my laptop, use a clean cloth to remove dust and fingerprints, and then turn it on. But this laptop cannot be disturbed at all without turning on: a key press, opening the lid, touching the trackpad, anything causes it to turn on. This feature (which, along with the impossibility to disable the turn-on sound, kinda bothers me) cannot be turned off, unlike with Intel MacBooks. I don't like to press keys and touch the keyboard with the computer being responsive, so I was wondering:

Is there a way to make the keyboard momentarily unresponsive? Maybe a Hammerspoon script?

I know this might seem like a silly question, but I would like to be able to handle my computer knowing I'm not gonna press a random key combination which is going to need undoing later. I think Apple should implement a "cleaning mode" where the computer is unresponsive so that one can clean the body of the computer every once in a while.

Thanks!


Solution

  • You need to setup an event tap for the keyboard. You can tap into a variety of events, for example the key down event. Whenever that event is triggered a callback function is executed which is passed the event data (e). Just return true if you want to block the event. That should disable your keyboard.

    You can extract the key code and flags (ctrl, shift, option...) from the event. This will allow you to program a shortcut to stop the event tap and make your keyboard functionally again.

    For example:

    local events = hs.eventtap.event.types
    keyboardTracker = hs.eventtap.new({ events.keyDown }, function (e)
      local keyCode = e:getKeyCode()
      local flags = e:getFlags()
      if flags.ctrl and keyCode == hs.keycodes.map.f then
        return false
      end
      return true
    end)
    keyboardTracker:start()
    

    Be careful, if an unhandled error is thrown from within the callback function you effective disable your keyboard.

    Tapping into mouse events is similar.