[Windows 10] Switch to different virtual desktop on Win+{1,9}

Post your working scripts, libraries and tools
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

[Windows 10] Switch to different virtual desktop on Win+{1,9}

12 Mar 2016, 08:30

EDIT 2: Updated for the AU. Thanks to Grabacr07 for the new IID and to wrecklass for testing.

EDIT: FIxed the script not working when explorer died (yes, I did it the lazy way, so, erm, be aware of that if using this in another script - recreating ImmersiveShell is the proper way, I imagine)

Hi,

Written at the request of a user, this will switch Windows 10's virtual desktops instantly when the Win key and a number from 1 to 9 is hit.

Credits:
  • Based off the C code from https://github.com/nullpo-head/Windows- ... g-Shortcut, with additional credits to NickoTin for actually finding the COM interface
  • HotKeyIt for the quick way to map multiple Win+{1,9} shortcuts: //autohotkey.com/board/topic/41780-key-range-mapping/?p=260688

Code: Select all

#NoTrayIcon
#Persistent
; #SingleInstance force
; #KeyHistory 0
; #InstallKeybdHook
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SetBatchLines -1
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

main(), return

RunHotkey:
	switchToDesktop(StrReplace(A_ThisHotkey, "#", , , 1) - 1)
return

switchToDesktop(idx)
{
	global ppDesktopManager, IID_IVirtualDesktop

	DllCall(vtable(ppDesktopManager, 7), "Ptr", ppDesktopManager, "Ptr*", pDesktops)
	if (pDesktops) {
		DllCall(vtable(pDesktops, 4), "Ptr", pDesktops, "UInt", idx, "Ptr", &IID_IVirtualDesktop, "Ptr*", VirtualDesktop)
		if (VirtualDesktop) {
			DllCall(vtable(ppDesktopManager, 9), "Ptr", ppDesktopManager, "Ptr", VirtualDesktop)
			ObjRelease(VirtualDesktop) ; I assume these should be freed
		}
		ObjRelease(pDesktops)
	}
}

main()
{
	OnExit, cleanup

	OnMessage(DllCall("RegisterWindowMessage", Str, "TaskbarCreated"), "WM_TASKBARCREATED")

	static ImmersiveShell := ComObjCreate("{C2F03A33-21F5-47FA-B4BB-156362A2F239}", "{00000000-0000-0000-C000-000000000046}")
	global IID_IVirtualDesktop, ppDesktopManager
	
	try ppDesktopManager := ComObjQuery(ImmersiveShell, "{C5E0CDCA-7B6E-41B2-9FC4-D93975CC467B}", "{f31574d6-b682-4cdc-bd56-1827860abec6}")
	if (!ppDesktopManager)
		ppDesktopManager := ComObjQuery(ImmersiveShell, "{C5E0CDCA-7B6E-41B2-9FC4-D93975CC467B}", "{AF8DA486-95BB-4460-B3B7-6E7A6B2962B5}")

	GUID(IID_IVirtualDesktop, "{FF72FFDD-BE7E-43FC-9C03-AD81681E88E4}")
	ObjRelease(ImmersiveShell)

	Loop 9
		Hotkey, #%A_Index%, RunHotkey
	return

cleanup:
	if (ppDesktopManager)
		ObjRelease(ppDesktopManager)
	ExitApp
}

WM_TASKBARCREATED()
{
    Reload
}

vtable(ptr, n) {
    ; NumGet(ptr+0) returns the address of the object's virtual function
    ; table (vtable for short). The remainder of the expression retrieves
    ; the address of the nth function's address from the vtable.
    return NumGet(NumGet(ptr+0), n*A_PtrSize)
}

GUID(ByRef GUID, sGUID) ; Converts a string to a binary GUID
{
    VarSetCapacity(GUID, 16, 0)
    DllCall("ole32\CLSIDFromString", "Str", sGUID, "Ptr", &GUID)
}

Last edited by qwerty12 on 07 Aug 2016, 15:04, edited 2 times in total.
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

12 Mar 2016, 10:33

Can I somehow change the hotkey to #^? That is already the hotkey for managing the virtual desktops... Like #^D to make a new one...
try it and see
...
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

12 Mar 2016, 11:27

derz00 wrote:Can I somehow change the hotkey to #^? That is already the hotkey for managing the virtual desktops... Like #^D to make a new one...
Sure:

Code: Select all

--- VirtualDesktopSwitchHotkey.ahk	2016-03-12 13:25:03 +0000
+++ "VirtualDesktopSwitchHotkey - Copy.ahk"	2016-03-12 16:17:13 +0000
@@ -12,7 +12,7 @@
 main(), return
 
 RunHotkey:
-	switchToDesktop(StrReplace(A_ThisHotkey, "#", , , 1) - 1)
+	switchToDesktop(StrReplace(A_ThisHotkey, "#^", , , 2) - 1)
 return
 
 switchToDesktop(idx)
@@ -41,7 +41,7 @@
 	ObjRelease(ImmersiveShell)
 
 	Loop 9
-		Hotkey, #%A_Index%, RunHotkey
+		Hotkey, #^%A_Index%, RunHotkey
 	return
 
 cleanup:
Admittedly, Win+<Num> isn't a great shortcut since it interferes with starting pinned applications from the taskbar...
Like #^D to make a new one...
Add the following in, I just did it after "main(), return":

Code: Select all

#^d::
{
	DllCall(vtable(ppDesktopManager, 10), "Ptr", ppDesktopManager, "Ptr*", ppNewDesktop)
	if (ppNewDesktop) {
		DllCall(vtable(ppDesktopManager, 9), "Ptr", ppDesktopManager, "Ptr", ppNewDesktop) ; switch to newly-created desktop
		ObjRelease(ppNewDesktop) ; Again, I assume this is to be freed
	}
}
return
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

12 Mar 2016, 12:07

Sorry, I mean that #^D is already built in... :)
try it and see
...
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

12 Mar 2016, 12:18

Oops, the more you know... (I don't actually use virtual desktops - something I got used to not having after years of Windows...) :)
rockbyo5
Posts: 4
Joined: 19 Jan 2016, 10:46

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

01 Apr 2016, 09:32

Hey, @qwerty12!

i would like it alot if you could create an hotkey to move the currently active window to another virtual desktop.

oh, and also, this script is purely awesome, i simply cant imagine how complex it might be to do, considering my actual autohotkey level. nice job man :)
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

01 Apr 2016, 18:40

rockbyo5 wrote:Hey, @qwerty12!

i would like it alot if you could create an hotkey to move the currently active window to another virtual desktop.

oh, and also, this script is purely awesome, i simply cant imagine how complex it might be to do, considering my actual autohotkey level. nice job man :)
A dirtier method (sending keystrokes to achieve it) is available here: https://autohotkey.com/boards/viewtopic.php?f=6&t=9224

However, using some of the code posted here, it should be possible to implement it more cleanly using the MoveWindowToDesktop method of the IVirtualDesktopManager: https://msdn.microsoft.com/en-us/librar ... s.85).aspx (although I saw a reference elsewhere suggesting this may not work on windows from a different process i.e. an application can only call this on it's OWN windows - a workaround may be possible, not sure)
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

01 Apr 2016, 19:27

Hey,
rockbyo5 wrote: i would like it alot if you could create an hotkey to move the currently active window to another virtual desktop.
SifJar's said it all. The only workaround I can think of would be to use HotKeyIt's AhkDll to run AutoHotkey commands in the context of the target program. Perhaps consider installing https://github.com/Eun/MoveToDesktop? It does provide a hotkey to move the window quickly.
oh, and also, this script is purely awesome, i simply cant imagine how complex it might be to do, considering my actual autohotkey level. nice job man :)
Thanks, but most of the work was done by others. I'm here to learn too - indeed, I've been lucky enough to have my COM questions answered here :-)
rockbyo5
Posts: 4
Joined: 19 Jan 2016, 10:46

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

26 Apr 2016, 10:00

thanks to you both for your replies, i'll look it up when i can.
have a nice day ;)
wrecklass
Posts: 6
Joined: 20 Nov 2015, 16:40

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

11 May 2016, 00:51

Thanks for the script. I had been using another script that used ahk to send key presses, but it was slow and often had errors.

I incorporated your script along with portions of another script you referenced to move the active window to a different desktop. Together these make Windows new Task View virtual desktops a lot more useful.
trololosha
Posts: 2
Joined: 10 Jun 2016, 08:28

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

10 Jun 2016, 08:33

Hello!

Thanks for the script.
I'm quite new in autohotkey scripting and can't understand how to change shortcuts a bit. I'm want to map <#a to switch to desktop 5, <#w to desktop 6 and so on.
Can you please help me with that? :)
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

10 Jun 2016, 09:06

Hi,

wrecklass: Glad it works for you!

trololosha: If a and w follow sequentially on a keyboard layout, I have no idea which one it would be, so I'm just going to directly map the keys instead of trying to be all fancy (and come to think about it, I only do what I do currently because I deal with numbers and not letters). Download the script again from the first post (I fixed a bug) and make the following changes:

* Remove the

Code: Select all

RunHotkey:
	switchToDesktop(StrReplace(A_ThisHotkey, "#", , , 1) - 1)
return
section 'cause it's not needed once we do step 2

* Remove

Code: Select all

	Loop 9
		Hotkey, #%A_Index%, RunHotkey
to stop the Win+[1-9] keys getting hijacked

* Put the following right at the end of the script:

Code: Select all

; Desktops start from 0 in code
#a::switchToDesktop(4)
#w::switchToDesktop(5)
trololosha
Posts: 2
Joined: 10 Jun 2016, 08:28

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

10 Jun 2016, 09:21

It's quite strange. I tried to use switchToDesktop function without RunHotKey command, and it didn't work. But I didn't imagine that RunHotKey command can affect the whole script even I didn't use it.
After removing RunHotKey command from script - everything is started to work. Thank you. :)
wrecklass
Posts: 6
Joined: 20 Nov 2015, 16:40

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

03 Aug 2016, 15:26

It appears the script no longer works with the Anniversary update to WIndows 10 this week. I'm guessing that MS has changed the API calls in some way. Hopefully this can be fixed.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

05 Aug 2016, 17:44

See if replacing '{AF8DA486-95BB-4460-B3B7-6E7A6B2962B5}' with '{00000000-0000-0000-C000-000000000046}' works. If not, try '{f31574d6-b682-4cdc-bd56-1827860abec6}' (credits to Grabacr07). I can't test it since I need to install the Anniversary Update fresh.

EDIT: Thank you, wrecklass, for testing. I've updated the first post.
Last edited by qwerty12 on 07 Aug 2016, 15:00, edited 1 time in total.
wrecklass
Posts: 6
Joined: 20 Nov 2015, 16:40

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

06 Aug 2016, 00:17

Thanks qwerty12! The second option did the trick. SOOO happy to have this working again, it's a real time saver.

BTW, The upgrade for Windows 10 deletes all of the desktops you have created, so you have to recreate them.
Guest

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

08 Aug 2016, 07:05

Am I the only one having some taskbar applications flashing after switching among the virtual desktops?
It's a bit annoying if they keep highlighting even if there is nothing happening there...
wrecklass
Posts: 6
Joined: 20 Nov 2015, 16:40

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

16 Aug 2016, 01:00

Guest wrote:Am I the only one having some taskbar applications flashing after switching among the virtual desktops?
It's a bit annoying if they keep highlighting even if there is nothing happening there...
I don't know if you are the only person with that problem, I do know I don't have the problem. What is your computer hardware/GPU? It's possible you need an update to your drivers.

Also, how do you have your Multitasking settings set up?
m0b-ua
Posts: 12
Joined: 31 Aug 2016, 06:35

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

31 Aug 2016, 06:52

Guest wrote:Am I the only one having some taskbar applications flashing after switching among the virtual desktops?
It's a bit annoying if they keep highlighting even if there is nothing happening there...
I have the same problem! I think it's because none window is active after desktop change, and the last active window on current desktop starts to blink till you click on it (select it, activate it). If you don't activate it and switch to next desktop, blinking window will stay on taskbar and won't hide.

The script shoud activate last window after switching. I fix it by adding "Send !{Tab}" to the end of "switchToDesktop" function, but i think there is a better way to fix the issue.


My solution:

Code: Select all

switchToDesktop(idx)
{
  global CurrentDesktopTest
  global ppDesktopManager, IID_IVirtualDesktop
  DllCall(vtable(ppDesktopManager, 7), "Ptr", ppDesktopManager, "Ptr*", pDesktops)
  if (pDesktops) {
    DllCall(vtable(pDesktops, 4), "Ptr", pDesktops, "UInt", idx-1, "Ptr", &IID_IVirtualDesktop, "Ptr*", VirtualDesktop)
    if (VirtualDesktop)
    {
      DllCall(vtable(ppDesktopManager, 9), "Ptr", ppDesktopManager, "Ptr", VirtualDesktop)
      ObjRelease(VirtualDesktop) ; I assume these should be freed
      if (CurrentDesktopTest != idx)
      {
        Send !{Tab}
        CurrentDesktopTest = %idx%
      }
    }
    ObjRelease(pDesktops)
  }
}
nunizaku

Re: [Windows 10] Switch to different virtual desktop on Win+{1,9}

30 Dec 2016, 09:34

Hi, sorry for *slight* offtopic, couse i already have everything i much need mapper to logitech 20+ button mouse and it works great,
i just look for a way to maximixed windows go to new dekstop, like osx, and if restored, go to original desktop.

any kind soul give me a clue for that?

Return to “Scripts and Functions”

Who is online

Users browsing this forum: loidokaar and 64 guests