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>
|
2018-12-31 07:10:35 +01:00
|
|
|
-- @author Emmanuel Lepage Vallee <elv1313@gmail.com>
|
|
|
|
-- @copyright 2018 Emmanuel Lepage Vallee
|
2019-10-27 20:39:23 +01:00
|
|
|
-- @inputmodule 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")
|
2018-12-29 02:47:06 +01:00
|
|
|
local gdebug = require("gears.debug")
|
2019-10-16 06:41:34 +02:00
|
|
|
local gobject = require("gears.object")
|
2009-04-14 17:53:22 +02:00
|
|
|
|
2018-12-31 07:10:35 +01:00
|
|
|
--- The keyboard key used to trigger this keybinding.
|
|
|
|
--
|
|
|
|
-- It can be the key symbol, such as `space`, the character, such as ` ` or the
|
|
|
|
-- keycode such as `#65`.
|
|
|
|
--
|
|
|
|
-- @property key
|
|
|
|
-- @param string
|
|
|
|
|
|
|
|
--- The table of modifier keys.
|
|
|
|
--
|
|
|
|
-- A modifier, such as `Control` are a predetermined set of keys that can be
|
|
|
|
-- used to implement keybindings. Note that this list is fix and cannot be
|
|
|
|
-- extended using random key names, code or characters.
|
|
|
|
--
|
|
|
|
-- Common modifiers are:
|
|
|
|
--
|
|
|
|
-- <table class='widget_list' border=1>
|
|
|
|
-- <tr style='font-weight: bold;'>
|
|
|
|
-- <th align='center'>Name</th>
|
|
|
|
-- <th align='center'>Description</th>
|
|
|
|
-- </tr>
|
|
|
|
-- <tr><td>Mod1</td><td>Usually called Alt on PCs and Option on Macs</td></tr>
|
|
|
|
-- <tr><td>Mod4</td><td>Also called Super, Windows and Command ⌘</td></tr>
|
|
|
|
-- <tr><td>Mod5</td><td>Also called AltGr or ISO Level 3</td></tr>
|
|
|
|
-- <tr><td>Shift</td><td>Both left and right shift keys</td></tr>
|
|
|
|
-- <tr><td>Control</td><td>Also called CTRL on some keyboards</td></tr>
|
|
|
|
-- </table>
|
|
|
|
--
|
|
|
|
-- Please note that Awesome ignores the status of "Lock" and "Mod2" (Num Lock).
|
|
|
|
--
|
|
|
|
-- @property modifiers
|
|
|
|
-- @tparam table modifiers
|
|
|
|
|
|
|
|
--- The key description.
|
|
|
|
--
|
|
|
|
-- This is used, for example, by the `awful.hotkey_popup`.
|
|
|
|
--
|
|
|
|
-- @property description
|
|
|
|
-- @param string
|
|
|
|
|
|
|
|
--- The key name.
|
|
|
|
--
|
|
|
|
-- This can be useful when searching for keybindings by keywords.
|
|
|
|
--
|
|
|
|
-- @property name
|
|
|
|
-- @param string
|
|
|
|
|
|
|
|
--- The key group.
|
|
|
|
--
|
|
|
|
-- This is used, for example, by the `awful.hotkey_popup`.
|
|
|
|
--
|
|
|
|
-- @property group
|
|
|
|
-- @param string
|
|
|
|
|
|
|
|
--- The callback when this key is pressed.
|
|
|
|
--
|
|
|
|
-- @property on_press
|
|
|
|
-- @param function
|
|
|
|
|
|
|
|
--- The callback when this key is released.
|
|
|
|
--
|
|
|
|
-- @property on_release
|
|
|
|
-- @param function
|
|
|
|
|
2015-09-30 13:17:12 +02:00
|
|
|
local key = { mt = {}, hotkeys = {} }
|
|
|
|
|
2018-12-31 07:10:35 +01:00
|
|
|
local reverse_map = setmetatable({}, {__mode="k"})
|
|
|
|
|
|
|
|
function key:set_key(k)
|
|
|
|
for _, v in ipairs(self) do
|
|
|
|
v.key = k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function key:get_key()
|
|
|
|
return self[1].key
|
|
|
|
end
|
|
|
|
|
|
|
|
function key:set_modifiers(mod)
|
|
|
|
local subsets = gmath.subsets(key.ignore_modifiers)
|
|
|
|
for k, set in ipairs(subsets) do
|
|
|
|
self[k].modifiers = gtable.join(mod, set)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, prop in ipairs { "description", "group", "on_press", "on_release", "name" } do
|
|
|
|
key["get_"..prop] = function(self)
|
|
|
|
return reverse_map[self][prop]
|
|
|
|
end
|
|
|
|
key["set_"..prop] = function(self, value)
|
|
|
|
reverse_map[self][prop] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Execute this keybinding.
|
|
|
|
--
|
|
|
|
-- @method :trigger
|
|
|
|
|
|
|
|
function key:trigger()
|
|
|
|
local data = reverse_map[self]
|
|
|
|
if data.on_press then
|
|
|
|
data.on_press()
|
|
|
|
end
|
|
|
|
|
|
|
|
if data.on_release then
|
|
|
|
data.on_release()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-31 23:16:45 +01:00
|
|
|
function key:get_has_root_binding()
|
|
|
|
return capi.root.has_key(self)
|
|
|
|
end
|
|
|
|
|
2019-10-16 06:41:34 +02:00
|
|
|
-- This is used by the keygrabber and prompt to identify valid awful.key
|
|
|
|
-- objects. It *cannot* be put directly in the object since `capi` uses a lot
|
|
|
|
-- of `next` internally and fixing that would suck more.
|
|
|
|
function key:get__is_awful_key()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2018-12-31 07:10:35 +01:00
|
|
|
local function index_handler(self, k)
|
|
|
|
if key["get_"..k] then
|
|
|
|
return key["get_"..k](self)
|
|
|
|
end
|
|
|
|
|
|
|
|
if type(key[k]) == "function" then
|
2018-12-31 23:16:45 +01:00
|
|
|
return key[k]
|
2018-12-31 07:10:35 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local data = reverse_map[self]
|
|
|
|
assert(data)
|
|
|
|
|
|
|
|
return data[k]
|
|
|
|
end
|
|
|
|
|
|
|
|
local function newindex_handler(self, k, value)
|
|
|
|
if key["set_"..k] then
|
|
|
|
return key["set_"..k](self, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
local data = reverse_map[self]
|
|
|
|
assert(data)
|
|
|
|
|
|
|
|
data[k] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
local obj_mt = {
|
|
|
|
__index = index_handler,
|
|
|
|
__newindex = newindex_handler
|
|
|
|
}
|
|
|
|
|
2009-08-28 15:26:41 +02:00
|
|
|
--- Modifiers to ignore.
|
2018-12-31 07:10:35 +01:00
|
|
|
-- By default this is initialized as `{ "Lock", "Mod2" }`
|
2009-08-28 15:26:41 +02:00
|
|
|
-- 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
|
|
|
--- 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
|
2018-12-29 02:47:06 +01:00
|
|
|
-- @deprecated awful.key.execute
|
2016-04-08 08:45:29 +02:00
|
|
|
function key.execute(mod, k)
|
2018-12-29 02:47:06 +01:00
|
|
|
gdebug.deprecate("Use `awful.keyboard.emulate_key_combination` or "..
|
|
|
|
"`my_key:trigger()` instead of `awful.key.execute()`",
|
|
|
|
{deprecated_in=5}
|
|
|
|
)
|
2019-01-21 05:04:39 +01:00
|
|
|
|
2018-12-29 02:47:06 +01:00
|
|
|
require("awful.keyboard").emulate_key_combination(mod, k)
|
2016-04-08 08:45:29 +02:00
|
|
|
end
|
|
|
|
|
2018-12-31 07:10:35 +01:00
|
|
|
--- Create a new key binding.
|
2018-12-25 06:49:22 +01:00
|
|
|
--
|
2018-12-31 07:10:35 +01:00
|
|
|
-- @constructorfct2 awful.key
|
|
|
|
-- @tparam table args
|
|
|
|
-- @tparam function args.key The key to trigger an event. It can be the character
|
|
|
|
-- itself of `#+keycode` (**mandatory**).
|
|
|
|
-- @tparam function args.modifiers A list of modifier keys. Valid modifiers are:
|
|
|
|
-- `Any`, `Mod1`, Mod2`, `Mod3`, `Mod4`, `Mod5`, `Shift`, `Lock` and `Control`.
|
|
|
|
-- This argument is (**mandatory**).
|
|
|
|
-- @tparam function args.on_press Callback for when the key is pressed.
|
|
|
|
-- @tparam function args.on_release Callback for when the key is released.
|
|
|
|
|
|
|
|
--- Create a new key binding (alternate constructor).
|
|
|
|
--
|
|
|
|
-- @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. It can be the character
|
|
|
|
-- itself of `#+keycode`.
|
2015-06-19 20:49:53 +02:00
|
|
|
-- @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.
|
2018-12-31 07:10:35 +01:00
|
|
|
-- @tparam[opt] table data User data for key,
|
2015-09-30 13:17:12 +02:00
|
|
|
-- 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
|
|
|
|
2018-12-31 07:10:35 +01:00
|
|
|
local function new_common(mod, _key, press, release, data)
|
2015-09-30 13:17:12 +02:00
|
|
|
if type(release)=='table' then
|
|
|
|
data=release
|
|
|
|
release=nil
|
|
|
|
end
|
2018-12-31 07:10:35 +01:00
|
|
|
|
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
|
2019-10-16 06:41:34 +02:00
|
|
|
local sub_key = capi.key {
|
|
|
|
modifiers = gtable.join(mod, set),
|
|
|
|
key = _key
|
|
|
|
}
|
|
|
|
|
|
|
|
sub_key._private._legacy_convert_to = ret
|
|
|
|
|
|
|
|
sub_key:connect_signal("press", function(_, ...)
|
|
|
|
if ret.on_press then
|
|
|
|
ret.on_press(...)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
sub_key:connect_signal("release", function(_, ...)
|
|
|
|
if ret.on_release then
|
|
|
|
ret.on_release(...)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
ret[#ret + 1] = sub_key
|
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-12-31 07:10:35 +01:00
|
|
|
data.on_press = press
|
|
|
|
data.on_release = release
|
|
|
|
data._is_capi_key = false
|
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
|
|
|
|
2018-12-31 07:10:35 +01:00
|
|
|
-- Store the private data
|
|
|
|
reverse_map[ret] = data
|
|
|
|
|
|
|
|
--WARNING this object needs to expose only ordered keys for legacy reasons.
|
|
|
|
-- All other properties needs to be fully handled by the meta table and never
|
|
|
|
-- be stored directly in the object.
|
|
|
|
|
|
|
|
return setmetatable(ret, obj_mt)
|
|
|
|
end
|
|
|
|
|
|
|
|
function key.new(args, _key, press, release, data)
|
|
|
|
-- Assume this is the new constructor.
|
|
|
|
if not _key then
|
|
|
|
assert(not (press or release or data), "Calling awful.key() requires a key name")
|
|
|
|
return new_common(
|
|
|
|
args.modifiers,
|
|
|
|
args.key,
|
|
|
|
args.on_press,
|
|
|
|
args.on_release,
|
|
|
|
args
|
|
|
|
)
|
|
|
|
else
|
|
|
|
return new_common(args, _key, press, release, data)
|
|
|
|
end
|
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
|
|
|
|
|
2019-10-16 06:41:34 +02:00
|
|
|
gobject.properties(capi.key, {
|
|
|
|
auto_emit = true,
|
|
|
|
})
|
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
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
|