Posted on

I bet you've been in a situation where you need to quickly mute or unmute your microphone on a video call. It's a common scenario for remote workers: you're in a long meeting, and suddenly a service or server goes down. You need to check it out and fix it fast, but you can't just leave the meeting. So, you mute yourself, quickly switch to the terminal, and start investigating the issue. A couple of minutes later, someone asks you a question, and now everyone is waiting for you to answer. This awkward silence settles in while you're trying to find the right window and the right button to unmute yourself and respond.

Most apps have a keyboard shortcut, but it only works inside the app. So in this scenario that I just described it wouldn't work.

I wish my Macbook had a built-in media key to toggle the microphone, just like my Thinkpad Nano does. But it doesn't. So I had to find a workaround...

The shell script

After some investigation I realized that I can control the microphone volume via Applescript with OSAScript (Open Scripting Architecture). So I wrote a simple shell script that alternates between 0% and 100% input volume, which in practice is the same as muting and unmuting the microphone.

#!/bin/sh

if [ $(osascript -e "input volume of (get volume settings)") -eq 0 ]; then
        osascript -e "set volume input volume 100"
        osascript -e "display notification \"You are now unmuted\" with title \"🎤 on\" sound name \"Funk\""
else
        osascript -e "set volume input volume 0"
        osascript -e "display notification \"You are now muted\" with title \"🎤 off\" sound name \"Funk\""
fi

I saved this file on my ~/.dotfiles/bin directory with the name toggle_mic and gave it permissions to execute:

chmod +x ~/.dotfiles/bin/toggle_mic

You can simply add this as a bash/zsh function instead if you just want to call it from the terminal. But I wanted to bind it to a keyboard key so saving it as an executable file was a much more flexible solution.

It would also be interesting to have a status bar icon indicating the current status of the microphone, but it's not possible to do without relying on a third party app. In my case, I have that status indicator via the app Krisp.ai that displays a red cross on top of it's menu bar icon when either sound input or output are muted.

The Krisp.ai menu bar icon
    indicating that the mic is disabled
Krisp.ai app menu bar icon showing that the microphone is muted.

The keyboard shortcut

I thought this step would be simple with me having to just use configure my Keychron K3 Max to bind a key to run this script, but I couldn't figure out how to do it with VIA, probably it's not even possible. So I ended up using the app Karabiner-Elements to map my Del key to run the above script.

To map a key to run a shell script, just add the following rule to your complex_modifications rules in your ~/.config/karabiner/karabiner.json config file:

{
  "description": "Toggle Mic",
  "manipulators": [
    {
      "from": {
        "key_code": "delete_forward"
      },
      "to": [
        {
          "shell_command": "~/bin/toggle_mic"
        }
      ],
      "type": "basic"
    }
  ]
}

Or if you prefer the UI:

The created shortcut on
    Karabiner Elements app
Creating a new shortcut to map the Del key to a shell command on Karabiner Elements app.

And now, the final touch, the keycap. I replaced the Del keycap with a Mic to make it stand out.

My Keychron K3 Max keyboard
    with a mic key replacing the original Del key
My Keychron K3 Max with a Mic keycap where once the Del key was.

This setup is working for me, even though there are some rough edges that could use some improvement:

  • Apps do not show that you're muted. The mute setting is at the OS level.
  • I have to rely on the Krisp.ai app to have a visual indicator of the mute status