【问题标题】:Changing contents of global variables in a Lua script for Awesome Window Manager?更改真棒窗口管理器的 Lua 脚本中全局变量的内容?
【发布时间】:2018-07-06 05:23:14
【问题描述】:

所以我一直在尝试配置我的 Awesome WM 配置 (rc.lua) 以检测我的 IBM 型号 M13 是否在登录/重置时连接到我的笔记本电脑。这是为了更改 modkey 应该是什么,因为 M13 没有超级密钥。

以下代码对我来说很有意义,并在为 awesome.spawn.easy_async 函数创建的函数中更改了 modkey,但在完成后 modkey 会更改回 Mod4。

modkey = "Mod4"

awful.spawn.easy_async(
   "xinput list",
   function(stdout, stderr, reason, code)
      local msg = "Regular keyboard Modkey = Super"

      -- Debug notification that shows that the modkey is 
      -- at its default for the superkey Mod4
      naughty.notify({
         text = modkey,
         timeout =7
      })

      if code ~= 0 then
         msg = "Missing xinput to see devices\nModkey = Super"
      elseif stdout:match("CHESEN") == "CHESEN" then 
         -- CHESEN is my PS/2 to USB adapter
         msg = "IBM M13 detected\nModkey = Alt"
         modkey = "Mod1"  -- Sets new modkey to Alt
      end

      -- Notification message
      naughty.notify({
         text = msg,
         timeout =7
      })
   end
)
-- Debug notification to verify key but key goes back to Mod4
naughty.notify({
   text = modkey,
   timeout =7
})

输出可以在这里看到。它不会按顺序打印通知,但 Mod 4 的打印都是调试打印。

Notification Output

除了不时更改我的配置之外,我不怎么使用 Lua,所以我很难理解如何在不重置的情况下更改我的全局变量 modkey。我尝试的其他方法是将函数定义为我调用 setModKey 作为参数传递给 easy_async 的函数,我尝试使用 _G 设置 modkey 将其设置为 _G.modkey,但我最终得到相同的结果。

我是否遗漏了 Lua 的基本内容,或者这是否受到 Awesome WM 如何利用 Lua 的影响?任何帮助将不胜感激。

【问题讨论】:

  • 为什么这是awful.spawn.easy_async?如果您希望每次重置都发生这种情况,只需将函数的内容放在modkey = "Mod4" 之后。

标签: lua awesome-wm


【解决方案1】:

使用io.popen 代替awful.spawn.easy_async。是的,通常不推荐使用io.popen,但这里会发生以下情况:

  • 精彩开始
  • 你调用easy_async来捕获xinput list的输出
  • 由于它是异步的,因此您的配置会继续加载,例如你所有的键绑定都设置好了
  • easy_async 完成了它的工作,而您将 modkey 设置为其他值。

这意味着从现在开始定义的任何键绑定都使用新的 modkey,但所有已经存在的键绑定都不会被此修改。所以,基本上什么都没发生。

对于您对naughty.notify 的调试调用:首先触发函数之后的那个,然后才触发内部的那个。所以它不会返回,而是首先显示旧值,然后再显示新值。

【讨论】:

  • 非常感谢。我应该考虑查看 Lua 的参考资料,但我主要只是查看 Awesome 的 API 文档。感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 2017-03-05
  • 2013-07-08
  • 2021-10-19
  • 2022-10-13
  • 2014-01-31
  • 2013-12-28
相关资源
最近更新 更多