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
10 local capi
= { key
= key
}
11 local util
= require("awful.util")
13 --- Create easily new key objects ignoring certain modifiers.
15 local key
= { mt
= {} }
17 --- Modifiers to ignore.
18 -- By default this is initialized as { "Lock", "Mod2" }
19 -- so the Caps Lock or Num Lock modifier are not taking into account by awesome
20 -- when pressing keys.
21 -- @name ignore_modifiers
23 local ignore_modifiers
= { "Lock", "Mod2" }
25 --- Create a new key to use as binding.
26 -- This function is useful to create several keys from one, because it will use
27 -- the ignore_modifier variable to create more key with or without the ignored
28 -- modifiers activated.
29 -- For example if you want to ignore CapsLock in your keybinding (which is
30 -- ignored by default by this function), creating key binding with this function
31 -- will return 2 key objects: one with CapsLock on, and the other one with
34 -- @return A table with one or several key objects.
35 function key
.new(mod, _key
, press
, release
)
37 local subsets
= util
.subsets(ignore_modifiers
)
38 for _
, set
in ipairs(subsets
) do
39 ret
[#ret
+ 1] = capi
.key({ modifiers
= util
.table.join(mod, set
),
42 ret
[#ret
]:connect_signal("press", function(kobj
, ...) press(...) end)
45 ret
[#ret
]:connect_signal("release", function(kobj
, ...) release(...) end)
51 --- Compare a key object with modifiers and key.
52 -- @param _key The key object.
53 -- @param pressed_mod The modifiers to compare with.
54 -- @param pressed_key The key to compare with.
55 function key
.match(_key
, pressed_mod
, pressed_key
)
56 -- First, compare key.
57 if pressed_key
~= _key
.key
then return false end
59 local mod = _key
.modifiers
60 -- For each modifier of the key object, check that the modifier has been
62 for _
, m
in ipairs(mod) do
63 -- Has it been pressed?
64 if not util
.table.hasitem(pressed_mod
, m
) then
65 -- No, so this is failure!
69 -- If the number of pressed modifier is ~=, it is probably >, so this is not
70 -- the same, return false.
71 return #pressed_mod
== #mod
74 function key
.mt
:__call(...)
78 return setmetatable(key
, key
.mt
)
80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80