2009-04-14 17:53:22 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2009 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local ipairs = ipairs
|
|
|
|
local capi = { key = key }
|
|
|
|
local util = require("awful.util")
|
|
|
|
|
2009-08-28 15:26:41 +02:00
|
|
|
--- Create easily new key objects ignoring certain modifiers.
|
2012-06-12 20:13:09 +02:00
|
|
|
-- awful.key
|
|
|
|
local key = { mt = {} }
|
2009-04-14 17:53:22 +02:00
|
|
|
|
2009-08-28 15:26:41 +02:00
|
|
|
--- Modifiers to ignore.
|
|
|
|
-- By default this is initialized as { "Lock", "Mod2" }
|
|
|
|
-- so the Caps Lock or Num Lock modifier are not taking into account by awesome
|
|
|
|
-- when pressing keys.
|
|
|
|
-- @name ignore_modifiers
|
|
|
|
-- @class table
|
2012-06-12 20:13:09 +02:00
|
|
|
local ignore_modifiers = { "Lock", "Mod2" }
|
2009-04-14 17:53:22 +02:00
|
|
|
|
|
|
|
--- 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
|
2009-08-28 15:26:41 +02:00
|
|
|
-- ignored by default by this function), creating key binding with this function
|
2009-04-14 17:53:22 +02:00
|
|
|
-- will return 2 key objects: one with CapsLock on, and the other one with
|
|
|
|
-- CapsLock off.
|
2009-05-01 15:37:17 +02:00
|
|
|
-- @see capi.key
|
2009-04-17 11:13:22 +02:00
|
|
|
-- @return A table with one or several key objects.
|
2012-06-12 20:13:09 +02:00
|
|
|
function key.new(mod, _key, press, release)
|
2009-04-14 17:53:22 +02:00
|
|
|
local ret = {}
|
|
|
|
local subsets = util.subsets(ignore_modifiers)
|
|
|
|
for _, set in ipairs(subsets) do
|
2009-06-25 16:32:15 +02:00
|
|
|
ret[#ret + 1] = capi.key({ modifiers = util.table.join(mod, set),
|
2012-06-12 20:13:09 +02:00
|
|
|
key = _key })
|
2009-06-25 16:32:15 +02:00
|
|
|
if press then
|
2009-10-09 20:39:55 +02:00
|
|
|
ret[#ret]:connect_signal("press", function(kobj, ...) press(...) end)
|
2009-06-25 16:32:15 +02:00
|
|
|
end
|
|
|
|
if release then
|
2009-10-09 20:39:55 +02:00
|
|
|
ret[#ret]:connect_signal("release", function(kobj, ...) release(...) end)
|
2009-06-25 16:32:15 +02:00
|
|
|
end
|
2009-04-14 17:53:22 +02:00
|
|
|
end
|
2009-04-17 11:13:22 +02:00
|
|
|
return ret
|
2009-04-14 17:53:22 +02:00
|
|
|
end
|
|
|
|
|
2009-04-27 18:35:04 +02:00
|
|
|
--- 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.
|
2012-06-12 20:13:09 +02:00
|
|
|
function key.match(_key, pressed_mod, pressed_key)
|
2009-04-27 18:35:04 +02:00
|
|
|
-- First, compare key.
|
2012-06-12 20:13:09 +02:00
|
|
|
if pressed_key ~= _key.key then return false end
|
2009-04-27 18:35:04 +02:00
|
|
|
-- Then, compare mod
|
2012-06-12 20:13:09 +02:00
|
|
|
local mod = _key.modifiers
|
2009-04-28 20:02:10 +02:00
|
|
|
-- For each modifier of the key object, check that the modifier has been
|
|
|
|
-- pressed.
|
|
|
|
for _, m in ipairs(mod) do
|
|
|
|
-- Has it been pressed?
|
2009-04-28 22:48:24 +02:00
|
|
|
if not util.table.hasitem(pressed_mod, m) then
|
2009-04-28 20:02:10 +02:00
|
|
|
-- No, so this is failure!
|
|
|
|
return false
|
2009-04-27 18:35:04 +02:00
|
|
|
end
|
|
|
|
end
|
2009-04-28 20:02:10 +02:00
|
|
|
-- If the number of pressed modifier is ~=, it is probably >, so this is not
|
|
|
|
-- the same, return false.
|
2012-05-13 00:23:05 +02:00
|
|
|
return #pressed_mod == #mod
|
2009-04-27 18:35:04 +02:00
|
|
|
end
|
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
function key.mt:__call(...)
|
|
|
|
return key.new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(key, key.mt)
|
2009-04-14 17:53:22 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|