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
|
|
|
|
|
2019-11-18 08:03:51 +01:00
|
|
|
--- A group of keys.
|
|
|
|
--
|
|
|
|
-- The valid keygroups are:
|
|
|
|
--
|
|
|
|
-- * **numrow**: The row above the letters in the US PC-105/PC-104 keyboards
|
|
|
|
-- and its derivative. This is usually the number 1-9 followed by 0.
|
|
|
|
-- * **arrows**: The Left/Right/Top/Bottom keys usually located right of the
|
|
|
|
-- spacebar.
|
|
|
|
--
|
|
|
|
-- @property keygroup
|
|
|
|
|
2018-12-31 07:10:35 +01:00
|
|
|
--- 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
|
|
|
|
|
2020-02-20 03:25:37 +01:00
|
|
|
--- The description of the function run from a key binding.
|
2018-12-31 07:10:35 +01:00
|
|
|
--
|
2020-02-20 03:25:37 +01:00
|
|
|
-- This is used, for example, by `awful.hotkeys_popup`.
|
2018-12-31 07:10:35 +01:00
|
|
|
--
|
|
|
|
-- @property description
|
|
|
|
-- @param string
|
|
|
|
|
|
|
|
--- The key name.
|
|
|
|
--
|
|
|
|
-- This can be useful when searching for keybindings by keywords.
|
|
|
|
--
|
|
|
|
-- @property name
|
|
|
|
-- @param string
|
|
|
|
|
2020-02-20 03:25:37 +01:00
|
|
|
--- The key group bound to a function in a key binding.
|
2018-12-31 07:10:35 +01:00
|
|
|
--
|
2020-02-20 03:25:37 +01:00
|
|
|
-- This is used, for example, by `awful.hotkeys_popup`.
|
2018-12-31 07:10:35 +01:00
|
|
|
--
|
|
|
|
-- @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
|
|
|
|
2019-11-18 08:03:51 +01:00
|
|
|
local function new_common(mod, _keys, 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)
|
2019-11-18 08:03:51 +01:00
|
|
|
for _, key_pair in ipairs(_keys) do
|
|
|
|
for _, set in ipairs(subsets) do
|
|
|
|
local sub_key = capi.key {
|
|
|
|
modifiers = gtable.join(mod, set),
|
|
|
|
key = key_pair[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
sub_key._private._legacy_convert_to = ret
|
|
|
|
|
|
|
|
sub_key:connect_signal("press", function(_, ...)
|
|
|
|
if ret.on_press then
|
|
|
|
if key_pair[2] ~= nil then
|
|
|
|
ret.on_press(key_pair[2], ...)
|
|
|
|
else
|
|
|
|
ret.on_press(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
sub_key:connect_signal("release", function(_, ...)
|
|
|
|
if ret.on_release then
|
|
|
|
if key_pair[2] ~= nil then
|
|
|
|
ret.on_release(key_pair[2], ...)
|
|
|
|
else
|
|
|
|
ret.on_release(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
ret[#ret + 1] = sub_key
|
|
|
|
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
|
2019-11-18 08:03:51 +01:00
|
|
|
data.keys = _keys
|
2018-12-31 07:10:35 +01:00
|
|
|
data.on_press = press
|
|
|
|
data.on_release = release
|
|
|
|
data._is_capi_key = false
|
2019-11-18 08:03:51 +01:00
|
|
|
assert((not data.key) or type(data.key) == "string")
|
2015-09-30 13:17:12 +02:00
|
|
|
table.insert(key.hotkeys, data)
|
2019-11-18 08:03:51 +01:00
|
|
|
data.execute = function(_)
|
|
|
|
assert(#_keys == 1, "key:execute() makes no sense for groups")
|
|
|
|
key.execute(mod, _keys[1])
|
|
|
|
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
|
|
|
|
|
2020-02-20 03:25:37 +01:00
|
|
|
--- The default definitions of keygroups.
|
|
|
|
--
|
|
|
|
-- A definition for a keygroup (say, **arrows**) can be accessed by indexing
|
|
|
|
-- this table (e.g. `awful.key.keygroups.arrows`).
|
|
|
|
--
|
|
|
|
-- Every definition is given as an array, where every element is another array
|
|
|
|
-- with the following structure:
|
|
|
|
--
|
|
|
|
-- * The first element is a string representing a key, in any format the
|
|
|
|
-- property `key` will allow.
|
|
|
|
-- * The second element is a value. Key bindings created by `awful.key` and a
|
|
|
|
-- `keygroup` are bound to a 1-parameter function, whose parameter is this
|
|
|
|
-- second element.
|
|
|
|
--
|
|
|
|
-- As an example, **arrows** is currently defined thus:
|
|
|
|
--
|
|
|
|
-- arrows = {
|
|
|
|
-- {"Left" , "Left" },
|
|
|
|
-- {"Right" , "Right" },
|
|
|
|
-- {"Up" , "Up" },
|
|
|
|
-- {"Down" , "Down" },
|
|
|
|
-- }
|
|
|
|
--
|
|
|
|
-- This table is acessed internally by Awesome. Users will usually use key
|
|
|
|
-- bindings with the property `keygroup` instead of accessing this table
|
|
|
|
-- directly.
|
|
|
|
-- @name awful.key.keygroups
|
|
|
|
-- @class table
|
2020-02-19 02:48:39 +01:00
|
|
|
key.keygroups = {
|
2019-11-18 08:03:51 +01:00
|
|
|
numrow = {},
|
|
|
|
arrows = {
|
|
|
|
{"Left" , "Left" },
|
|
|
|
{"Right" , "Right" },
|
2020-02-19 01:57:44 +01:00
|
|
|
{"Up" , "Up" },
|
2020-02-19 02:48:39 +01:00
|
|
|
{"Down" , "Down" },
|
2019-11-18 08:03:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Technically, this isn't very popular, but we have been doing this for 12
|
|
|
|
-- years and nobody complained too loudly.
|
|
|
|
for i = 1, 10 do
|
2020-02-19 02:48:39 +01:00
|
|
|
table.insert(key.keygroups.numrow, {"#" .. i + 9, i == 10 and 0 or i})
|
2019-11-18 08:03:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Allow key objects to provide more than 1 key.
|
|
|
|
--
|
|
|
|
-- Some "groups" like arrows, the numpad, F-keys or numrow "belong together"
|
|
|
|
local function get_keys(args)
|
|
|
|
if not args.keygroup then return {{args.key}} end
|
|
|
|
|
|
|
|
-- Make sure nothing weird happens.
|
|
|
|
assert(
|
|
|
|
not args.key,
|
|
|
|
"Please provide either the `key` or `keygroup` property, not both"
|
|
|
|
)
|
|
|
|
|
2020-02-19 02:48:39 +01:00
|
|
|
assert(key.keygroups[args.keygroup], "Please provide a valid keygroup")
|
|
|
|
return key.keygroups[args.keygroup]
|
2019-11-18 08:03:51 +01:00
|
|
|
end
|
|
|
|
|
2018-12-31 07:10:35 +01:00
|
|
|
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")
|
2019-11-18 08:03:51 +01:00
|
|
|
|
|
|
|
local keys = get_keys(args)
|
|
|
|
|
2018-12-31 07:10:35 +01:00
|
|
|
return new_common(
|
|
|
|
args.modifiers,
|
2019-11-18 08:03:51 +01:00
|
|
|
keys,
|
2018-12-31 07:10:35 +01:00
|
|
|
args.on_press,
|
|
|
|
args.on_release,
|
|
|
|
args
|
|
|
|
)
|
|
|
|
else
|
2019-11-18 08:03:51 +01:00
|
|
|
return new_common(args, {{_key}}, press, release, data)
|
2018-12-31 07:10:35 +01:00
|
|
|
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
|