Skip to content

Class KeyEvent ​

One key press or release, passed to key handlers.

For bindings, compare raw_key and modifiers, and prevent both the press and the release:

lua
local function matches(binding, event)
	if event.raw_key ~= binding.key then return false end
	for _, modifier in ipairs({ "super", "shift", "ctrl", "alt" }) do
		if (binding.modifiers[modifier] or false) ~= event.modifiers[modifier] then return false end
	end
	return true
end

local close_binding = { key = Key.q, modifiers = { super = true } }
wm.input:on("key", function(event)
	if wm.is_session_locked or event.shortcuts_inhibited then return end
	if matches(close_binding, event) then
		event:prevent_default()
		if event.state == "Pressed" then
			local focused = seat:focused()
			if focused and focused.kind == "window" then focused:close() end
		end
	end
end)

Properties ​

event.keyboard: Keyboardkeyboard read only ​

The keyboard the key came from.

event.state: "Pressed" | "Released"state read only ​

Capitalised, unlike the state keyboard:key takes.

event.key: Keysymkey read only ​

The symbol the key produces with the current layout and modifiers. Shift and A gives A.

  • NoSymbol when the key produces several symbols at once. keys has them all.

event.raw_key: Keysymraw_key read only ​

The symbol without modifiers. Shift and A gives a. Use it for bindings.

  • When the active layout has no Latin letters, it comes from the first layout that has them, so a binding on Key.q still works with a Russian layout.

event.keys: Keysym[]keys read only ​

Every symbol the key produces with the current layout and modifiers. Usually one.

event.modifiers: Modifiersmodifiers read only ​

The modifiers after this key took effect. Pressing Shift gives shift = true on that same event, releasing it gives false.

event.keycode: numberkeycode read only ​

The xkb keycode, which is the evdev code plus 8. A is 38. keyboard:key takes the evdev code.

event.time: numbertime read only ​

The device's timestamp in milliseconds.

  • 0 for keys sent with keyboard:key.

event.serial: numberserial read only ​

A number that grows with each input event.

event.shortcuts_inhibited: booleanshortcuts_inhibited read only ​

Whether a ShortcutsInhibitor is active on this keyboard's seat. Hazel doesn't act on it. Key handlers check it and leave the key to the app.

Methods ​

event:prevent_default() -> nilprevent_default ​

Keeps the key from the focused app, or from the lock screen while the session is locked. Later handlers still see the event.

  • Prevent the press and the release together. An app that gets only one of them thinks the key is held, or sees a release it never saw pressed.