2009-04-14 17:53:22 +02:00
|
|
|
---------------------------------------------------------------------------
|
2014-05-20 13:02:39 +02:00
|
|
|
--- Create easily new key objects ignoring certain modifiers.
|
|
|
|
--
|
2009-04-14 17:53:22 +02:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2009 Julien Danjou
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @classmod awful.key
|
2009-04-14 17:53:22 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local ipairs = ipairs
|
2019-01-21 05:04:39 +01:00
|
|
|
local capi = { key = key, root = root, awesome = awesome }
|
2017-03-08 21:18:33 +01:00
|
|
|
local gmath = require("gears.math")
|
|
|
|
local gtable = require("gears.table")
|
2009-04-14 17:53:22 +02:00
|
|
|
|
2015-09-30 13:17:12 +02:00
|
|
|
local key = { mt = {}, hotkeys = {} }
|
|
|
|
|
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.
|
2016-06-18 07:03:53 +02:00
|
|
|
-- @name awful.key.ignore_modifiers
|
2009-08-28 15:26:41 +02:00
|
|
|
-- @class table
|
2016-06-18 07:03:53 +02:00
|
|
|
key.ignore_modifiers = { "Lock", "Mod2" }
|
2009-04-14 17:53:22 +02:00
|
|
|
|
2016-04-08 08:45:29 +02:00
|
|
|
--- Convert the modifiers into pc105 key names
|
2019-01-21 05:04:39 +01:00
|
|
|
local conversion = nil
|
|
|
|
|
|
|
|
local function generate_conversion_map()
|
|
|
|
if conversion then return conversion end
|
|
|
|
|
|
|
|
local mods = capi.awesome._modifiers
|
|
|
|
assert(mods)
|
|
|
|
|
|
|
|
conversion = {}
|
|
|
|
|
|
|
|
for mod, keysyms in pairs(mods) do
|
|
|
|
for _, keysym in ipairs(keysyms) do
|
|
|
|
assert(keysym.keysym)
|
|
|
|
conversion[mod] = conversion[mod] or keysym.keysym
|
|
|
|
conversion[keysym.keysym] = mod
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return conversion
|
|
|
|
end
|
|
|
|
|
|
|
|
capi.awesome.connect_signal("xkb::map_changed" , function() conversion = nil end)
|
2016-04-08 08:45:29 +02:00
|
|
|
|
|
|
|
--- Execute a key combination.
|
|
|
|
-- If an awesome keybinding is assigned to the combination, it should be
|
|
|
|
-- executed.
|
2019-01-21 05:04:39 +01:00
|
|
|
--
|
|
|
|
-- To limit the chances of accidentally leaving a modifier key locked when
|
|
|
|
-- calling this function from a keybinding, make sure is attached to the
|
|
|
|
-- release event and not the press event.
|
|
|
|
--
|
2016-04-08 08:45:29 +02:00
|
|
|
-- @see root.fake_input
|
|
|
|
-- @tparam table mod A modified table. Valid modifiers are: Any, Mod1,
|
|
|
|
-- Mod2, Mod3, Mod4, Mod5, Shift, Lock and Control.
|
|
|
|
-- @tparam string k The key
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct awful.key.execute
|
2016-04-08 08:45:29 +02:00
|
|
|
function key.execute(mod, k)
|
2019-01-21 05:04:39 +01:00
|
|
|
local modmap = generate_conversion_map()
|
|
|
|
local active = capi.awesome._active_modifiers
|
|
|
|
|
|
|
|
-- Release all modifiers
|
|
|
|
for _, m in ipairs(active) do
|
|
|
|
assert(modmap[m])
|
|
|
|
root.fake_input("key_release", modmap[m])
|
|
|
|
end
|
|
|
|
|
2016-04-08 08:45:29 +02:00
|
|
|
for _, v in ipairs(mod) do
|
2019-01-21 05:04:39 +01:00
|
|
|
local m = modmap[v]
|
2016-04-08 08:45:29 +02:00
|
|
|
if m then
|
|
|
|
root.fake_input("key_press", m)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
root.fake_input("key_press" , k)
|
|
|
|
root.fake_input("key_release", k)
|
|
|
|
|
|
|
|
for _, v in ipairs(mod) do
|
2019-01-21 05:04:39 +01:00
|
|
|
local m = modmap[v]
|
2016-04-08 08:45:29 +02:00
|
|
|
if m then
|
|
|
|
root.fake_input("key_release", m)
|
|
|
|
end
|
|
|
|
end
|
2019-01-21 05:04:39 +01:00
|
|
|
|
|
|
|
-- Restore the previous modifiers all modifiers. Please note that yes,
|
|
|
|
-- there is a race condition if the user was fast enough to release the
|
|
|
|
-- key during this operation.
|
|
|
|
for _, m in ipairs(active) do
|
|
|
|
root.fake_input("key_press", modmap[m])
|
|
|
|
end
|
2016-04-08 08:45:29 +02:00
|
|
|
end
|
|
|
|
|
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
|
2015-06-19 20:49:53 +02:00
|
|
|
-- the ignore_modifier variable to create several keys with and without the
|
|
|
|
-- ignored modifiers activated.
|
2009-04-14 17:53:22 +02:00
|
|
|
-- For example if you want to ignore CapsLock in your keybinding (which is
|
2015-06-19 20:49:53 +02:00
|
|
|
-- ignored by default by this function), creating a key binding with this
|
|
|
|
-- function will return 2 key objects: one with CapsLock on, and another one
|
|
|
|
-- with CapsLock off.
|
2018-12-25 06:49:22 +01:00
|
|
|
--
|
2015-06-19 20:49:53 +02:00
|
|
|
-- @tparam table mod A list of modifier keys. Valid modifiers are: Any, Mod1,
|
|
|
|
-- Mod2, Mod3, Mod4, Mod5, Shift, Lock and Control.
|
|
|
|
-- @tparam string _key The key to trigger an event.
|
|
|
|
-- @tparam function press Callback for when the key is pressed.
|
2015-09-30 13:17:12 +02:00
|
|
|
-- @tparam[opt] function release Callback for when the key is released.
|
|
|
|
-- @tparam table data User data for key,
|
|
|
|
-- for example {description="select next tag", group="tag"}.
|
2015-06-19 20:49:53 +02:00
|
|
|
-- @treturn table A table with one or several key objects.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @constructorfct awful.key
|
2018-12-25 06:49:22 +01:00
|
|
|
|
2015-09-30 13:17:12 +02:00
|
|
|
function key.new(mod, _key, press, release, data)
|
|
|
|
if type(release)=='table' then
|
|
|
|
data=release
|
|
|
|
release=nil
|
|
|
|
end
|
2009-04-14 17:53:22 +02:00
|
|
|
local ret = {}
|
2017-03-08 21:18:33 +01:00
|
|
|
local subsets = gmath.subsets(key.ignore_modifiers)
|
2009-04-14 17:53:22 +02:00
|
|
|
for _, set in ipairs(subsets) do
|
2017-03-08 21:18:33 +01:00
|
|
|
ret[#ret + 1] = capi.key({ modifiers = gtable.join(mod, set),
|
2012-06-12 20:13:09 +02:00
|
|
|
key = _key })
|
2009-06-25 16:32:15 +02:00
|
|
|
if press then
|
2016-02-07 15:02:25 +01:00
|
|
|
ret[#ret]:connect_signal("press", function(_, ...) press(...) end)
|
2009-06-25 16:32:15 +02:00
|
|
|
end
|
|
|
|
if release then
|
2016-02-07 15:02:25 +01:00
|
|
|
ret[#ret]:connect_signal("release", function(_, ...) release(...) end)
|
2009-06-25 16:32:15 +02:00
|
|
|
end
|
2009-04-14 17:53:22 +02:00
|
|
|
end
|
2015-09-30 13:17:12 +02:00
|
|
|
|
|
|
|
-- append custom userdata (like description) to a hotkey
|
2017-03-08 21:18:33 +01:00
|
|
|
data = data and gtable.clone(data) or {}
|
2015-09-30 13:17:12 +02:00
|
|
|
data.mod = mod
|
|
|
|
data.key = _key
|
2018-07-21 20:24:37 +02:00
|
|
|
data.press = press
|
|
|
|
data.release = release
|
2015-09-30 13:17:12 +02:00
|
|
|
table.insert(key.hotkeys, data)
|
2016-04-08 08:45:29 +02:00
|
|
|
data.execute = function(_) key.execute(mod, _key) end
|
2015-09-30 13:17:12 +02:00
|
|
|
|
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.
|
2012-11-19 14:09:10 +01:00
|
|
|
-- @param _key The key object.
|
2009-04-27 18:35:04 +02:00
|
|
|
-- @param pressed_mod The modifiers to compare with.
|
|
|
|
-- @param pressed_key The key to compare with.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct awful.key.match
|
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?
|
2017-03-08 21:18:33 +01:00
|
|
|
if not gtable.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
|