better macos keyboard layout indicator
return

When using the menubar area to the right side of the notch for other textual information (e.g. currently playing media info), the built in macOS keyboard layout indicator takes up an annoying amount of space. I’ve created a hammerspoon script to display a small indicator just to the left of the notch that only appears when using the layout that is lesser used.

While the script is designed for just distinguishing whether a Japanese keyboard layout is active or not, it is straightforward enough to modify it for other layouts.

local indicator = nil
local indicatorSize = 20
-- will need to be changed depending on your display setup.
local indicatorX = hs.screen.mainScreen():frame().w/2 - indicatorSize - 120
local indicatorY = 11

local function createIndicator()
local canvas = hs.canvas.new({x = 0, y = 0, w = indicatorSize, h = indicatorSize})

canvas[1] = {
type = "circle",
action = "fill",
fillColor = {red = 0.0, green = 0.188, blue = 0.663, alpha = 1.0},
}

canvas[2] = {
type = "text",
text = "",
textSize = 12,
textColor = {white = 1.0, alpha = 1.0},
textAlignment = "center",
frame = {x = 0.5, y = 2, w = indicatorSize, h = indicatorSize}
}

local screen = hs.screen.mainScreen():frame()
local centerX = screen.w / 2
canvas:topLeft({x = indicatorX, y = indicatorY})

return canvas
end

local function updateIndicator()
if not indicator then
indicator = createIndicator()
end

local currentSource = hs.keycodes.currentSourceID()
local isJp = string.find(currentSource, "RomajiTyping.Japanese") ~= nil or
string.find(currentSource:lower(), "hiragana") ~= nil

if isJp then
indicator:show()
else
indicator:hide()
end
end

local inputWatcher = hs.keycodes.inputSourceChanged(function()
updateIndicator()
end)

updateIndicator()