Tablet Mode toggle for Windows 10

Post your working scripts, libraries and tools for AHK v1.1 and older
johnnyzero
Posts: 11
Joined: 04 Mar 2016, 12:24

Tablet Mode toggle for Windows 10

05 Apr 2016, 17:12

Here's a simple script that toggles Windows 10's Tablet Mode on/off. Early Windows 10 builds had a built-in keyboard shortcut for this (Win+~), but it was removed at some point.

I realize this is not the most elegant solution (keystrokes), but it works.

Code: Select all

run ms-settings:tabletmode
sleep, 1000
send {space}
send {alt down}{F4}{alt up}
The Zip file includes the .ahk source, a compiled EXE version, and the source icon file.

You can create a shortcut to the EXE and then pin it to your taskbar and/or Start screen, or even assign a keyboard shortcut (hotkey). I find it a bit easier than having to access the Action Center every time I want to switch modes.
Attachments
TabletToggle.zip
(421.93 KiB) Downloaded 964 times
whitewater
Posts: 17
Joined: 13 Mar 2016, 16:19

Re: Tablet Mode toggle for Windows 10

05 Apr 2016, 19:05

Johnny, Thanks for the code. I did find a different solution that uses click to toggle the mode. (It is not very elegant, but it works on my Surface 3 Pro)
F3::
send, #a
CoordMode, Mouse, Screen
sleep, 200
Send {click 1510 ,936 }

IfWinActive, Action center
{
send #a
}
return
johnnyzero
Posts: 11
Joined: 04 Mar 2016, 12:24

Re: Tablet Mode toggle for Windows 10

05 Apr 2016, 21:02

Yes, your script will work fine on a given machine once you figure out the "click coordinates" to use (using Window Spy or whatever). However, I wanted to create a no-tweaking-required script that would just work on any Windows 10 system, independent of the screen resolution and/or the location of the Quick Action buttons.

I'm hoping that someone will eventually come out with an AHK library to handle some of this new-fangled Windows 10 UI stuff. Then we won't have to rely so much on mouse clicks & keystrokes. :D
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Tablet Mode toggle for Windows 10

06 Apr 2016, 05:54

johnnyzero wrote:I'm hoping that someone will eventually come out with an AHK library to handle some of this new-fangled Windows 10 UI stuff. Then we won't have to rely so much on mouse clicks & keystrokes. :D
Unfortunately the WinAPI doesn't expose any functions for handling this stuff programmatically (I guess because Microsoft don't want people developing software that changes user's settings without permission, which makes sense), meaning that unless there's some hidden internal interface for this (either functions in some DLL or a COM interface or a binary somewhere capable of changing these things), we're stuck using UI interactions (clicks & keystrokes).
whitewater
Posts: 17
Joined: 13 Mar 2016, 16:19

Re: Tablet Mode toggle for Windows 10

06 Apr 2016, 14:12

The Win10 issue is frustrating, but I did find a way to access control panel items and toggle Hide/UnHide Taskbar

Code: Select all

F6::
run control.exe /name Microsoft.Taskbar
sleep, 500
send u  ;A&utohide the Taskbar
ControlClick, OK, A
return
Win 8 control panel names can be found below, but many work in Win 10
https://msdn.microsoft.com/en-us/librar ... s.85).aspx

Unfortunately, the ActionCenter is no longer in the control panel, so you can't use this method to toggle between tablet and desktop modes. I would still like to find a programmatic way to toggle Tablet mode.
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Re: Tablet Mode toggle for Windows 10

06 Apr 2016, 15:44

SifJar wrote:
johnnyzero wrote:I'm hoping that someone will eventually come out with an AHK library to handle some of this new-fangled Windows 10 UI stuff. Then we won't have to rely so much on mouse clicks & keystrokes. :D
Unfortunately the WinAPI doesn't expose any functions for handling this stuff programmatically (I guess because Microsoft don't want people developing software that changes user's settings without permission, which makes sense), meaning that unless there's some hidden internal interface for this (either functions in some DLL or a COM interface or a binary somewhere capable of changing these things), we're stuck using UI interactions (clicks & keystrokes).
A lot of the new Win 10 visual stuff is still just an interface to act on the registry or something like it. For example, both Tablet/Desktop modes and Scaling% are registry entries that you can toggle with AHK. The issue is (and that's not a Win 10 issue, it's for Windows in general), unlike when you do it through the Windows interface, using AHK necessitates a logoff/reboot for the change to take effect. But again, that's not exclusively a Win 10 issue. There are a few AHK things you can try for the registry change to take effect automatically, but in some cases (such as these two examples), the only way seems to be a reboot.
whitewater wrote:The Win10 issue is frustrating, but I did find a way to access control panel items and toggle Hide/UnHide Taskbar
Not everything has changed on going to Win 10. Doesn't this good old code for toggling taskbar autohide still work?

Code: Select all

;TurtleOfDoom: http://www.autohotkey.com/board/topic/25932-trying-to-toggle-autohide-taskbar-with-keystroke-in-vista/?p=327990

ToggleAutoHideTaskBar()

ToggleAutoHideTaskBar()
{
   VarSetCapacity( APPBARDATA, 36, 0 )
   NumPut(36, APPBARDATA, 0, "UInt")
   bits := DllCall("Shell32.dll\SHAppBarMessage"
             ,"UInt", 4
             ,"UInt", &APPBARDATA )
  NumPut( (bits ^ 0x1), APPBARDATA, 32, "UInt" )
  DllCall("Shell32.dll\SHAppBarMessage"
             ,"UInt", ( ABM_SETSTATE := 0xA )
             ,"UInt", &APPBARDATA )
}
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Tablet Mode toggle for Windows 10

06 Apr 2016, 16:32

lblb wrote:A lot of the new Win 10 visual stuff is still just an interface to act on the registry or something like it. For example, both Tablet/Desktop modes and Scaling% are registry entries that you can toggle with AHK. The issue is (and that's not a Win 10 issue, it's for Windows in general), unlike when you do it through the Windows interface, using AHK necessitates a logoff/reboot for the change to take effect. But again, that's not exclusively a Win 10 issue. There are a few AHK things you can try for the registry change to take effect automatically, but in some cases (such as these two examples), the only way seems to be a reboot.
The registry isn't really a way to change these settings, it's more like a record of what the settings ARE. And seeing as it's the record that is looked up at login, it makes sense that changing the records there will change the way those settings are loaded at next login. And yes, sometimes you can trigger some settings being reloaded from the registry, but it's not correct to say that the "Win 10 visual stuff" is an interface to the registry - it directly changes settings, and then records those changes to the registry (but it doesn't change them by writing to the registry)
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Re: Tablet Mode toggle for Windows 10

06 Apr 2016, 17:00

@SifJar,

I agree with most of what you said. I'm not trying to start a semantics or even a detailed discussion (as this would not be the appropriate thread), I was just trying to say in simple terms that it's not all new Windows magic that just popped up in Windows 10 and that many Win 10 settings are directly accessible in some way or another. But while as you said the registry is indeed a record, it can certainly be used to trigger things directly. There are plenty of system and software settings that can be affected by simple registry changes and that take effect immediately on change. As an end-user, there are plenty of settings that an AHK user can change directly by only writing to the registry. Anyway, I don't want to hijack this thread with this tangential discussion.

Cheers!
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Re: Tablet Mode toggle for Windows 10

07 Apr 2016, 03:03

Working off johnnyzero's code, here is the best I could do to improve the reliability (with Winwait, controlsend, and winclose) and the intrusiveness (on my system, you barely see the Settings window on the taskbar; you may have to adjust the sleep delays for your system) to toggle between tablet and desktop modes:

Code: Select all

#Singleinstance, force
Setbatchlines, -1
SetWinDelay, -1

StringTrimRight, AppDataPath, A_AppData, 8
Run, %AppDataPath%\Local\Packages\windows.immersivecontrolpanel_cw5n1h2txyewy\LocalState\Indexed\Settings\en-US\AAA_SystemSettings_ShellMode_Toggle.settingcontent-ms
WinWait, Settings ahk_class ApplicationFrameWindow
Sleep, 100
WinMove, Settings ahk_class ApplicationFrameWindow, , 8000, -8000
Sleep, 700
ControlSend,, {Space}, Settings ahk_class ApplicationFrameWindow
WinClose, Settings ahk_class ApplicationFrameWindow
johnnyzero
Posts: 11
Joined: 04 Mar 2016, 12:24

Re: Tablet Mode toggle for Windows 10

07 Apr 2016, 11:56

@lblb,

Nice work. That's a huge improvement over my original (rather crude) script. It's quicker & less intrusive. I tried it on several different systems, and it worked flawlessly without having to adjust the Sleep delays.
whitewater
Posts: 17
Joined: 13 Mar 2016, 16:19

Re: Tablet Mode toggle for Windows 10

08 Apr 2016, 16:01

Ibib,
Thanks for the much more general Tablet/Desktop Toggle.
I would like to display the Start Menu when I toggle to Desktop. (This was an option during Win 10 development, but they took it out)
Is there a way to check if the Start Menu is on?
How about checking whether mode is Tablet or Desktop?
Thanks for the great code!
johnnyzero
Posts: 11
Joined: 04 Mar 2016, 12:24

Re: Tablet Mode toggle for Windows 10

08 Apr 2016, 16:38

Your script can check for Desktop/Tablet mode by reading the value of registry key: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\ImmersiveShell\TabletMode

If in Desktop mode, then have the last line of your script do a Send {LWin} to display the Start menu.
whitewater
Posts: 17
Joined: 13 Mar 2016, 16:19

Re: Tablet Mode toggle for Windows 10

08 Apr 2016, 16:50

johnnyz,
Perfect, just what I was looking for. I agree that a Win 10 library of scripts would be a good idea. I have a Surface Pro3 and find Win 10 lacking in many areas. An AHK library may be what we need until MS gets it right.
johnnyzero
Posts: 11
Joined: 04 Mar 2016, 12:24

Re: Tablet Mode toggle for Windows 10

08 Apr 2016, 16:57

Actually, all you need to do is add the following line to the end of lblb's script:
Send {LWin}

There's really no need to check the mode: if you're in Tablet mode, the Win key press will simply have no effect.
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Re: Tablet Mode toggle for Windows 10

08 Apr 2016, 17:10

@johnnyzero and @whitewater,

Thanks for confirming that it also works for you. This information will be very useful as I am currently updating various scripts from the Win 7 era dedicated to tablet PC's that I have shared at other places (see for example: http://forum.tabletpcreview.com/threads ... -pc.56322/)

The code seems to work well to toggle several settings accessible through the various UI shortcuts here: %AppDataPath%\Local\Packages\windows.immersivecontrolpanel_cw5n1h2txyewy\LocalState\Indexed\Settings\en-US\. Although I'm guessing the name of the last subfolder may depend on the system language.

Cheers!
whitewater
Posts: 17
Joined: 13 Mar 2016, 16:19

Re: Tablet Mode toggle for Windows 10

08 Apr 2016, 17:54

I added @johnnyzero's RegRead to @Ibid's code after the WinClose statement. FYI, The TabletMode value does not appear to change until you leave the program, so the logic is backwards, but it works.
Johnny is right thou, you don't need it because LWin has no effect in Tablet mode
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Re: Tablet Mode toggle for Windows 10

08 Apr 2016, 18:02

Hi whitewater,

The registry TabletMode value does change but there is a short delay. So if you needed it, you could introduce a delay before reading the value. Or better yet, don't introduce a delay and read the value right away knowing that the value that is read corresponds to the previous mode that hasn't been changed yet to the new mode.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Tablet Mode toggle for Windows 10

09 Apr 2016, 17:46

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!
Last edited by qwerty12 on 11 Apr 2016, 15:20, edited 1 time in total.
whitewater
Posts: 17
Joined: 13 Mar 2016, 16:19

Re: Tablet Mode toggle for Windows 10

10 Apr 2016, 18:30

qwerty12,
Thanks for the code. It works more consistently than the other methods in this topic. On my Surface pro 3, it toggles quickly and accurately. When I toggle to Desktop, it brings up the start Menu all the time. This is what I want, but others may want the desktop, or may want the previous setting. Any ideas which parameter may control this? (or where to find documentation of the settings)

Also, is there a way to toggle Hide/Unhide the taskbar without calling up the window?
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Tablet Mode toggle for Windows 10

10 Apr 2016, 19:09

Thanks for testing, whitewater.
When I toggle to Desktop, it brings up the start Menu all the time.
Do you still have the LWin press from this thread present in your script? It's very possible that the problem is caused by something on my end (laziness in not working out the full possible values for the TMCTRIGGER value, calling the wrong function from the vtable etc. etc.), but it's just that I tried the script on my non-tablet X230 with StartIsBack++, and a stock 10586 install in VMware - in both installs, exiting back to the desktop did not show the start menu. I also tried the script just now using the closest thing I have to a Windows 10 tablet - an iPad connected to my X230 over Remote Desktop - and I did not see the start menu when going back into desktop mode over RDP.
Any ideas which parameter may control this?
Sure, just remove the if (TabletModeController_GetMode(TabletModeController, mode) == 0) line and then replace the line with TabletModeController_SetMode entirely with TabletModeController_SetMode(TabletModeController, TABLETMODESTATE_DESKTOPMODE). This will make the script always try setting the current view mode into desktop mode. For tablet mode, it's the same thing, just use TABLETMODESTATE_TABLETMODE instead.
Also, is there a way to toggle Hide/Unhide the taskbar without calling up the window?
Have you tried the code from lbib's post: https://autohotkey.com/boards/viewtopic ... 437#p79437
I don't personally know how; I like to use RaMMicHaeL's 7+ Taskbar Tweaker and assign toggling auto-hide to a middle-click press on the Aero Peek button using 7TT's Advanced Options.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 257 guests