Skip to content

Input ​

Keyboard layout, media keys, screenshots, the cursor and the touchpad.

All of them build on your first config.

Keyboard layout and repeat ​

lua
local function set_up_keyboard(keyboard)
	keyboard.layout = "us"
	keyboard.repeat_rate = 30
	keyboard.repeat_delay = 300
end

for _, each_seat in ipairs(wm.input.seats:all()) do
	local keyboard = each_seat:keyboard()
	if keyboard then set_up_keyboard(keyboard) end
end
wm.input:on("new_keyboard", set_up_keyboard)
  • layout takes the names setxkbmap takes, such as "de" or "us,ru".
  • Plugging in a keyboard resets its layout, so new_keyboard sets it again. That event doesn't fire for keyboards that are already there when you save the config, so the loop above sets those.
  • Bindings match event.raw_key, the key without Shift or the layout's other levels. mod + Q works the same on a Russian layout.

Media keys ​

Volume through PipeWire's wpctl, brightness through brightnessctl, players through playerctl. Swap in the tools you use.

lua
local media_keys = {
	XF86AudioRaiseVolume = { "wpctl", "set-volume", "-l", "1", "@DEFAULT_AUDIO_SINK@", "5%+" },
	XF86AudioLowerVolume = { "wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", "5%-" },
	XF86AudioMute = { "wpctl", "set-mute", "@DEFAULT_AUDIO_SINK@", "toggle" },
	XF86AudioPlay = { "playerctl", "play-pause" },
	XF86AudioNext = { "playerctl", "next" },
	XF86AudioPrev = { "playerctl", "previous" },
	XF86MonBrightnessUp = { "brightnessctl", "set", "5%+" },
	XF86MonBrightnessDown = { "brightnessctl", "set", "5%-" },
}

for key, command in pairs(media_keys) do
	bind("", key, function()
		spawn(command[1], { table.unpack(command, 2) })
	end)
end
  • bind("", ...) is a key with no modifiers held.
  • spawn runs the program directly, with no shell. A program that isn't installed is logged.

Screenshots ​

Print saves the whole screen to ~/Pictures, Shift + Print copies an area you pick. It uses grim, slurp and wl-copy.

lua
bind("", "Print", function()
	spawn("sh", { "-c", 'mkdir -p ~/Pictures && grim ~/Pictures/$(date +%Y-%m-%d_%H-%M-%S).png' })
end)
bind("shift", "Print", function()
	spawn("sh", { "-c", 'grim -g "$(slurp)" - | wl-copy' })
end)

For a pipe or $(...), start sh -c with the command line.

The cursor ​

lua
local function style_cursor(pointer)
	pointer.cursor.theme = "Adwaita"
	pointer.cursor.size = 24
end

for _, each_seat in ipairs(wm.input.seats:all()) do
	local pointer = each_seat:pointer()
	if pointer then style_cursor(pointer) end
end
wm.input:on("new_pointer", style_cursor)

Without these, Hazel uses XCURSOR_THEME and XCURSOR_SIZE from its environment. Apps that draw their own cursor read those variables too, so set them before starting Hazel as well if your apps' cursors don't match.

Natural scrolling ​

Turns touchpad scrolling around, so the content follows your fingers. Mouse wheels stay as they are. Add wheel = true to natural to turn those around too.

lua
local natural = { finger = true }
local rescrolling = false

wm.input:on("pointer_scroll", function(event)
	if rescrolling or not natural[event.source] then return end
	event:prevent_default()
	local delta = event.delta
	if event.source == "wheel" then delta = Point(delta.x / 15, delta.y / 15) end
	rescrolling = true
	event.pointer:scroll(-delta.x, -delta.y, event.source)
	rescrolling = false
end)

The handler keeps the scroll from the app and sends it again the other way. pointer:scroll fires pointer_scroll once more, which rescrolling lets through. A wheel scroll is sent in notches of 15 pixels.

Touchpad swipes ​

A three-finger swipe left or right switches workspace.

Needs Workspaces.

lua
local swiped = 0

wm.input:on("pointer_gesture", function(event)
	if event.kind ~= "swipe" or event.fingers ~= 3 then return end
	if event.phase == "begin" then
		event:prevent_default()
		swiped = 0
	elseif event.phase == "update" then
		swiped = swiped + event.delta.x
	elseif not event.cancelled and math.abs(swiped) > 100 and wm.store.workspaces then
		local step = swiped < 0 and 1 or -1
		wm:emit("show_workspace", (wm.store.workspaces.current - 1 + step) % 9 + 1)
	end
end)

prevent_default() on the begin keeps the whole swipe from the app under the pointer. Two-finger scrolling isn't a gesture and still reaches apps.

All of them ​

lua
local function set_up_keyboard(keyboard)
	keyboard.layout = "us"
	keyboard.repeat_rate = 30
	keyboard.repeat_delay = 300
end

for _, each_seat in ipairs(wm.input.seats:all()) do
	local keyboard = each_seat:keyboard()
	if keyboard then set_up_keyboard(keyboard) end
end
wm.input:on("new_keyboard", set_up_keyboard)

local media_keys = {
	XF86AudioRaiseVolume = { "wpctl", "set-volume", "-l", "1", "@DEFAULT_AUDIO_SINK@", "5%+" },
	XF86AudioLowerVolume = { "wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", "5%-" },
	XF86AudioMute = { "wpctl", "set-mute", "@DEFAULT_AUDIO_SINK@", "toggle" },
	XF86AudioPlay = { "playerctl", "play-pause" },
	XF86AudioNext = { "playerctl", "next" },
	XF86AudioPrev = { "playerctl", "previous" },
	XF86MonBrightnessUp = { "brightnessctl", "set", "5%+" },
	XF86MonBrightnessDown = { "brightnessctl", "set", "5%-" },
}

for key, command in pairs(media_keys) do
	bind("", key, function()
		spawn(command[1], { table.unpack(command, 2) })
	end)
end

bind("", "Print", function()
	spawn("sh", { "-c", 'mkdir -p ~/Pictures && grim ~/Pictures/$(date +%Y-%m-%d_%H-%M-%S).png' })
end)
bind("shift", "Print", function()
	spawn("sh", { "-c", 'grim -g "$(slurp)" - | wl-copy' })
end)

local function style_cursor(pointer)
	pointer.cursor.theme = "Adwaita"
	pointer.cursor.size = 24
end

for _, each_seat in ipairs(wm.input.seats:all()) do
	local pointer = each_seat:pointer()
	if pointer then style_cursor(pointer) end
end
wm.input:on("new_pointer", style_cursor)

local natural = { finger = true }
local rescrolling = false

wm.input:on("pointer_scroll", function(event)
	if rescrolling or not natural[event.source] then return end
	event:prevent_default()
	local delta = event.delta
	if event.source == "wheel" then delta = Point(delta.x / 15, delta.y / 15) end
	rescrolling = true
	event.pointer:scroll(-delta.x, -delta.y, event.source)
	rescrolling = false
end)

local swiped = 0

wm.input:on("pointer_gesture", function(event)
	if event.kind ~= "swipe" or event.fingers ~= 3 then return end
	if event.phase == "begin" then
		event:prevent_default()
		swiped = 0
	elseif event.phase == "update" then
		swiped = swiped + event.delta.x
	elseif not event.cancelled and math.abs(swiped) > 100 and wm.store.workspaces then
		local step = swiped < 0 and 1 or -1
		wm:emit("show_workspace", (wm.store.workspaces.current - 1 + step) % 9 + 1)
	end
end)