2

我是 Lua 和hammerspoon 的新手,我一生都无法弄清楚为什么下面的代码在我的主屏幕上可以正常工作,但是在我的辅助显示器上循环位置不起作用。在辅助显示器上点击 Windows 上的快捷方式会启用第一个位置,但第二次按下没有任何作用。

阅读文档, hs.screen.mainScreen() 启用当前聚焦屏幕,所以它应该工作?

顺便说一句......辅助显示器实际上与主要运行 PBP 的显示器在物理上相同(否则显卡无法处理分辨率)

我只在下面发布了我认为相关的代码:

local rightScreen = hs.screen.primaryScreen(0x600003f98880)
local leftScreen = rightScreen:toWest()

function bindKey(key, fn)
  hs.hotkey.bind({"cmd", "ctrl","alt"}, key, fn)
end

grid = {
  {key="q", units={positions.leftthird, positions.left50, positions.left66}},
  {key="w", units={positions.middlethird}},
  {key="e", units={positions.rightthird}},
  {key="r", units={positions.left50, positions.lower50Left50, positions.upper50Left50, positions.upper50, positions.lower50}}, -- virker IKKE på sekundære skærm
  {key="t", units={positions.right50, positions.lower50Right50, positions.upper50Right50, positions.upper50, positions.lower50}}, -- virker IKKE på sekundære skærm

}

hs.fnutils.each(grid, function(entry)
  bindKey(entry.key, function()
    local units = entry.units
    local screen = hs.screen.mainScreen()
    local window = hs.window.focusedWindow()
    local windowGeo = window:frame()

    local index = 0
    hs.fnutils.find(units, function(unit)
      index = index + 1

      local geo = hs.geometry.new(unit):fromUnitRect(screen:frame()):floor()
     return windowGeo:equals(geo)
    end)
    if index == #units then index = 0 end

    window:moveToUnit(units[index + 1])
  end)
end)
4

1 回答 1

1

我能做的最好的事情是发布我自己的代码,目的与我 3 多年前编写的类似目的,并且从那时起它在内部和外部监视器上都运行良好。如您所见,我使用win:setFrame的是:moveToUnit. 抱歉,我无法帮助您调试问题,但我已经忘记了 Lua 和 Hammerspoon……希望对您有所帮助

function coords ()
  return hs.window.focusedWindow(), hs.window.focusedWindow():frame(),
         hs.window.focusedWindow():screen(), hs.window.focusedWindow():screen():frame()
end


hs.hotkey.bind({"cmd", "alt", "ctrl"}, "left", function()

  local win, wf, scr, sf = coords()

  if wf.x <= sf.x and wf.w <= math.floor(sf.w/3) then
    wf.x = sf.x
    wf.w = math.floor(sf.w/4)
  elseif wf.x <= sf.x and wf.w <= math.floor(sf.w/2) then
    wf.x = sf.x
    wf.w = math.floor(sf.w/3)
  else
    wf.x = sf.x
    wf.w = math.floor(sf.w/2)
  end
  wf.y=sf.y
  wf.h=sf.h
  win:setFrame(wf, 0)
end)


hs.hotkey.bind({"cmd", "alt", "ctrl"}, "right", function()

  local win, wf, scr, sf = coords()

  if wf.x >= math.floor(sf.x + sf.w/3) and wf.w <= math.floor(sf.w/3) then
    wf.w = sf.w/4
    wf.x = math.floor(sf.x + 3 * sf.w/4)
  elseif wf.x >= math.floor(sf.x + sf.w/2) and wf.w <= math.floor(sf.w/2) then
    wf.w = sf.w/3
    wf.x = math.floor(sf.x + 2 * sf.w/3)
  else
    wf.w = sf.w/2
    wf.x = math.floor(sf.x + sf.w/2)
  end
  wf.y=sf.y
  wf.h=sf.h
  win:setFrame(wf, 0)
end)


hs.hotkey.bind({"cmd", "alt", "ctrl"}, "up", function()

  local win, wf, scr, sf = coords()

  win:setFrame(sf, 0)
end)

hs.hotkey.bind({"cmd", "alt", "ctrl"}, "down", function()

  local win, wf, scr, sf = coords()

  if wf.x ~= math.floor(sf.x + sf.w/3) and wf.w ~= math.floor(sf.w/3) then
    wf.w = math.floor(sf.w/3)
    wf.x = math.floor(sf.x + sf.w/3)
  else
    wf.w = sf.w/2
    wf.x = math.floor(sf.x + sf.w/4)
  end
  wf.y=sf.y
  wf.h=sf.h

  win:setFrame(wf, 0)
end)
于 2020-03-18T12:33:45.423 回答