AutoHotKey - 在 Windows 10 中切換到下一個/上一個桌面的自訂快捷方式

AutoHotKey - 在 Windows 10 中切換到下一個/上一個桌面的自訂快捷方式

我編寫了一個小型的AutoHotKey 腳本來在Windows 10 中切換虛擬桌面。 CTRL + 左/右)。

代碼:

#LAlt::^#Right ; switch to next desktop with Windows key + Left Alt key
#LCtrl::^#Left ; switch to next desktop with Windows key + Left CTRL key

此程式碼有效,但前提是您在按鍵之前單擊視窗或當前桌面上的其他任何內容。我嘗試使用“單擊”功能在切換桌面之前模擬單擊(有效),但它將滑鼠移動到我單擊的座標,這顯然不是我想要的。我嘗試過使用 ControlClick,但從未讓它發揮作用。

所以我的問題是:有沒有辦法可以在執行切換桌面的命令之前將焦點設定在目前桌面上?或者,是否有其他方法可以讓我在不模擬預設 Win 10 快捷方式的情況下切換桌面?

謝謝!


編輯1

我注意到的一件事是,如果我單擊任務欄,然後執行LWin+ LAlt/ ,我可以透過按住並在和之間交替來LCtrl平滑地來回切換。LWinLAltLCtrl

當我單擊任務欄然後執行LWin+LAlt時,這就是 KeyHistory 輸出的內容:

VK  SC  Type    Up/Dn   Elapsed Key     Window
----------------------------------------------
5B  15B     d   1.91    LWin            
A4  038 h   d   0.31    LAlt            
A2  01D i   d   0.00    LControl        
A2  01D i   u   0.00    LControl        
A4  038 i   u   0.00    LAlt            
A2  01D i   d   0.05    LControl        
5B  15B i   d   0.02    LWin            
27  14D i   d   0.02    Right           
27  14D i   u   0.00    Right           
A2  01D i   u   0.01    LControl        
5B  15B i   u   0.02    LWin            
A2  01D i   d   0.01    LControl        
5B  15B i   d   0.00    LWin            
A2  01D i   u   0.00    LControl        
A4  038 s   u   0.00    LAlt            
5B  15B     u   0.06    LWin            
A2  01D i   d   0.00    LControl        
A2  01D i   u   0.00    LControl
(This is what I want it to do without clicking the taskbar)

但是當我按下LWin+時LAlt,KeyHistory 顯示按鍵事件在「Right Up」事件之後停止:

VK  SC  Type    Up/Dn   Elapsed Key     Window
----------------------------------------------
5B  15B     d   1.91    LWin            
A4  038 h   d   0.31    LAlt            
A2  01D i   d   0.00    LControl        
A2  01D i   u   0.00    LControl        
A4  038 i   u   0.00    LAlt            
A2  01D i   d   0.05    LControl        
5B  15B i   d   0.02    LWin            
27  14D i   d   0.02    Right           
27  14D i   u   0.00    Right
(There should be more after this)

LControl Up 事件從未被觸發,這似乎把一切都搞砸了。

答案1

有時,當您嘗試傳送修飾鍵(win / ctrl / alt)並且觸發字串也有修飾鍵時,您需要等待觸發鍵被釋放,否則它們會影響您發現的替換字串。

嘗試使用KeyWait來完成此操作。請注意,我們現在使用熱鍵語法與熱字串

#LAlt:: ; switch to next desktop with Windows key + Left Alt key
  KeyWait LAlt
  SendInput #^{Right}
  Return

#LCtrl:: ; switch to previous desktop with Windows key + Left CTRL key
  KeyWait LCtrl
  SendInput #^{Left}
  Return

為了切換桌面,上述內容對我有用。

在其他情況下,有時即使這種方法也不起作用,還有另一種可能的解決方案。而不是像這樣的關鍵等待......

KeyWait LAlt
KeyWait LCtrl

...替換為以下對應的按鍵之一以清除按鍵的狀態:

Send,{LAlt Down}{LAlt Up}
Send,{LCtrl Down}{LCtrl Up}

答案2

除非我在某個地方遺漏了一點,Windows++/箭頭不會ctrl切換桌面嗎?至少對我來說是這樣。

答案3

不知道它是否仍然相關,但Reddit 的“pzone”編寫了一個腳本,該腳本使用帶有全局變數的函數來記住您所在的桌面,以便您可以綁定一個熱鍵在兩個桌面之間切換。只有當您有 2 個桌面時它才有效。

switchedDesktop := false
switchDesktop() 
{
  global switchedDesktop
    if switchedDesktop
    {
        SendEvent ^#{Right}
        switchedDesktop := false
    }
    else
    {
        SendEvent ^#{Left}
        switchedDesktop := true
    }
}

https://www.reddit.com/r/AutoHotkey/comments/3fyudo/automatically_switch_ Between_windows_10_virtual

答案4

例如,我想為下一個桌面設定 Page Down,為上一個桌面設定 Page Up 在我安裝 AutoHotkey 後,我建立一個新的 AutoHotKey 腳本並向其中新增此程式碼並儲存,然後執行腳本

PgDn::^#Right ;Next Desktop
return 

PgUp::^#Left ;Previous Desktop
return 

效果會很好:)

相關內容