首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用windowAutoHotkey实现快捷键切换工作空间

使用windowAutoHotkey实现快捷键切换工作空间

作者头像
超级大猪
发布2022-11-29 17:25:17
8990
发布2022-11-29 17:25:17
举报
文章被收录于专栏:大猪的笔记大猪的笔记

在win10和11中可以分配工作空间,但是只能使用默认的ctrl+win+箭头。这需要使用两只手才能操作,不太方便。 可以使用windowAutoHotkey把快捷键改为 alt + 1/2/3/4 来快速切换到特定的窗口。 user_config.ahk

; ==================== 
; === INSTRUCTIONS === 
; ==================== 
; 1. Any lines starting with ; are ignored 
; 2. After changing this config file run script file "desktop_switcher.ahk" 
; 3. Every line is in the format HOTKEY::ACTION 
; === SYMBOLS === 
; ! <- Alt 
; + <- Shift 
; ^ <- Ctrl 
; #   <- Win 
; For more, visit https://autohotkey.com/docs/Hotkeys.htm 
; ==================== 
; === INSTRUCTIONS === 
; ==================== 
; 1. Any lines starting with ; are ignored 
; 2. After changing this config file run script file "desktop_switcher.ahk" 
; 3. Every line is in the format HOTKEY::ACTION 
; === SYMBOLS === 
; ! <- Alt 
; + <- Shift 
; ^ <- Ctrl 
; #   <- Win 
; For more, visit https://autohotkey.com/docs/Hotkeys.htm 
; === EXAMPLES === 
; !n::switchDesktopToRight() <- <Alt> + <N> will switch to the next desktop (to the right of the current one) 
; #!space::switchDesktopToRight()        <- <Win> + <Alt> + <Space> will switch to next desktop 
; CapsLock & n::switchDesktopToRight() <- <CapsLock> + <N> will switch to the next desktop (& is necessary when using non-modifier key such as CapsLock) 
; =========================== 
; === END OF INSTRUCTIONS === 
; =========================== 
CapsLock & 1::switchDesktopByNumber(1) 
CapsLock & 2::switchDesktopByNumber(2) 
CapsLock & 3::switchDesktopByNumber(3) 
CapsLock & 4::switchDesktopByNumber(4) 
CapsLock & 5::switchDesktopByNumber(5) 
CapsLock & 6::switchDesktopByNumber(6) 
CapsLock & 7::switchDesktopByNumber(7) 
CapsLock & 8::switchDesktopByNumber(8) 
CapsLock & 9::switchDesktopByNumber(9) 
CapsLock & Numpad1::switchDesktopByNumber(1) 
CapsLock & Numpad2::switchDesktopByNumber(2) 
CapsLock & Numpad3::switchDesktopByNumber(3) 
CapsLock & Numpad4::switchDesktopByNumber(4) 
CapsLock & Numpad5::switchDesktopByNumber(5) 
CapsLock & Numpad6::switchDesktopByNumber(6) 
CapsLock & Numpad7::switchDesktopByNumber(7) 
CapsLock & Numpad8::switchDesktopByNumber(8) 
CapsLock & Numpad9::switchDesktopByNumber(9) 
CapsLock & n::switchDesktopToRight() 
CapsLock & p::switchDesktopToLeft() 
CapsLock & s::switchDesktopToRight() 
CapsLock & a::switchDesktopToLeft() 
CapsLock & tab::switchDesktopToLastOpened() 
CapsLock & c::createVirtualDesktop() 
CapsLock & d::deleteVirtualDesktop() 
CapsLock & q::MoveCurrentWindowToDesktop(1) 
CapsLock & w::MoveCurrentWindowToDesktop(2) 
CapsLock & e::MoveCurrentWindowToDesktop(3) 
CapsLock & r::MoveCurrentWindowToDesktop(4) 
CapsLock & t::MoveCurrentWindowToDesktop(5) 
CapsLock & y::MoveCurrentWindowToDesktop(6) 
CapsLock & u::MoveCurrentWindowToDesktop(7) 
CapsLock & i::MoveCurrentWindowToDesktop(8) 
CapsLock & o::MoveCurrentWindowToDesktop(9) 
CapsLock & Right::MoveCurrentWindowToRightDesktop() 
CapsLock & Left::MoveCurrentWindowToLeftDesktop() 
; === INSTRUCTIONS === 
; Below is the alternate key configuration. Delete symbol ; in the beginning of the line to enable. 
; Note, that  ^!1  means "Ctrl + Alt + 1" and ^#1  means "Ctrl + Win + 1" 
; === END OF INSTRUCTIONS === 
; ^!1::switchDesktopByNumber(1) 
; ^!2::switchDesktopByNumber(2) 
; ^!3::switchDesktopByNumber(3) 
; ^!4::switchDesktopByNumber(4) 
; ^!5::switchDesktopByNumber(5) 
; ^!6::switchDesktopByNumber(6) 
; ^!7::switchDesktopByNumber(7) 
; ^!8::switchDesktopByNumber(8) 
; ^!9::switchDesktopByNumber(9) 
!1::switchDesktopByNumber(1) 
!2::switchDesktopByNumber(2) 
!3::switchDesktopByNumber(3) 
!4::switchDesktopByNumber(4) 
!5::switchDesktopByNumber(5) 
!6::switchDesktopByNumber(6) 
!7::switchDesktopByNumber(7) 
!8::switchDesktopByNumber(8) 
!9::switchDesktopByNumber(9) 
; ^!Numpad1::switchDesktopByNumber(1) 
; ^!Numpad2::switchDesktopByNumber(2) 
; ^!Numpad3::switchDesktopByNumber(3) 
; ^!Numpad4::switchDesktopByNumber(4) 
; ^!Numpad5::switchDesktopByNumber(5) 
; ^!Numpad6::switchDesktopByNumber(6) 
; ^!Numpad7::switchDesktopByNumber(7) 
; ^!Numpad8::switchDesktopByNumber(8) 
; ^!Numpad9::switchDesktopByNumber(9) 
; ^!n::switchDesktopToRight() 
; ^!p::switchDesktopToLeft() 
; ^!s::switchDesktopToRight() 
; ^!a::switchDesktopToLeft() 
; ^!tab::switchDesktopToLastOpened() 
; ^!c::createVirtualDesktop() 
; ^!d::deleteVirtualDesktop() 
; ^#1::MoveCurrentWindowToDesktop(1) 
; ^#2::MoveCurrentWindowToDesktop(2) 
; ^#3::MoveCurrentWindowToDesktop(3) 
; ^#4::MoveCurrentWindowToDesktop(4) 
; ^#5::MoveCurrentWindowToDesktop(5) 
; ^#6::MoveCurrentWindowToDesktop(6) 
; ^#7::MoveCurrentWindowToDesktop(7) 
; ^#8::MoveCurrentWindowToDesktop(8) 
; ^#9::MoveCurrentWindowToDesktop(9) 
; ^#Numpad1::MoveCurrentWindowToDesktop(1) 
; ^#Numpad2::MoveCurrentWindowToDesktop(2) 
; ^#Numpad3::MoveCurrentWindowToDesktop(3) 
; ^#Numpad4::MoveCurrentWindowToDesktop(4) 
; ^#Numpad5::MoveCurrentWindowToDesktop(5) 
; ^#Numpad6::MoveCurrentWindowToDesktop(6) 
; ^#Numpad7::MoveCurrentWindowToDesktop(7) 
; ^#Numpad8::MoveCurrentWindowToDesktop(8) 
; ^#Numpad9::MoveCurrentWindowToDesktop(9) 
; ^#Right::MoveCurrentWindowToRightDesktop() 
; ^#Left::MoveCurrentWindowToLeftDesktop() 
; === INSTRUCTIONS === 
; Additional alternative shortcut for moving current window to left or right desktop (ctrl+shift+Win+left/right) 
; === END OF INSTRUCTIONS === 
; ^#+Right::MoveCurrentWindowToRightDesktop() 
; ^#+Left::MoveCurrentWindowToLeftDesktop() 

window_switch.ahk

#SingleInstance Force ; The script will Reload if launched while already running 
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases 
#KeyHistory 0 ; Ensures user privacy when debugging is not needed 
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory 
SendMode Input ; Recommended for new scripts due to its superior speed and reliability 
; Globals 
DesktopCount := 2 ; Windows starts with 2 desktops at boot 
CurrentDesktop := 1 ; Desktop count is 1-indexed (Microsoft numbers them this way) 
LastOpenedDesktop := 1 
; DLL 
hVirtualDesktopAccessor := DllCall("LoadLibrary", "Str", A_ScriptDir . "\VirtualDesktopAccessor.dll", "Ptr") 
global IsWindowOnDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "IsWindowOnDesktopNumber", "Ptr") 
global MoveWindowToDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "MoveWindowToDesktopNumber", "Ptr") 
; Main 
SetKeyDelay, 75 
mapDesktopsFromRegistry() 
OutputDebug, [loading] desktops: %DesktopCount% current: %CurrentDesktop% 
#Include %A_ScriptDir%\user_config.ahk 
return 
; 
; This function examines the registry to build an accurate list of the current virtual desktops and which one we're currently on. 
; List of desktops appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops 
; On Windows 11 the current desktop UUID appears to be in the same location 
; On previous versions in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\1\VirtualDesktops 
; 
mapDesktopsFromRegistry() 
{ 
    global CurrentDesktop, DesktopCount 
    ; Get the current desktop UUID. Length should be 32 always, but there's no guarantee this couldn't change in a later Windows release so we check. 
    IdLength := 32 
    SessionId := getSessionId() 
    if (SessionId) { 
        RegRead, CurrentDesktopId, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, CurrentVirtualDesktop 
        if ErrorLevel { 
            RegRead, CurrentDesktopId, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\%SessionId%\VirtualDesktops, CurrentVirtualDesktop 
        } 
        if (CurrentDesktopId) { 
            IdLength := StrLen(CurrentDesktopId) 
        } 
    } 
    ; Get a list of the UUIDs for all virtual desktops on the system 
    RegRead, DesktopList, HKEY_CURRENT_USER, SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, VirtualDesktopIDs 
    if (DesktopList) { 
        DesktopListLength := StrLen(DesktopList) 
        ; Figure out how many virtual desktops there are 
        DesktopCount := floor(DesktopListLength / IdLength) 
    } 
    else { 
        DesktopCount := 1 
    } 
    ; Parse the REG_DATA string that stores the array of UUID's for virtual desktops in the registry. 
    i := 0 
 while (CurrentDesktopId and i < DesktopCount) { 
 StartPos := (i * IdLength) + 1 
 DesktopIter := SubStr(DesktopList, StartPos, IdLength) 
 OutputDebug, The iterator is pointing at %DesktopIter% and count is %i%. 
 ; Break out if we find a match in the list. If we didn't find anything, keep the 
        ; old guess and pray we're still correct :-D. 
 if (DesktopIter = CurrentDesktopId) { 
 CurrentDesktop := i + 1 
 OutputDebug, Current desktop number is %CurrentDesktop% with an ID of %DesktopIter%. 
 break 
 } 
        i++ 
 } 
} 
; 
; This functions finds out ID of current session. 
; 
getSessionId() 
{ 
 ProcessId := DllCall("GetCurrentProcessId", "UInt") 
 if ErrorLevel { 
 OutputDebug, Error getting current process id: %ErrorLevel% 
 return 
 } 
 OutputDebug, Current Process Id: %ProcessId% 
 DllCall("ProcessIdToSessionId", "UInt", ProcessId, "UInt*", SessionId) 
 if ErrorLevel { 
 OutputDebug, Error getting session id: %ErrorLevel% 
 return 
 } 
 OutputDebug, Current Session Id: %SessionId% 
 return SessionId 
} 
_switchDesktopToTarget(targetDesktop) 
{ 
 ; Globals variables should have been updated via updateGlobalVariables() prior to entering this function 
 global CurrentDesktop, DesktopCount, LastOpenedDesktop 
 ; Don't attempt to switch to an invalid desktop 
    if (targetDesktop > DesktopCount || targetDesktop < 1 || targetDesktop == CurrentDesktop) { 
        OutputDebug, [invalid] target: %targetDesktop% current: %CurrentDesktop% 
        return 
    } 
    LastOpenedDesktop := CurrentDesktop 
    ; Fixes the issue of active windows in intermediate desktops capturing the switch shortcut and therefore delaying or stopping the switching sequence. This also fixes the flashing window button after switching in the taskbar. More info: https://github.com/pmb6tz/windows-desktop-switcher/pull/19 
    WinActivate, ahk_class Shell_TrayWnd 
    ; Go right until we reach the desktop we want 
    while(CurrentDesktop < targetDesktop) { 
        Send {LWin down}{LCtrl down}{Right down}{LWin up}{LCtrl up}{Right up} 
        CurrentDesktop++ 
        OutputDebug, [right] target: %targetDesktop% current: %CurrentDesktop% 
    } 
    ; Go left until we reach the desktop we want 
    while(CurrentDesktop > targetDesktop) { 
        Send {LWin down}{LCtrl down}{Left down}{Lwin up}{LCtrl up}{Left up} 
        CurrentDesktop-- 
        OutputDebug, [left] target: %targetDesktop% current: %CurrentDesktop% 
    } 
    ; Makes the WinActivate fix less intrusive 
    Sleep, 50 
    focusTheForemostWindow(targetDesktop) 
} 
updateGlobalVariables() 
{ 
    ; Re-generate the list of desktops and where we fit in that. We do this because 
    ; the user may have switched desktops via some other means than the script. 
    mapDesktopsFromRegistry() 
} 
switchDesktopByNumber(targetDesktop) 
{ 
    global CurrentDesktop, DesktopCount 
    updateGlobalVariables() 
    _switchDesktopToTarget(targetDesktop) 
} 
switchDesktopToLastOpened() 
{ 
    global CurrentDesktop, DesktopCount, LastOpenedDesktop 
    updateGlobalVariables() 
    _switchDesktopToTarget(LastOpenedDesktop) 
} 
switchDesktopToRight() 
{ 
    global CurrentDesktop, DesktopCount 
    updateGlobalVariables() 
    _switchDesktopToTarget(CurrentDesktop == DesktopCount ? 1 : CurrentDesktop + 1) 
} 
switchDesktopToLeft() 
{ 
    global CurrentDesktop, DesktopCount 
    updateGlobalVariables() 
    _switchDesktopToTarget(CurrentDesktop == 1 ? DesktopCount : CurrentDesktop - 1) 
} 
focusTheForemostWindow(targetDesktop) { 
    foremostWindowId := getForemostWindowIdOnDesktop(targetDesktop) 
    if isWindowNonMinimized(foremostWindowId) { 
        WinActivate, ahk_id %foremostWindowId% 
        Send !{Esc} 
    } 
} 
isWindowNonMinimized(windowId) { 
    WinGet MMX, MinMax, ahk_id %windowId% 
    return MMX != -1 
} 
getForemostWindowIdOnDesktop(n) 
{ 
    n := n - 1 ; Desktops start at 0, while in script it's 1 
 ; winIDList contains a list of windows IDs ordered from the top to the bottom for each desktop. 
 WinGet winIDList, list 
 Loop % winIDList { 
        windowID := % winIDList%A_Index% 
        windowIsOnDesktop := DllCall(IsWindowOnDesktopNumberProc, UInt, windowID, UInt, n) 
 ; Select the first (and foremost) window which is in the specified desktop. 
 if (windowIsOnDesktop == 1) { 
 return windowID 
 } 
 } 
} 
MoveCurrentWindowToDesktop(desktopNumber) { 
 WinGet, activeHwnd, ID, A 
 DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, desktopNumber - 1) 
    switchDesktopByNumber(desktopNumber) 
} 
MoveCurrentWindowToRightDesktop() 
{ 
 global CurrentDesktop, DesktopCount 
    updateGlobalVariables() 
 WinGet, activeHwnd, ID, A 
 DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, (CurrentDesktop == DesktopCount ? 1 : CurrentDesktop + 1) - 1) 
    _switchDesktopToTarget(CurrentDesktop == DesktopCount ? 1 : CurrentDesktop + 1) 
} 
MoveCurrentWindowToLeftDesktop() 
{ 
 global CurrentDesktop, DesktopCount 
    updateGlobalVariables() 
 WinGet, activeHwnd, ID, A 
 DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, (CurrentDesktop == 1 ? DesktopCount : CurrentDesktop - 1) - 1) 
    _switchDesktopToTarget(CurrentDesktop == 1 ? DesktopCount : CurrentDesktop - 1) 
} 
; 
; This function creates a new virtual desktop and switches to it 
; 
createVirtualDesktop() 
{ 
 global CurrentDesktop, DesktopCount 
 Send, #^d 
 DesktopCount++ 
 CurrentDesktop := DesktopCount 
 OutputDebug, [create] desktops: %DesktopCount% current: %CurrentDesktop% 
} 
; 
; This function deletes the current virtual desktop 
; 
deleteVirtualDesktop() 
{ 
 global CurrentDesktop, DesktopCount, LastOpenedDesktop 
 Send, #^{F4} 
 if (LastOpenedDesktop >= CurrentDesktop) { 
 LastOpenedDesktop-- 
 } 
 DesktopCount-- 
 CurrentDesktop-- 
 OutputDebug, [delete] desktops: %DesktopCount% current: %CurrentDesktop% 
} 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-06-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档