2012-04-30 17:44:58 +02:00
|
|
|
---------------------------------------------------------------------------
|
2018-07-21 20:18:44 +02:00
|
|
|
--- A keyboard grabbing and transaction object.
|
|
|
|
--
|
|
|
|
-- This module allows to grab all keyboard inputs until stopped. It is used
|
|
|
|
-- to gather input data (such as in `awful.prompt`), implement VI-like keybindings
|
|
|
|
-- or multi key transactions such as emulating the Alt+Tab behavior.
|
|
|
|
--
|
|
|
|
-- Note that this module has been redesigned in Awesome 4.3 to be object oriented
|
|
|
|
-- and stateful. The use of the older global keygrabbing API is discouraged
|
|
|
|
-- going forward since it had problem with recursive keygrabbers and required
|
|
|
|
-- a lot of boiler plate code to get anything done.
|
|
|
|
--
|
|
|
|
-- Using keygrabber as transactions
|
|
|
|
-- --------------------------------
|
|
|
|
--
|
|
|
|
-- The transactional keybindings are keybindings that start with a normal
|
|
|
|
-- keybindings, but only end when a (mod)key is released. In the classic
|
|
|
|
-- "Alt+Tab" transaction, first pressing "Alt+Tab" will display a popup listing
|
|
|
|
-- all windows (clients). This popup will only disappear when "Alt" is released.
|
|
|
|
--
|
|
|
|
-- @DOC_text_awful_keygrabber_alttab_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- In that example, because `export_keybindings` is set to `true`, pressing
|
|
|
|
-- `alt+tab` or `alt+shift+tab` will start the transaction even if the
|
|
|
|
-- keygrabber is not (yet) running.
|
|
|
|
--
|
|
|
|
-- Using keygrabber for modal keybindings (VI like)
|
|
|
|
-- ------------------------------------------------
|
|
|
|
--
|
2021-10-05 04:20:03 +02:00
|
|
|
-- VI-like modal keybindings are triggered by a key, like `Escape`, followed by
|
2018-07-21 20:18:44 +02:00
|
|
|
-- either a number, an adjective (or noun) and closed by a verb. For example
|
|
|
|
-- `<Escape>+2+t+f` could mean "focus (f) the second (2) tag (t)".
|
|
|
|
-- `<Escape>+2+h+t+f` would "focus (f) two (2) tags (t) to the right (h)".
|
|
|
|
--
|
|
|
|
-- Here is a basic implementation of such a system. Note that the action
|
|
|
|
-- functions themselves are not implemented to keep the example size and
|
|
|
|
-- complexity to a minimum. The implementation is just if/elseif of all action
|
|
|
|
-- and the code can be found in the normal `rc.lua` keybindings section:
|
|
|
|
--
|
|
|
|
-- @DOC_text_awful_keygrabber_vimode_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- Using signals
|
|
|
|
-- -------------
|
|
|
|
--
|
|
|
|
-- When the keygrabber is running, it will emit signals on each event. The
|
|
|
|
-- format is "key_name".."::".."pressed_or_released". For example, to attach
|
|
|
|
-- a callback to `Escape` being pressed, do:
|
|
|
|
--
|
|
|
|
-- mykeygrabber:connect_signal("Escape::pressed", function(self, modifiers, modset)
|
|
|
|
-- print("Escape called!")
|
|
|
|
-- end)
|
|
|
|
--
|
|
|
|
-- The `self` argument is the keygrabber instance. The `modifiers` is a list of
|
|
|
|
-- all the currently pressed modifiers and the `modset` is the same table, but
|
|
|
|
-- with the modifiers as keys instead of value. It allow to save time by
|
|
|
|
-- checking `if modset.Mod4 then ... end` instead of looping into the `modifiers`
|
|
|
|
-- table or using `gears.table`.
|
2014-05-20 13:02:39 +02:00
|
|
|
--
|
2012-04-30 17:44:58 +02:00
|
|
|
-- @author dodo
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @author Emmanuel Lepage Vallee <elv1313@gmail.com>
|
2012-04-30 17:44:58 +02:00
|
|
|
-- @copyright 2012 dodo
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @copyright 2017 Emmanuel Lepage Vallee
|
2019-06-06 08:40:00 +02:00
|
|
|
-- @coreclassmod awful.keygrabber
|
2012-04-30 17:44:58 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local ipairs = ipairs
|
|
|
|
local table = table
|
2018-07-21 20:18:44 +02:00
|
|
|
local gdebug = require('gears.debug')
|
|
|
|
local akey = require("awful.key")
|
|
|
|
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)
|
|
|
|
local gtable = require("gears.table")
|
|
|
|
local gobject = require("gears.object")
|
|
|
|
local gtimer = require("gears.timer")
|
2018-12-29 03:06:32 +01:00
|
|
|
local akeyboard = require("awful.keyboard")
|
2018-07-21 20:18:44 +02:00
|
|
|
local glib = require("lgi").GLib
|
2019-01-21 05:04:39 +01:00
|
|
|
local capi = { keygrabber = keygrabber, root = root, awesome = awesome }
|
2012-04-30 17:44:58 +02:00
|
|
|
|
2018-07-21 20:18:44 +02:00
|
|
|
local keygrab = {}
|
2012-04-30 17:44:58 +02:00
|
|
|
|
|
|
|
-- Private data
|
|
|
|
local grabbers = {}
|
|
|
|
local keygrabbing = false
|
|
|
|
|
2018-07-21 20:18:44 +02:00
|
|
|
local keygrabber = {
|
|
|
|
object = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Instead of checking for every modifiers, check the key directly.
|
2019-01-21 05:04:39 +01:00
|
|
|
local conversion = nil
|
2018-07-21 20:18:44 +02:00
|
|
|
|
|
|
|
--BEGIN one day create a proper API to add and remove keybindings at runtime.
|
|
|
|
-- Doing it this way is horrible.
|
|
|
|
|
2019-01-21 05:04:39 +01:00
|
|
|
-- Read the modifiers name and map their keysyms to the modkeys
|
|
|
|
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[keysym.keysym] = mod
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return conversion
|
|
|
|
end
|
|
|
|
|
|
|
|
capi.awesome.connect_signal("xkb::map_changed" , function() conversion = nil end)
|
|
|
|
|
2018-07-21 20:18:44 +02:00
|
|
|
local function add_root_keybindings(self, list)
|
|
|
|
assert(
|
|
|
|
list, "`add_root_keybindings` needs to be called with a list of keybindings"
|
|
|
|
)
|
|
|
|
|
2018-12-29 03:06:32 +01:00
|
|
|
for _, kb in ipairs(list) do
|
|
|
|
if kb.on_press then
|
|
|
|
local old_press = kb.on_press
|
|
|
|
kb.on_press = function(...)
|
|
|
|
self:start()
|
|
|
|
old_press(...)
|
2018-07-21 20:18:44 +02:00
|
|
|
end
|
2018-12-29 03:06:32 +01:00
|
|
|
end
|
2018-07-21 20:18:44 +02:00
|
|
|
|
2018-12-29 03:06:32 +01:00
|
|
|
if kb.on_release then
|
|
|
|
local old_release = kb.on_release
|
|
|
|
kb.on_release = function(...)
|
|
|
|
self:start()
|
|
|
|
old_release(...)
|
|
|
|
end
|
|
|
|
end
|
2018-07-21 20:18:44 +02:00
|
|
|
|
2018-12-29 03:06:32 +01:00
|
|
|
akeyboard.append_global_keybinding(kb)
|
2018-07-21 20:18:44 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--END hack
|
2012-04-30 17:44:58 +02:00
|
|
|
|
|
|
|
local function grabber(mod, key, event)
|
2016-02-07 15:02:25 +01:00
|
|
|
for _, keygrabber_function in ipairs(grabbers) do
|
2012-04-30 17:44:58 +02:00
|
|
|
-- continue if the grabber explicitly returns false
|
2015-07-13 21:13:58 +02:00
|
|
|
if keygrabber_function(mod, key, event) ~= false then
|
2012-04-30 17:44:58 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-29 02:13:52 +02:00
|
|
|
local function stop(self, stop_key, stop_mods)
|
|
|
|
keygrab.stop(self.grabber)
|
|
|
|
|
|
|
|
local timer = self._private.timer
|
|
|
|
if timer and timer.started then
|
|
|
|
timer:stop()
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.stop_callback then
|
|
|
|
self.stop_callback(
|
|
|
|
self.current_instance, stop_key, stop_mods, self.sequence
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
keygrab.emit_signal("property::current_instance", nil)
|
|
|
|
|
|
|
|
self.grabber = nil
|
|
|
|
self:emit_signal("stopped")
|
|
|
|
end
|
|
|
|
|
2018-07-21 20:18:44 +02:00
|
|
|
local function runner(self, modifiers, key, event)
|
2019-01-21 05:04:39 +01:00
|
|
|
local converted = generate_conversion_map()[key]
|
|
|
|
|
2018-07-21 20:18:44 +02:00
|
|
|
-- Stop the keygrabber with the `stop_key`
|
2019-01-21 05:04:39 +01:00
|
|
|
if (key == self.stop_key or (converted and converted == self.stop_key))
|
|
|
|
and event == self.stop_event and self.stop_key then
|
2022-08-29 02:13:52 +02:00
|
|
|
stop(self, key, modifiers)
|
2018-07-21 20:18:44 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Stop when only a subset of keys are allowed and it isn't one of them.
|
|
|
|
if self._private.allowed_keys and not self._private.allowed_keys[key] then
|
2022-08-29 02:13:52 +02:00
|
|
|
stop(self, key, modifiers)
|
2018-07-21 20:18:44 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Support multiple stop keys
|
2018-10-16 14:42:56 +02:00
|
|
|
if type(self.stop_key) == "table" and event == self.stop_event then
|
2018-07-21 20:18:44 +02:00
|
|
|
for _, k in ipairs(self.stop_key) do
|
|
|
|
if k == key then
|
2022-08-29 02:13:52 +02:00
|
|
|
stop(self, k, modifiers)
|
2018-07-21 20:18:44 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-21 05:04:39 +01:00
|
|
|
local is_modifier = converted ~= nil
|
2018-07-21 20:18:44 +02:00
|
|
|
|
|
|
|
-- Reset the inactivity timer on each events.
|
|
|
|
if self._private.timer and self._private.timer.started then
|
|
|
|
self._private.timer:again()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Lua strings are bytes, to handle UTF-8, use GLib
|
|
|
|
local seq_len = glib.utf8_strlen(self.sequence, -1)
|
|
|
|
|
|
|
|
-- Record the key sequence
|
2019-04-25 02:15:39 +02:00
|
|
|
if key == "BackSpace" and seq_len > 0 and event == "release" then
|
|
|
|
self.sequence = glib.utf8_substring(self.sequence, 0, seq_len - 1)
|
2018-07-21 20:18:44 +02:00
|
|
|
elseif glib.utf8_strlen(key, -1) == 1 and event == "release" then
|
|
|
|
self.sequence = self.sequence..key
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Convert index array to hash table
|
|
|
|
local mod = {}
|
|
|
|
for _, v in ipairs(modifiers) do mod[v] = true end
|
|
|
|
|
|
|
|
-- Emit some signals so the user can connect to a single type of event.
|
|
|
|
self:emit_signal(key.."::"..event, modifiers, mod)
|
|
|
|
|
|
|
|
local filtered_modifiers = {}
|
|
|
|
|
|
|
|
-- User defined cases
|
2022-09-06 02:23:42 +02:00
|
|
|
if self._private.keybindings[key] then
|
2018-07-21 20:18:44 +02:00
|
|
|
-- Remove caps and num lock
|
|
|
|
for _, m in ipairs(modifiers) do
|
|
|
|
if not gtable.hasitem(akey.ignore_modifiers, m) then
|
|
|
|
table.insert(filtered_modifiers, m)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for _,v in ipairs(self._private.keybindings[key]) do
|
2018-12-29 03:06:32 +01:00
|
|
|
if #filtered_modifiers == #v.modifiers then
|
2018-07-21 20:18:44 +02:00
|
|
|
local match = true
|
2018-12-29 03:06:32 +01:00
|
|
|
for _,v2 in ipairs(v.modifiers) do
|
2018-07-21 20:18:44 +02:00
|
|
|
match = match and mod[v2]
|
|
|
|
end
|
|
|
|
|
2022-09-06 00:50:43 +02:00
|
|
|
if match then
|
|
|
|
self:emit_signal("keybinding::triggered", v, event)
|
|
|
|
|
|
|
|
if event == "press" and v.on_press then
|
|
|
|
v.on_press(self)
|
|
|
|
|
|
|
|
if self.mask_event_callback ~= false then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
elseif event == "release" and v.on_release then
|
|
|
|
v.on_release(self)
|
2018-07-21 20:18:44 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Do not call the callbacks on modkeys
|
|
|
|
if is_modifier and self.mask_modkeys then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.keypressed_callback and event == "press" then
|
|
|
|
self.keypressed_callback(self, modifiers, key, event)
|
|
|
|
elseif self.keyreleased_callback and event == "release" then
|
|
|
|
self.keyreleased_callback(self, modifiers, key, event)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-30 17:44:58 +02:00
|
|
|
--- Stop grabbing the keyboard for the provided callback.
|
2018-07-21 20:18:44 +02:00
|
|
|
--
|
2015-01-21 08:33:03 +01:00
|
|
|
-- When no callback is given, the last grabber gets removed (last one added to
|
|
|
|
-- the stack).
|
2018-07-21 20:18:44 +02:00
|
|
|
--
|
|
|
|
-- @param[opt] g The key grabber that must be removed.
|
|
|
|
-- @deprecated awful.keygrabber.stop
|
|
|
|
function keygrab.stop(g)
|
|
|
|
-- If `run` is used directly and stop() is called with `g==nil`, get the
|
|
|
|
-- first keygrabber.
|
|
|
|
g = g
|
|
|
|
or keygrab.current_instance and keygrab.current_instance.grabber
|
|
|
|
or grabbers[#grabbers]
|
|
|
|
|
2012-04-30 17:44:58 +02:00
|
|
|
for i, v in ipairs(grabbers) do
|
|
|
|
if v == g then
|
|
|
|
table.remove(grabbers, i)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2018-07-21 20:18:44 +02:00
|
|
|
|
|
|
|
if g and keygrab.current_instance and keygrab.current_instance.grabber == g then
|
|
|
|
keygrab.current_instance = nil
|
|
|
|
end
|
|
|
|
|
2015-01-21 08:33:03 +01:00
|
|
|
-- Stop the global key grabber if the last grabber disappears from stack.
|
2012-04-30 17:44:58 +02:00
|
|
|
if #grabbers == 0 then
|
|
|
|
keygrabbing = false
|
|
|
|
capi.keygrabber.stop()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-21 20:18:44 +02:00
|
|
|
--- The keygrabber timeout.
|
|
|
|
--
|
|
|
|
-- @DOC_text_awful_keygrabber_timeout_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property timeout
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam[opt=nil] number|nil timeout
|
|
|
|
-- @propertyunit second
|
|
|
|
-- @negativeallowed false
|
|
|
|
-- @propertytype nil No timeout.
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @see gears.timer
|
|
|
|
-- @see timeout_callback
|
|
|
|
|
|
|
|
--- The key on which the keygrabber listen to terminate itself.
|
|
|
|
--
|
2018-10-16 14:42:56 +02:00
|
|
|
-- When this is set, the running keygrabber will quit when [one of] the stop
|
2018-07-21 20:18:44 +02:00
|
|
|
-- key event occurs.
|
|
|
|
--
|
|
|
|
-- By default, the event is `press`. It is common for use case like the
|
|
|
|
-- `awful.prompt` where `return` (enter) will terminate the keygrabbing. Using
|
|
|
|
-- `release` as an event is more appropriate when the keygrabber is tied to a
|
|
|
|
-- modifier key. For example, an Alt+Tab implementation stops when `mod1` (Alt)
|
|
|
|
-- is released.
|
|
|
|
--
|
|
|
|
-- It can also be a table containing many keys (as values).
|
|
|
|
--
|
|
|
|
-- Please note that modkeys are not accepted as `stop_key`s. You have to use
|
|
|
|
-- their corresponding key names such as `Control_L`.
|
|
|
|
--
|
|
|
|
-- @property stop_key
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam[opt=nil] string|table|nil stop_key
|
|
|
|
-- @propertyunit nil No stop key.
|
|
|
|
-- @propertyunit string A single stop key.
|
|
|
|
-- @propertyunit table One or more stop key(s).
|
|
|
|
-- @tablerowtype A list of key names, such as `"Control"` or `"a"`.
|
2018-10-16 14:42:56 +02:00
|
|
|
-- @see stop_event
|
2018-07-21 20:18:44 +02:00
|
|
|
|
|
|
|
--- The event on which the keygrabbing will be terminated.
|
|
|
|
--
|
2018-10-16 14:42:56 +02:00
|
|
|
-- @property stop_event
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam[opt="press"] string stop_event
|
|
|
|
-- @propertyvalue "press" When the keyboard key is first pressed.
|
|
|
|
-- @propertyvalue "release" When the keyboard key is released.
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @see stop_key
|
|
|
|
|
|
|
|
--- Whether or not to execute the key press/release callbacks when keybindings are called.
|
|
|
|
--
|
|
|
|
-- When this property is set to false, the `keyreleased_callback` and
|
|
|
|
-- `keypressed_callback` callbacks will be executed even when the event was
|
|
|
|
-- consumed by a `keybinding`.
|
|
|
|
--
|
|
|
|
-- By default, keybindings block those callbacks from being executed.
|
|
|
|
--
|
|
|
|
-- @property mask_event_callback
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam[opt=true] boolean mask_event_callback
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @see keybindings
|
|
|
|
-- @see keyreleased_callback
|
|
|
|
-- @see keypressed_callback
|
|
|
|
|
|
|
|
--- Do not call the callbacks on modifier keys (like `Control` or `Mod4`) events.
|
|
|
|
-- @property mask_modkeys
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam[opt=false] boolean mask_modkeys
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @see mask_event_callback
|
|
|
|
|
|
|
|
--- Export all keygrabber keybindings as root (global) keybindings.
|
|
|
|
--
|
|
|
|
-- When this is enabled, calling all of the keygrabber object `keybinding`s will
|
|
|
|
-- will create root `awful.key` and will automatically starts the grabbing.
|
|
|
|
--
|
|
|
|
-- Use this with caution. If many keygrabber or "real" root keybindings are set
|
|
|
|
-- on the same key combination, they are all executed and there is almost no
|
|
|
|
-- safe way to undo that. Make sure the `keygrabber` that use this option
|
|
|
|
-- have a single instance.
|
|
|
|
--
|
|
|
|
-- @property export_keybindings
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam[opt=false] boolean export_keybindings
|
2018-07-21 20:18:44 +02:00
|
|
|
|
|
|
|
--- The root (global) keybinding to start this keygrabber.
|
|
|
|
--
|
|
|
|
-- Instead of adding an entry to `root.keys` or `rc.lua` `globalkeys` section,
|
|
|
|
-- this property can be used to take care of everything. This way, it becomes
|
|
|
|
-- easier to package modules using keygrabbers.
|
|
|
|
--
|
|
|
|
-- @DOC_text_awful_keygrabber_root_keybindings_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property root_keybindings
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam[opt={}] table root_keybindings
|
|
|
|
-- @tablerowtype A list of `awful.key` objects.
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @see export_keybindings
|
|
|
|
-- @see keybindings
|
|
|
|
|
|
|
|
--- The keybindings associated with this keygrabber.
|
|
|
|
--
|
2018-12-29 03:06:32 +01:00
|
|
|
-- This property contains a table of `awful.key` objects.
|
2018-07-21 20:18:44 +02:00
|
|
|
--
|
|
|
|
-- @property keybindings
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam[opt={}] table keybindings
|
|
|
|
-- @tablerowtype A list of `awful.key` objects.
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @see export_keybindings
|
|
|
|
-- @see root_keybindings
|
2018-12-29 03:06:32 +01:00
|
|
|
-- @see awful.key
|
2018-07-21 20:18:44 +02:00
|
|
|
|
|
|
|
--- If any key is pressed that is not in this list, the keygrabber is stopped.
|
|
|
|
--
|
|
|
|
-- When defined, this property allows to define an implicit way to release the
|
|
|
|
-- keygrabber. It helps save some boilerplate code in the handler callbacks.
|
|
|
|
--
|
|
|
|
-- It is useful when a transaction only handle a limited number of keys. If
|
2021-10-05 04:20:03 +02:00
|
|
|
-- a key unhandled by the transaction is triggered, the transaction is
|
2018-07-21 20:18:44 +02:00
|
|
|
-- canceled.
|
|
|
|
--
|
|
|
|
-- @DOC_text_awful_keygrabber_allowed_keys_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property allowed_keys
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam[opt=nil] table|nil allowed_keys
|
|
|
|
-- @propertytype nil All keys are allowed.
|
|
|
|
-- @propertytype table Only some keys are allowed.
|
|
|
|
-- @tablerowtype A list of key names, such as `"Control"` or `"a"`.
|
2018-07-21 20:18:44 +02:00
|
|
|
|
|
|
|
--- The sequence of keys recorded since the start of the keygrabber.
|
|
|
|
--
|
|
|
|
-- In this example, the `stop_callback` is used to retrieve the final key
|
|
|
|
-- sequence.
|
|
|
|
--
|
|
|
|
-- Please note that backspace will remove elements from the sequence and
|
|
|
|
-- named keys and modifiers are ignored.
|
|
|
|
--
|
|
|
|
-- @DOC_text_awful_keygrabber_autostart_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property sequence
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam[opt=""] string sequence
|
2018-07-21 20:18:44 +02:00
|
|
|
--
|
|
|
|
|
|
|
|
--- The current (running) instance of this keygrabber.
|
|
|
|
--
|
|
|
|
-- The keygrabber instance is created when the keygrabber starts. It is an object
|
|
|
|
-- mirroring this keygrabber object, but where extra properties can be added. It
|
|
|
|
-- is useful to keep some contextual data part of the current transaction without
|
2021-10-05 04:20:03 +02:00
|
|
|
-- polluting the original object of having extra boilerplate code.
|
2018-07-21 20:18:44 +02:00
|
|
|
--
|
|
|
|
-- @tfield keygrabber current_instance
|
2019-12-22 06:27:21 +01:00
|
|
|
-- @emits property::current_instance
|
2018-07-21 20:18:44 +02:00
|
|
|
|
|
|
|
--- The global signal used to track the `current_instance`.
|
|
|
|
--
|
|
|
|
-- @signal property::current_instance
|
|
|
|
|
|
|
|
--- If a keygrabber is currently running.
|
|
|
|
-- @tfield boolean is_running
|
|
|
|
|
|
|
|
--- Start the keygrabber.
|
|
|
|
--
|
|
|
|
-- Note that only a single keygrabber can be started at any one time. If another
|
|
|
|
-- keygrabber (or this one) is currently running. This method returns false.
|
|
|
|
--
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method start
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @treturn boolean If the keygrabber was successfully started.
|
2019-12-22 06:27:21 +01:00
|
|
|
-- @emits started
|
|
|
|
-- @emits property::current_instance
|
2018-07-21 20:18:44 +02:00
|
|
|
function keygrabber:start()
|
|
|
|
if self.grabber or keygrab.current_instance then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
self.current_instance = setmetatable({}, {
|
|
|
|
__index = self,
|
|
|
|
__newindex = function(tab, key, value)
|
|
|
|
if keygrabber["set_"..key] then
|
|
|
|
self[key](self, value)
|
|
|
|
else
|
|
|
|
rawset(tab, key, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
|
|
|
self.sequence = ""
|
|
|
|
|
|
|
|
if self.start_callback then
|
|
|
|
self.start_callback(self.current_instance)
|
|
|
|
end
|
|
|
|
|
|
|
|
self.grabber = keygrab.run(function(...) return runner(self, ...) end)
|
|
|
|
|
2021-10-05 04:20:03 +02:00
|
|
|
-- Ease making keygrabber that won't hang forever if no action is taken.
|
2018-07-21 20:18:44 +02:00
|
|
|
if self.timeout and not self._private.timer then
|
|
|
|
self._private.timer = gtimer {
|
|
|
|
timeout = self.timeout,
|
|
|
|
single_shot = true,
|
|
|
|
callback = function()
|
|
|
|
if self.timeout_callback then
|
|
|
|
pcall(self.timeout_callback, self)
|
|
|
|
end
|
|
|
|
self:stop()
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
if self._private.timer then
|
|
|
|
self._private.timer:start()
|
|
|
|
end
|
|
|
|
|
|
|
|
keygrab.current_instance = self
|
|
|
|
|
|
|
|
keygrab.emit_signal("property::current_instance", keygrab.current_instance)
|
|
|
|
|
|
|
|
self:emit_signal("started")
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Stop the keygrabber.
|
2021-05-26 23:17:43 +02:00
|
|
|
--
|
|
|
|
-- Also stops any `timeout`.
|
|
|
|
--
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method stop
|
2022-08-29 02:13:52 +02:00
|
|
|
-- @tparam string|nil stop_key Override the key passed to `stop_callback` **DEPRECATED**
|
2022-09-18 18:22:02 +02:00
|
|
|
-- @tparam tale|nil stop_mods Override the modifiers passed to `stop_callback` **DEPRECATED**
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2019-12-22 06:27:21 +01:00
|
|
|
-- @emits stopped
|
|
|
|
-- @emits property::current_instance
|
2022-08-29 02:13:52 +02:00
|
|
|
function keygrabber:stop(stop_key, stop_mods)
|
|
|
|
if stop_key then
|
|
|
|
gdebug.deprecate(
|
|
|
|
"The `stop_key` is deprecated. Overriding callback parameters "..
|
|
|
|
"is an anti-pattern and might confuse third party modules.",
|
|
|
|
{deprecated_in=5}
|
|
|
|
)
|
2021-05-26 23:17:43 +02:00
|
|
|
end
|
|
|
|
|
2022-08-29 02:13:52 +02:00
|
|
|
if stop_mods then
|
|
|
|
gdebug.deprecate(
|
|
|
|
"The `stop_mods` is deprecated. Overriding callback parameters "..
|
|
|
|
"is an anti-pattern and might confuse third party modules.",
|
|
|
|
{deprecated_in=5}
|
2018-07-21 20:18:44 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-08-29 02:13:52 +02:00
|
|
|
stop(self, stop_key, stop_mods)
|
2018-07-21 20:18:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Add a keybinding to this keygrabber.
|
|
|
|
--
|
|
|
|
-- Those keybindings will automatically start the keygrabbing when hit.
|
|
|
|
--
|
2022-08-29 02:13:52 +02:00
|
|
|
-- Please note that this method previously accepted a
|
|
|
|
-- `mods, keycode, callback, description` signature. This is deprecated. Store
|
|
|
|
-- those values in an `awful.key` prior to passing it to this function. The
|
|
|
|
-- previous method signature made it impossible to alter the description and/or
|
|
|
|
-- enable/disable the keybinding.
|
|
|
|
--
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method add_keybinding
|
2018-12-29 03:06:32 +01:00
|
|
|
-- @tparam awful.key key The key.
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @tparam string description.group The keybinding group
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2022-09-06 00:57:23 +02:00
|
|
|
-- @see remove_keybinding
|
2018-12-29 03:06:32 +01:00
|
|
|
|
2022-08-29 02:13:52 +02:00
|
|
|
function keygrabber:add_keybinding(key, keycode, callback, description)
|
2022-05-29 22:10:29 +02:00
|
|
|
local mods = not key._is_awful_key and key or nil
|
2018-12-29 03:06:32 +01:00
|
|
|
|
|
|
|
if mods then
|
|
|
|
gdebug.deprecate(":add_keybinding now takes `awful.key` objects instead"
|
|
|
|
.. " of multiple parameters",
|
|
|
|
{deprecated_in=5}
|
|
|
|
)
|
|
|
|
|
|
|
|
key = akey {
|
|
|
|
modifiers = mods,
|
2022-08-29 02:13:52 +02:00
|
|
|
key = keycode,
|
|
|
|
description = description,
|
|
|
|
on_press = callback
|
2018-12-29 03:06:32 +01:00
|
|
|
}
|
2022-08-29 02:13:52 +02:00
|
|
|
elseif keycode or callback or description then
|
|
|
|
gdebug.deprecate(
|
|
|
|
":add_keybinding only accept a single parameter. All "..
|
|
|
|
"other were ignored.",
|
|
|
|
{deprecated_in=4}
|
|
|
|
)
|
2018-12-29 03:06:32 +01:00
|
|
|
end
|
|
|
|
|
2022-05-29 22:10:29 +02:00
|
|
|
self._private.keybindings[key.key] = self._private.keybindings[key.key] or {}
|
|
|
|
table.insert(self._private.keybindings[key.key], key)
|
2018-07-21 20:18:44 +02:00
|
|
|
|
|
|
|
if self.export_keybindings then
|
2018-12-29 03:06:32 +01:00
|
|
|
add_root_keybindings(self, {key})
|
2018-07-21 20:18:44 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-06 00:57:23 +02:00
|
|
|
--- Remove a keybinding from the keygrabber.
|
|
|
|
--
|
|
|
|
-- @method remove_keybinding
|
|
|
|
-- @treturn boolean `true` if removed, `false` if not found.
|
|
|
|
-- @see add_keybinding
|
|
|
|
|
|
|
|
function keygrabber:remove_keybinding(key)
|
|
|
|
for idx, obj in ipairs(self._private.keybindings[key.key]) do
|
|
|
|
if obj == key then
|
|
|
|
table.remove(self._private.keybindings[key.key], idx)
|
|
|
|
|
|
|
|
if #self._private.keybindings[key.key] == 0 then
|
|
|
|
self._private.keybindings[key.key] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2018-07-21 20:18:44 +02:00
|
|
|
function keygrabber:set_root_keybindings(keys)
|
2018-12-29 03:06:32 +01:00
|
|
|
local real_keys = {}
|
|
|
|
|
|
|
|
-- Handle the pre-object-oriented input structures.
|
|
|
|
for _, key in ipairs(keys) do
|
|
|
|
if key._is_awful_key then
|
|
|
|
table.insert(real_keys, key)
|
|
|
|
else
|
|
|
|
gdebug.deprecate(":set_root_keybindings now takes `awful.key` "
|
|
|
|
.. " objects instead of tables",
|
|
|
|
{deprecated_in=5}
|
|
|
|
)
|
|
|
|
|
|
|
|
local mods, keycode, press, release, data = unpack(key)
|
|
|
|
|
|
|
|
table.insert(real_keys, akey {
|
|
|
|
modifiers = mods,
|
|
|
|
key = keycode,
|
|
|
|
description = (data or {}).description,
|
|
|
|
on_press = press,
|
|
|
|
on_release = release,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
add_root_keybindings(self, real_keys)
|
2018-07-21 20:18:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Turn into a set.
|
|
|
|
function keygrabber:set_allowed_keys(keys)
|
|
|
|
self._private.allowed_keys = {}
|
|
|
|
|
|
|
|
for _, v in ipairs(keys) do
|
|
|
|
self._private.allowed_keys[v] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-16 14:42:56 +02:00
|
|
|
--TODO v5 remove this
|
|
|
|
function keygrabber:set_release_event(event)
|
|
|
|
-- It has never been in a release, so it can be deprecated right away
|
|
|
|
gdebug.deprecate("release_event has been renamed to stop_event")
|
|
|
|
|
|
|
|
self.stop_event = event
|
|
|
|
end
|
|
|
|
|
|
|
|
--TODO v5 remove this
|
|
|
|
function keygrabber:get_release_event()
|
|
|
|
return self.stop_event
|
|
|
|
end
|
|
|
|
|
2018-07-21 20:18:44 +02:00
|
|
|
--- When the keygrabber starts.
|
|
|
|
-- @signal started
|
|
|
|
|
|
|
|
--- When the keygrabber stops.
|
|
|
|
-- @signal stopped
|
|
|
|
|
2022-09-06 00:50:43 +02:00
|
|
|
--- When an `awful.key` is pressed.
|
|
|
|
-- @signal keybinding::triggered
|
|
|
|
-- @tparam awful.keybinding self
|
|
|
|
-- @tparam awful.key key The keybinding.
|
|
|
|
-- @tparam string event Either `"press"` or `"release"`.
|
|
|
|
|
2018-07-21 20:18:44 +02:00
|
|
|
--- A function called when a keygrabber starts.
|
|
|
|
-- @callback start_callback
|
|
|
|
-- @tparam keygrabber keygrabber The keygrabber.
|
|
|
|
|
|
|
|
--- The callback when the keygrabbing stops.
|
|
|
|
--
|
2018-10-16 14:42:56 +02:00
|
|
|
-- @usage local function my_done_cb(self, stop_key, stop_mods, sequence)
|
2018-07-21 20:18:44 +02:00
|
|
|
-- -- do something
|
|
|
|
-- end
|
|
|
|
-- @tparam table self The current transaction object.
|
|
|
|
-- @tparam string stop_key The key(s) that stop the keygrabbing (if any)
|
2018-10-16 14:42:56 +02:00
|
|
|
-- @tparam table stop_mods The modifiers key (if any)
|
2021-10-05 04:20:03 +02:00
|
|
|
-- @tparam string sequence The recorded key sequence.
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @callback stop_callback
|
|
|
|
-- @see sequence
|
|
|
|
-- @see stop
|
|
|
|
|
|
|
|
--- The function called when the keygrabber stops because of a `timeout`.
|
|
|
|
--
|
|
|
|
-- Note that the `stop_callback` is also called when the keygrabbers timeout.
|
|
|
|
--
|
|
|
|
-- @callback timeout_callback
|
|
|
|
-- @see timeout
|
|
|
|
|
|
|
|
--- The callback function to call with mod table, key and command as arguments
|
|
|
|
-- when a key was pressed.
|
|
|
|
--
|
|
|
|
-- @callback keypressed_callback
|
|
|
|
-- @tparam table self The current transaction object.
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam table mod The current modifiers (like `"Control"` or `"Shift"`).
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @tparam string key The key name.
|
|
|
|
-- @tparam string event The event ("press" or "release").
|
2020-01-28 10:57:43 +01:00
|
|
|
-- @usage local function my_keypressed_cb(self, mod, key, command)
|
2018-07-21 20:18:44 +02:00
|
|
|
-- -- do something
|
|
|
|
-- end
|
|
|
|
|
|
|
|
--- The callback function to call with mod table, key and command as arguments
|
|
|
|
-- when a key was released.
|
2020-01-28 10:57:43 +01:00
|
|
|
-- @usage local function my_keyreleased_cb(self, mod, key, command)
|
2018-07-21 20:18:44 +02:00
|
|
|
-- -- do something
|
|
|
|
-- end
|
|
|
|
-- @callback keyreleased_callback
|
|
|
|
-- @tparam table self The current transaction object.
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam table mod The current modifiers (like `"Control"` or `"Shift"`).
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @tparam string key The key name.
|
|
|
|
-- @tparam string event The event ("press" or "release")
|
|
|
|
|
|
|
|
--- A wrapper around the keygrabber to make it easier to add keybindings.
|
|
|
|
--
|
|
|
|
-- This is similar to the `awful.prompt`, but without an actual widget. It can
|
|
|
|
-- be used to implement custom transactions such as alt+tab or keyboard menu
|
|
|
|
-- navigation.
|
|
|
|
--
|
|
|
|
-- Note that the object returned can be used as a client or global keybinding
|
2018-10-16 14:42:56 +02:00
|
|
|
-- callback without modification. Make sure to set `stop_key` and `stop_event`
|
2018-07-21 20:18:44 +02:00
|
|
|
-- to proper values to avoid permanently locking the keyboard.
|
|
|
|
--
|
|
|
|
-- @tparam table args
|
2018-10-16 14:42:56 +02:00
|
|
|
-- @tparam[opt="press"] string args.stop_event Release event ("press" or "release")
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @tparam[opt=nil] string|table args.stop_key The key that has to be kept pressed.
|
|
|
|
-- @tparam table args.keybindings All keybindings.
|
|
|
|
-- @tparam[opt=-1] number args.timeout The maximum inactivity delay.
|
|
|
|
-- @tparam[opt=true] boolean args.mask_event_callback Do not call `keypressed_callback`
|
|
|
|
-- or `keyreleased_callback` when a hook is called.
|
|
|
|
-- @tparam[opt] function args.start_callback
|
|
|
|
-- @tparam[opt] function args.stop_callback
|
|
|
|
-- @tparam[opt] function args.timeout_callback
|
|
|
|
-- @tparam[opt] function args.keypressed_callback
|
|
|
|
-- @tparam[opt] function args.keyreleased_callback
|
|
|
|
-- @tparam[opt=nil] table|nil args.allowed_keys A table with all keys that
|
2021-10-05 04:20:03 +02:00
|
|
|
-- **won't** stop the keygrabber.
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @tparam[opt] table args.root_keybindings The root (global) keybindings.
|
|
|
|
-- @tparam[opt=false] boolean args.export_keybindings Create root (global) keybindings.
|
|
|
|
-- @tparam[opt=false] boolean args.autostart Start the grabbing immediately
|
|
|
|
-- @tparam[opt=false] boolean args.mask_modkeys Do not call the callbacks on
|
2022-08-22 08:02:26 +02:00
|
|
|
-- modifier keys (like `"Control"` or `"Mod4"`) events.
|
2019-06-07 20:59:34 +02:00
|
|
|
-- @constructorfct awful.keygrabber
|
2018-07-21 20:18:44 +02:00
|
|
|
function keygrab.run_with_keybindings(args)
|
|
|
|
args = args or {}
|
|
|
|
|
|
|
|
local ret = gobject {
|
|
|
|
enable_properties = true,
|
|
|
|
enable_auto_signals = true
|
|
|
|
}
|
|
|
|
|
|
|
|
rawset(ret, "_private", {})
|
|
|
|
|
|
|
|
-- Do not use `rawset` or `_private` because it uses the `gears.object`
|
|
|
|
-- auto signals.
|
|
|
|
ret.sequence = ""
|
|
|
|
|
|
|
|
gtable.crush(ret, keygrabber, true)
|
|
|
|
|
|
|
|
gtable.crush(ret, args)
|
|
|
|
|
|
|
|
ret._private.keybindings = {}
|
2018-10-16 14:42:56 +02:00
|
|
|
ret.stop_event = args.stop_event or "press"
|
2018-07-21 20:18:44 +02:00
|
|
|
|
|
|
|
-- Build the hook map
|
|
|
|
for _,v in ipairs(args.keybindings or {}) do
|
2018-12-29 03:06:32 +01:00
|
|
|
if v._is_awful_key then
|
|
|
|
ret._private.keybindings[v.key] = ret._private.keybindings[v.key] or {}
|
|
|
|
table.insert(ret._private.keybindings[v.key], v)
|
|
|
|
elseif #v >= 3 and #v <= 4 then
|
|
|
|
gdebug.deprecate("keybindings now contains `awful.key` objects"
|
|
|
|
.. "instead of multiple tables",
|
|
|
|
{deprecated_in=5}
|
|
|
|
)
|
|
|
|
|
|
|
|
local modifiers, key, callback = unpack(v)
|
2018-07-21 20:18:44 +02:00
|
|
|
if type(callback) == "function" then
|
2018-12-29 03:06:32 +01:00
|
|
|
|
|
|
|
local k = akey {
|
|
|
|
modifiers = modifiers,
|
|
|
|
key = key,
|
|
|
|
on_press = callback,
|
|
|
|
}
|
|
|
|
|
2018-07-21 20:18:44 +02:00
|
|
|
ret._private.keybindings[key] = ret._private.keybindings[key] or {}
|
2018-12-29 03:06:32 +01:00
|
|
|
table.insert(ret._private.keybindings[key], k)
|
2018-07-21 20:18:44 +02:00
|
|
|
else
|
|
|
|
gdebug.print_warning(
|
|
|
|
"The hook's 3rd parameter has to be a function. " ..
|
2021-09-21 17:34:46 +02:00
|
|
|
gdebug.dump_return(v or {})
|
2018-07-21 20:18:44 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
gdebug.print_warning(
|
2021-09-21 17:34:46 +02:00
|
|
|
"The keybindings should be awful.key objects" ..
|
|
|
|
gdebug.dump_return(v or {})
|
2018-07-21 20:18:44 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.export_keybindings then
|
2018-12-29 03:06:32 +01:00
|
|
|
ret:set_root_keybindings(args.keybindings)
|
2018-07-21 20:18:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local mt = getmetatable(ret)
|
|
|
|
|
|
|
|
-- Add syntax-sugar to call `:start()`.
|
|
|
|
-- This allows keygrabbers object to be used as callbacks "functions".
|
|
|
|
function mt.__call()
|
|
|
|
ret:start()
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.autostart then
|
|
|
|
ret:start()
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
--- A lower level API to interact with the keygrabber directly.
|
|
|
|
--
|
2014-08-28 19:45:46 +02:00
|
|
|
-- Grab keyboard input and read pressed keys, calling the least callback
|
|
|
|
-- function from the stack at each keypress, until the stack is empty.
|
|
|
|
--
|
2012-04-30 17:44:58 +02:00
|
|
|
-- Calling run with the same callback again will bring the callback
|
2014-08-28 19:45:46 +02:00
|
|
|
-- to the top of the stack.
|
|
|
|
--
|
|
|
|
-- The callback function receives three arguments:
|
|
|
|
--
|
2015-02-20 15:41:19 +01:00
|
|
|
-- * a table containing modifiers keys
|
|
|
|
-- * a string with the pressed key
|
2018-07-21 20:18:44 +02:00
|
|
|
-- * a string with either "press" or "release" to indicate the event type.
|
|
|
|
--
|
|
|
|
-- Here is the content of the modifier table:
|
|
|
|
--
|
2019-06-08 01:08:05 +02:00
|
|
|
-- <table class='widget_list' border=1>
|
2018-07-21 20:18:44 +02:00
|
|
|
-- <tr style='font-weight: bold;'>
|
|
|
|
-- <th align='center'>Modifier name </th>
|
|
|
|
-- <th align='center'>Key name</th>
|
|
|
|
-- <th align='center'>Alternate key name</th>
|
|
|
|
-- </tr>
|
|
|
|
-- <tr><td> Mod4</td><td align='center'> Super_L </td><td align='center'> Super_R </td></tr>
|
|
|
|
-- <tr><td> Control </td><td align='center'> Control_L </td><td align='center'> Control_R </td></tr>
|
|
|
|
-- <tr><td> Shift </td><td align='center'> Shift_L </td><td align='center'> Shift_R </td></tr>
|
|
|
|
-- <tr><td> Mod1</td><td align='center'> Alt_L </td><td align='center'> Alt_R </td></tr>
|
|
|
|
-- </table>
|
2015-02-20 15:41:19 +01:00
|
|
|
--
|
|
|
|
-- A callback can return `false` to pass the events to the next
|
2014-08-28 19:45:46 +02:00
|
|
|
-- keygrabber in the stack.
|
2018-07-21 20:18:44 +02:00
|
|
|
--
|
|
|
|
-- @param g The key grabber callback that will get the key events until it
|
|
|
|
-- will be deleted or a new grabber is added.
|
2015-02-20 15:41:19 +01:00
|
|
|
-- @return the given callback `g`.
|
2014-08-28 19:45:46 +02:00
|
|
|
-- @usage
|
|
|
|
-- -- The following function can be bound to a key, and be used to resize a
|
|
|
|
-- -- client using the keyboard.
|
2012-04-30 17:44:58 +02:00
|
|
|
--
|
2014-03-15 02:56:58 +01:00
|
|
|
-- function resize(c)
|
2018-01-24 19:13:08 +01:00
|
|
|
-- local grabber
|
|
|
|
-- grabber = awful.keygrabber.run(function(mod, key, event)
|
2014-03-15 02:56:58 +01:00
|
|
|
-- if event == "release" then return end
|
2012-04-30 17:44:58 +02:00
|
|
|
--
|
2017-03-31 03:37:16 +02:00
|
|
|
-- if key == 'Up' then c:relative_move(0, 0, 0, 5)
|
|
|
|
-- elseif key == 'Down' then c:relative_move(0, 0, 0, -5)
|
|
|
|
-- elseif key == 'Right' then c:relative_move(0, 0, 5, 0)
|
|
|
|
-- elseif key == 'Left' then c:relative_move(0, 0, -5, 0)
|
2014-03-15 02:56:58 +01:00
|
|
|
-- else awful.keygrabber.stop(grabber)
|
|
|
|
-- end
|
|
|
|
-- end)
|
|
|
|
-- end
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @deprecated awful.keygrabber.run
|
2018-07-21 20:18:44 +02:00
|
|
|
function keygrab.run(g)
|
2015-07-11 22:53:57 +02:00
|
|
|
-- Remove the grabber if it is in the stack.
|
2018-07-21 20:18:44 +02:00
|
|
|
keygrab.stop(g)
|
|
|
|
|
2015-07-11 22:53:57 +02:00
|
|
|
-- Record the grabber that has been added most recently.
|
2012-04-30 17:44:58 +02:00
|
|
|
table.insert(grabbers, 1, g)
|
2018-07-21 20:18:44 +02:00
|
|
|
|
2015-07-11 22:53:57 +02:00
|
|
|
-- Start the keygrabber if it is not running already.
|
2012-04-30 17:44:58 +02:00
|
|
|
if not keygrabbing then
|
|
|
|
keygrabbing = true
|
|
|
|
capi.keygrabber.run(grabber)
|
|
|
|
end
|
2018-07-21 20:18:44 +02:00
|
|
|
|
2012-04-30 17:44:58 +02:00
|
|
|
return g
|
|
|
|
end
|
|
|
|
|
2018-07-21 20:18:44 +02:00
|
|
|
-- Implement the signal system for the keygrabber.
|
|
|
|
|
|
|
|
local signals = {}
|
|
|
|
|
|
|
|
--- Connect to a signal for all keygrabbers at once.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct awful.keygrabber.connect_signal
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @tparam string name The signal name.
|
|
|
|
-- @tparam function callback The callback.
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2018-07-21 20:18:44 +02:00
|
|
|
function keygrab.connect_signal(name, callback)
|
|
|
|
signals[name] = signals[name] or {}
|
|
|
|
|
|
|
|
-- Use table.insert instead of signals[name][callback] = true to make
|
|
|
|
-- the execution order stable across `emit_signal`. This avoids some
|
|
|
|
-- heisenbugs.
|
|
|
|
table.insert(signals[name], callback)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Disconnect to a signal for all keygrabbers at once.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct awful.keygrabber.disconnect_signal
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @tparam string name The signal name.
|
|
|
|
-- @tparam function callback The callback.
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2018-07-21 20:18:44 +02:00
|
|
|
function keygrab.disconnect_signal(name, callback)
|
|
|
|
signals[name] = signals[name] or {}
|
|
|
|
|
|
|
|
for k, v in ipairs(signals[name]) do
|
|
|
|
if v == callback then
|
|
|
|
table.remove(signals[name], k)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Emit a signal on the keygrabber module itself.
|
|
|
|
--
|
|
|
|
-- Warning, usually don't use this directly, use
|
|
|
|
-- `my_keygrabber:emit_signal(name, ...)`. This function works on the whole
|
|
|
|
-- keygrabber module, not one of its instance.
|
|
|
|
--
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct awful.keygrabber.emit_signal
|
2018-07-21 20:18:44 +02:00
|
|
|
-- @tparam string name The signal name.
|
|
|
|
-- @param ... Other arguments for the callbacks.
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2018-07-21 20:18:44 +02:00
|
|
|
function keygrab.emit_signal(name, ...)
|
|
|
|
signals[name] = signals[name] or {}
|
|
|
|
|
|
|
|
for _, cb in ipairs(signals[name]) do
|
|
|
|
cb(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function keygrab.get_is_running()
|
|
|
|
return keygrab.current_instance ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
--@DOC_object_COMMON@
|
|
|
|
|
|
|
|
return setmetatable(keygrab, {
|
|
|
|
__call = function(_, args) return keygrab.run_with_keybindings(args) end
|
|
|
|
})
|
2012-06-12 20:13:09 +02:00
|
|
|
|
2012-04-30 17:44:58 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|