ewmh.activate: focus and especially raise clients during startup
[awesome.git] / lib / awful / key.lua.in
blob5e211fde3f5fcc15945dd506b580ee9b90475674
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 capi = { key = key }
11 local util = require("awful.util")
13 --- Create easily new key objects ignoring certain modifiers.
14 -- awful.key
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
22 -- @class table
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
32 -- CapsLock off.
33 -- @see key
34 -- @return A table with one or several key objects.
35 function key.new(mod, _key, press, release)
36 local ret = {}
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),
40 key = _key })
41 if press then
42 ret[#ret]:connect_signal("press", function(kobj, ...) press(...) end)
43 end
44 if release then
45 ret[#ret]:connect_signal("release", function(kobj, ...) release(...) end)
46 end
47 end
48 return ret
49 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
58 -- Then, compare mod
59 local mod = _key.modifiers
60 -- For each modifier of the key object, check that the modifier has been
61 -- pressed.
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!
66 return false
67 end
68 end
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
72 end
74 function key.mt:__call(...)
75 return key.new(...)
76 end
78 return setmetatable(key, key.mt)
80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80