AHK 快餐店[11] 之 虚拟桌面 AutoHotkey 版

来源:互联网 发布:vb 二维数组 编辑:程序博客网 时间:2024/06/11 16:27

上回我们把键盘折腾个半死,今天我们要折腾的是:桌面。

虚拟桌面有什么用捏?如果你想在上班的时候看小众、煎蛋、玩游戏……又怕 boss 走过来的时候,手忙脚乱地关闭一堆窗口,那么虚拟桌面是必备的啦。

小众介绍过三款虚拟桌面:Yod’m 3D – 3D 虚拟桌面、VirtuaWin – 很酷的虚拟桌面[更新]、VirtuaWin – 很酷的虚拟桌面[更新]。

废话少说。下载虚拟桌面 AHK 版 | 来源。

[c-sharp] view plaincopy
  1. ;虚拟桌面 - Ahk  
  2. ;Autohotkey的脚本,实现虚拟桌面的功能,快捷键Alt+1,2,3,4  
  3. SetBatchLines, -1 ; maximize script speed!  
  4. SetWinDelay, -1  
  5. OnExit, CleanUp ; clean up in case of error (otherwise some windows will get lost)  
  6. numDesktops := 4 ; maximum number of desktops  
  7. curDesktop := 1 ; index number of current desktop  
  8. WinGet, windows1, List ; get list of all currently visible windows  
  9. ; ***** hotkeys *****  
  10. !1::SwitchToDesktop(1)  
  11. !2::SwitchToDesktop(2)  
  12. !3::SwitchToDesktop(3)  
  13. !4::SwitchToDesktop(4)  
  14. ^!1::SendActiveToDesktop(1)  
  15. ^!2::SendActiveToDesktop(2)  
  16. ^!3::SendActiveToDesktop(3)  
  17. ^!4::SendActiveToDesktop(4)  
  18. !0::ExitApp  
  19. ; ***** functions *****  
  20. switch to the desktop with the given index number  
  21. SwitchToDesktop(newDesktop)  
  22. {  
  23. global  
  24. if (curDesktop <> newDesktop)  
  25. {  
  26. GetCurrentWindows(curDesktop)  
  27. ;WinGet, windows%curDesktop%, List,,, Program Manager ; get list of all visible windows  
  28. ShowHideWindows(curDesktop, false)  
  29. ShowHideWindows(newDesktop, true)  
  30. curDesktop := newDesktop  
  31. Send, {ALT DOWN}{TAB}{ALT UP} ; activate the right window  
  32. }  
  33. return  
  34. }  
  35. ; sends the given window from the current desktop to the given desktop  
  36. SendToDesktop(windowID, newDesktop)  
  37. {  
  38. global  
  39. RemoveWindowID(curDesktop, windowID)  
  40. ; add window to destination desktop  
  41. windows%newDesktop% += 1  
  42. i := windows%newDesktop%  
  43. windows%newDesktop%%i% := windowID  
  44. WinHide, ahk_id %windowID%  
  45. Send, {ALT DOWN}{TAB}{ALT UP} ; activate the right window  
  46. }  
  47. ; sends the currently active window to the given desktop  
  48. SendActiveToDesktop(newDesktop)  
  49. {  
  50. WinGet, id, ID, A  
  51. SendToDesktop(id, newDesktop)  
  52. }  
  53. ; removes the given window id from the desktop  
  54. RemoveWindowID(desktopIdx, ID)  
  55. {  
  56. global  
  57. Loop, % windows%desktopIdx%  
  58. {  
  59. if (windows%desktopIdx%%A_Index% = ID)  
  60. {  
  61. RemoveWindowID_byIndex(desktopIdx, A_Index)  
  62. Break  
  63. }  
  64. }  
  65. }  
  66. this removes the window id at index from desktop number  
  67. RemoveWindowID_byIndex(desktopIdx, ID_idx)  
  68. {  
  69. global  
  70. Loop, % windows%desktopIdx% - ID_idx  
  71. {  
  72. idx1 := % A_Index + ID_idx - 1  
  73. idx2 := % A_Index + ID_idx  
  74. windows%desktopIdx%%idx1% := windows%desktopIdx%%idx2%  
  75. }  
  76. windows%desktopIdx% -= 1  
  77. }  
  78. this builds a list of all currently visible windows in stores it in desktop  
  79. GetCurrentWindows(index)  
  80. {  
  81. global  
  82. WinGet, windows%index%, List,,, Program Manager ; get list of all visible windows  
  83. ; now remove task bar “window” (is there a simpler way?)  
  84. Loop, % windows%index%  
  85. {  
  86. id := % windows%index%%A_Index%  
  87. WinGetClass, windowClass, ahk_id %id%  
  88. if windowClass = Shell_TrayWnd ; remove task bar window id  
  89. {  
  90. RemoveWindowID_byIndex(index, A_Index)  
  91. Break  
  92. }  
  93. }  
  94. }  
  95. if show=true then shows windows of desktop %index%, otherwise hides them  
  96. ShowHideWindows(index, show)  
  97. {  
  98. global  
  99. Loop, % windows%index%  
  100. {  
  101. id := % windows%index%%A_Index%  
  102. if show  
  103. WinShow, ahk_id %id%  
  104. else  
  105. WinHide, ahk_id %id%  
  106. }  
  107. }  
  108. ; show all windows from all desktops on exit  
  109. CleanUp:  
  110. Loop, %numDesktops%  
  111. ShowHideWindows(A_Index, true)  
  112. ExitApp  

运行,看看下面代码你就知道怎么用了:

!1::SwitchToDesktop(1)
!2::SwitchToDesktop(2)
!3::SwitchToDesktop(3)
!4::SwitchToDesktop(4)

^!1::SendActiveToDesktop(1)
^!2::SendActiveToDesktop(2)
^!3::SendActiveToDesktop(3)
^!4::SendActiveToDesktop(4)

再啰嗦一下使用方法:Alt + 1 切换到第一个桌面。其他类推。Ctrl + Alt + 1 把当前窗口发送到 第一个桌面。另外,如果虚拟桌面不够用的话,再添加:

!5::SwitchToDesktop(5)

如果你的键盘按键够用的话,是可以支持无限多个虚拟桌面的。

0 0