lua - Awesome WM (v3.5.5) keygrabber 替代品

标签 lua signals awesome-wm

我从来都不喜欢 Awesome 中默认的窗口切换可能性,所以我想我应该实现考虑到历史的 Alt-Tab 行为(并且做花哨的不透明效果)。

当按下 Alt-Tab 时,整个历史记录在一个表中,并附加到该历史记录的是最小化的窗口(在同一标签内)。生成此表后,我实例化一个捕获 Tab 键事件(切换到表中的下一个客户端)和 Alt 键释放事件(完全中止)的键盘抓取器。

一个标记会跟踪用户是否在按 Alt-tabbing 的过程中,以防止表格被一遍又一遍地生成。

代码(很多,你可能不需要看,但我的经验告诉我,当我不发布所有代码时,人们最终会要求它):

altTabbing = false
altTabIndex = 1
altTabHistory = {}
clientOpacities = {}

function altTabSetOpacities(restore)
   for i,c in pairs(altTabHistory) do
      if not restore and i ~= altTabIndex then
         c.opacity = 0.5
      else
         c.opacity = clientOpacities[i]
      end
   end
end

function myAltTab()
   -- First check if the user is already alttabbing, in which case the history
   -- should NOT be updated. If the user has just pressed alt-tab, generate a new 
   -- history-table

   if not altTabbing then -- generate history-table

      -- Clear Tables
      for i in pairs(altTabHistory) do altTabHistory[i] = nil end
      for i in pairs(clientOpacities) do clientOpacities[i] = nil end

      -- Get focus history for current tag
      local s = mouse.screen;
      local idx = 0
      local c = awful.client.focus.history.get(s, idx)

      while c do
         table.insert(altTabHistory, c)
         table.insert(clientOpacities, c.opacity)

         idx = idx + 1
         c = awful.client.focus.history.get(s, idx)
      end

      -- Minimized clients will not appear in the focus history
      -- Find them by cycling through all clients, and adding them to the list
      -- if not already there.
      -- This will preserve the history AND enable you to focus on minimized clients

      local t = awful.tag.selected(s)
      local all = client.get(s)

      for i = 1, #all do
         local c = all[i]
         local ctags = c:tags();

         -- check if the client is on the current tag
         local isCurrentTag = false
         for j = 1, #ctags do
            if t == ctags[j] then
               isCurrentTag = true
               break
            end
         end

         if isCurrentTag then
            -- check if client is already in the history
            -- if not, add it
            local addToHistory = true
            for k = 1, #altTabHistory do
               if altTabHistory[k] == c then
                  addToHistory = false
                  break
               end
            end

            if addToHistory then
               table.insert(altTabHistory, c)
               table.insert(clientOpacities, c.opacity)
            end
         end
      end

      -- reset current index and flag
      altTabIndex = 1
      altTabbing = true

      -- Now that we have collected all windows, we should run a keygrabber
      -- as long as the user is alt-tabbing:
      keygrabber.run(
         function (mod, key, event)  
            -- Stop alt-tabbing when the alt-key is released
            if key == "Alt_L" and event == "release" then
               altTabbing = false
               altTabSetOpacities(true)
               c = altTabHistory[altTabIndex]
               client.focus = c                  
               c:raise()   
               return false -- stop keygrabber
            end

            -- Move to next client on each Tab-press
            if key == "Tab" and event == "press" then
               myAltTab()
               return true -- keep going
            end

            return true -- keep going
         end
      )

   end -- if not altTabbing

   -- at this point, the user is alt-tabbing, so we should raise
   -- the next client in the history-table
   if #altTabHistory < 2 then return end

   -- Switch to next client
   altTabIndex = altTabIndex + 1
   if altTabIndex > #altTabHistory then
      altTabIndex = 1 -- wrap around
   end

   -- focus on current client
   local c = altTabHistory[altTabIndex]
   c.minimized = false
   c:raise()

   -- make current client stand out
   altTabSetOpacities(false)
end

我知道有很多代码,但最主要的是 keygrabber。由于仍然未知的原因,当我使用这种方法使用 Alt-Tabbing 时,Awesome 有时会崩溃。我想通过将信号连接到 Alt 和 Tab 键来替换 keygrabber,并在用户完成后立即断开它们。但是,出于某种原因,我无法执行此操作。

我像这样实例化一个新的键对象:

local altkey = awful.key({}, "Alt_L")[1]

我通过反复试验发现 awful.key() 实际上返回了一个表,我可以在其中查询第一个元素的 keykeysym 等,因此 [1]。但是,当我尝试将信号连接到该对象时,LUA 解释器会提示并告诉我它是一个 nil 对象。所以我的问题是:我在这里做的事情对吗?是否有可能按照我打算的方式更换 keygrabber?

最佳答案

要在 Awesome 中使用 Alt_L 键,您应该在 rc.lua 文件中引用“Mod1”,为了使其更具可读性,我在配置的开头添加了以下行,以便可以使用 Alt_L。

Alt_L = "Mod1"

关于lua - Awesome WM (v3.5.5) keygrabber 替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24756997/

相关文章:

mysql - 错误参数 #1 到 'ipairs'(预期表,得到 bool 值)

Perl 信号处理程序在 END block 中重置

window-managers - 如何在很棒的窗口管理器版本 >4 中删除标题栏

sqlite - Thunderbird - 来自 .sqlite 库的未读消息数

c++ - 跨脚本文件共享 lua 全局变量?

file - Lua中如何只读取文本文件的最后一行?

ios - iOS Objective C 中的 Lua - Require 的路径

c - 为什么我的 Linux 信号处理程序对信号没有反应?

java - netbeans 如何停止运行?

linux - 编辑 rc.lua 时的 Tabcompletion 和 docview