awesome/lib/awful/key.lua.in

72 lines
2.4 KiB
Lua
Raw Normal View History

---------------------------------------------------------------------------
-- @author Julien Danjou <julien@danjou.info>
-- @copyright 2009 Julien Danjou
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------
-- Grab environment we need
local setmetatable = setmetatable
local ipairs = ipairs
local unpack = unpack
local capi = { key = key }
local util = require("awful.util")
--- Key helper for awful
module("awful.key")
--- Modifiers to ignore
ignore_modifiers = { "Lock", "Mod2" }
--- Create a new key to use as binding.
-- This function is useful to create several keys from one, because it will use
-- the ignore_modifier variable to create more key with or without the ignored
-- modifiers activated.
-- For example if you want to ignore CapsLock in your keybinding (which is
-- ignored by default by this function), creatina key binding with this function
-- will return 2 key objects: one with CapsLock on, and the other one with
-- CapsLock off.
-- @see C api key() function for parameters.
-- @return A table with one or several key objects.
function new(mod, ...)
local ret = {}
local subsets = util.subsets(ignore_modifiers)
for _, set in ipairs(subsets) do
ret[#ret + 1] = capi.key(util.table.join(mod, set), unpack(arg))
end
return ret
end
--- Compare a key object with modifiers and key.
-- @param key The key object.
-- @param pressed_mod The modifiers to compare with.
-- @param pressed_key The key to compare with.
function match(key, pressed_mod, pressed_key)
-- First, compare key.
if pressed_key ~= key.key then return false end
-- Then, compare mod
local mod = key.modifiers
local nbmod = 0
-- For each modifier of the key object, check that the modifier has been
-- pressed.
for _, m in ipairs(mod) do
-- Has it been pressed?
if util.table.hasitem(pressed_mod, m) then
-- Yes, the number of modifier correctly pressed++
nbmod = nbmod + 1
else
-- No, so this is failure!
return false
end
end
-- If the number of pressed modifier is ~=, it is probably >, so this is not
-- the same, return false.
if nbmod ~= #mod then
return false
end
return true
end
setmetatable(_M, { __call = function(_, ...) return new(unpack(arg)) end })
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80