EDIT: I've confirmed the vtable positions are correct and 4 is the TMCTRIGGER value sent by the Settings application when it changes the Shell Mode.
Even easier yet
Code: Select all
#NoEnv
SetBatchLines -1
ListLines Off
#NoTrayIcon
TABLETMODESTATE_DESKTOPMODE := 0x0
TABLETMODESTATE_TABLETMODE := 0x1
TabletModeController_GetMode(TabletModeController, ByRef mode) {
return DllCall(NumGet(NumGet(TabletModeController+0),3*A_PtrSize), "Ptr", TabletModeController, "UInt*", mode)
}
TabletModeController_SetMode(TabletModeController, _TABLETMODESTATE, _TMCTRIGGER := 4) {
return DllCall(NumGet(NumGet(TabletModeController+0),4*A_PtrSize), "Ptr", TabletModeController, "UInt", _TABLETMODESTATE, "UInt", _TMCTRIGGER)
}
ImmersiveShell := ComObjCreate("{C2F03A33-21F5-47FA-B4BB-156362A2F239}", "{00000000-0000-0000-C000-000000000046}")
TabletModeController := ComObjQuery(ImmersiveShell, "{4fda780a-acd2-41f7-b4f2-ebe674c9bf2a}", "{4fda780a-acd2-41f7-b4f2-ebe674c9bf2a}")
if (TabletModeController_GetMode(TabletModeController, mode) == 0)
TabletModeController_SetMode(TabletModeController, mode == TABLETMODESTATE_DESKTOPMODE ? TABLETMODESTATE_TABLETMODE : TABLETMODESTATE_DESKTOPMODE)
ObjRelease(TabletModeController), TabletModeController := 0
ObjRelease(ImmersiveShell), ImmersiveShell := 0 ; Can be freed after TabletModeController is created, instead
This will toggle the mode without any mouse/keyboard/window manipulation.
I don't actually use tablet mode anyway, so I have no motivation to find out the possible values of _TMCTRIGGER. My guess is that it's set depending on how tablet mode was activated e.g. by setting the TM setting manually through Settings or if Windows entered it on when, say, the tablet it's running on is rotated. (Since I don't have a Windows tablet, I actually have no idea how TM gets activated other than activating it manually from Settings.) I think Windows uses it to decide whether it should nag you about it entering TM automatically and whether it should automatically change the mode back when there's a change in whatever it monitors.
I chose 4 as it's the value used when activating Tablet Mode from the Settings app. Since my AutoHotkey script is also changing the state manually, it seemed apt. You can see for yourself the last value used for the trigger by jumping to the Registry Key HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\2 and looking at the Value ModeTriggerCachedKey.
Have fun!