2012-04-30 17:44:58 +02:00
|
|
|
---------------------------------------------------------------------------
|
2014-05-20 13:02:39 +02:00
|
|
|
--- Keygrabber Stack
|
|
|
|
--
|
2012-04-30 17:44:58 +02:00
|
|
|
-- @author dodo
|
|
|
|
-- @copyright 2012 dodo
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-20 13:02:39 +02:00
|
|
|
-- @module awful.keygrabber
|
2012-04-30 17:44:58 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local ipairs = ipairs
|
|
|
|
local table = table
|
|
|
|
local capi = {
|
|
|
|
keygrabber = keygrabber }
|
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
local keygrabber = {}
|
2012-04-30 17:44:58 +02:00
|
|
|
|
|
|
|
-- Private data
|
|
|
|
local grabbers = {}
|
|
|
|
local keygrabbing = false
|
|
|
|
|
|
|
|
|
|
|
|
local function grabber(mod, key, event)
|
|
|
|
for i, g in ipairs(grabbers) do
|
|
|
|
-- continue if the grabber explicitly returns false
|
|
|
|
if g(mod, key, event) ~= false then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Stop grabbing the keyboard for the provided callback.
|
2015-01-21 08:33:03 +01:00
|
|
|
-- When no callback is given, the last grabber gets removed (last one added to
|
|
|
|
-- the stack).
|
2012-04-30 17:44:58 +02:00
|
|
|
-- @param g The key grabber that must be removed.
|
2012-06-12 20:13:09 +02:00
|
|
|
function keygrabber.stop(g)
|
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
|
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
|
|
|
|
|
|
|
|
---
|
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
|
|
|
|
-- * a string with either "press" or "release" to indicate the event type
|
|
|
|
--
|
|
|
|
-- A callback can return `false` to pass the events to the next
|
2014-08-28 19:45:46 +02:00
|
|
|
-- keygrabber in the stack.
|
2012-04-30 17:44:58 +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)
|
|
|
|
-- local grabber = awful.keygrabber.run(function(mod, key, event)
|
|
|
|
-- if event == "release" then return end
|
2012-04-30 17:44:58 +02:00
|
|
|
--
|
2014-08-28 19:45:46 +02:00
|
|
|
-- if key == 'Up' then awful.client.moveresize(0, 0, 0, 5, c)
|
|
|
|
-- elseif key == 'Down' then awful.client.moveresize(0, 0, 0, -5, c)
|
2014-03-15 02:56:58 +01:00
|
|
|
-- elseif key == 'Right' then awful.client.moveresize(0, 0, 5, 0, c)
|
|
|
|
-- elseif key == 'Left' then awful.client.moveresize(0, 0, -5, 0, c)
|
|
|
|
-- else awful.keygrabber.stop(grabber)
|
|
|
|
-- end
|
|
|
|
-- end)
|
|
|
|
-- end
|
2012-06-12 20:13:09 +02:00
|
|
|
function keygrabber.run(g)
|
2012-04-30 17:44:58 +02:00
|
|
|
-- Remove the grabber if its in stack
|
2012-06-12 20:13:09 +02:00
|
|
|
keygrabber.stop(g)
|
2012-04-30 17:44:58 +02:00
|
|
|
-- Record the grabber has latest added
|
|
|
|
table.insert(grabbers, 1, g)
|
|
|
|
-- start the keygrabber if its not running already
|
|
|
|
if not keygrabbing then
|
|
|
|
keygrabbing = true
|
|
|
|
capi.keygrabber.run(grabber)
|
|
|
|
end
|
|
|
|
return g
|
|
|
|
end
|
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
return keygrabber
|
|
|
|
|
2012-04-30 17:44:58 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|