awful.key: fix modifier comparison in match()
[awesome.git] / lib / awful / key.lua.in
blobc961ab5eb93252f83621ba0ecc80c4e1a82e1e70
1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2009 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 -- Grab environment we need
8 local setmetatable = setmetatable
9 local ipairs = ipairs
10 local unpack = unpack
11 local capi = { key = key }
12 local util = require("awful.util")
14 --- Key helper for awful
15 module("awful.key")
17 --- Modifiers to ignore
18 ignore_modifiers = { "Lock", "Mod2" }
20 --- Create a new key to use as binding.
21 -- This function is useful to create several keys from one, because it will use
22 -- the ignore_modifier variable to create more key with or without the ignored
23 -- modifiers activated.
24 -- For example if you want to ignore CapsLock in your keybinding (which is
25 -- ignored by default by this function), creatina key binding with this function
26 -- will return 2 key objects: one with CapsLock on, and the other one with
27 -- CapsLock off.
28 -- @see C api key() function for parameters.
29 -- @return A table with one or several key objects.
30 function new(mod, ...)
31 local ret = {}
32 local subsets = util.subsets(ignore_modifiers)
33 for _, set in ipairs(subsets) do
34 ret[#ret + 1] = capi.key(util.table.join(mod, set), unpack(arg))
35 end
36 return ret
37 end
39 --- Compare a key object with modifiers and key.
40 -- @param key The key object.
41 -- @param pressed_mod The modifiers to compare with.
42 -- @param pressed_key The key to compare with.
43 function match(key, pressed_mod, pressed_key)
44 -- First, compare key.
45 if pressed_key ~= key.key then return false end
46 -- Then, compare mod
47 local mod = key.modifiers
48 local nbmod = 0
49 -- For each modifier of the key object, check that the modifier has been
50 -- pressed.
51 for _, m in ipairs(mod) do
52 -- Has it been pressed?
53 if util.table.hasitem(pressed_mod, m) then
54 -- Yes, the number of modifier correctly pressed++
55 nbmod = nbmod + 1
56 else
57 -- No, so this is failure!
58 return false
59 end
60 end
61 -- If the number of pressed modifier is ~=, it is probably >, so this is not
62 -- the same, return false.
63 if nbmod ~= #mod then
64 return false
65 end
66 return true
67 end
69 setmetatable(_M, { __call = function(_, ...) return new(unpack(arg)) end })
71 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80