Why does powercfg not do what I want in Windows 7?
July 1, 2011 2:08 AM   Subscribe

How can I disable the keyboard Sleep key from a cmd script in Windows 7?

If I visit the Control Panel, then choose "Hardware and Sound", then choose "Change what the power buttons do", then change "When I press the sleep button:" to "Do nothing", then pressing the Sleep button on the keyboard does indeed do nothing. I can turn it back on by changing "When I press the sleep button:" back to "Sleep". So I'm sure that the physical button I'm playing with is indeed the one that Windows knows as the "sleep button", and I'm looking for a way to script that setting to avoid shenanigans in our school computer lab.

At present I'm doing this:
for /f "tokens=2 delims=:" %%G in ('powercfg -getactivescheme') do set guid=%%G
for /f %%G in ("%guid%") do set guid=%%G
powercfg -SetAcValueIndex %guid% SUB_BUTTONS SBUTTONACTION 0
And although this causes identical output from powercfg -q to what I get by making the same change using the GUI, it does not actually affect the sleep button, which continues to sleep the PC when pressed.

What do I need to do to make Windows pay attention to settings applied via powercfg?
posted by flabdablet to Computers & Internet (4 answers total)
 
Response by poster: OK, sussed it. If I add
powercfg -SetActive %guid%
to the end of what I already have, it works.


...and then this big guy says, "Now we can all get some sleep."

posted by flabdablet at 2:23 AM on July 1, 2011 [1 favorite]


Response by poster: All of which is very nice, but means that the sleep button comes back alive if the user picks a different power scheme. This version fixes that by modifying the sleep button action in all available power schemes:
for /f "skip=2 tokens=2,4 delims=:()" %%G in ('powercfg -list') do (
	powercfg -setAcValueIndex %%G sub_buttons sButtonAction 0
	if "%%H" == " *" powercfg -setActive %%G
)

posted by flabdablet at 1:50 AM on July 11, 2011


Response by poster: As it turns out, I also needed to do this for some Windows XP machines. The Windows XP version of powercfg has no button control options at all, and the registry value where the button settings appear to be kept is an inscrutable binary blob. Finding that out made me cross enough to write the following function and test harness, which works for both XP and 7.
call :SleepButton DoNothing
pause
call :SleepButton AskMe
pause
call :SleepButton StandBy
pause
call :SleepButton ShutDown
pause
goto :eof



:SleepButton
setLocal
set v=1& set w1=00000000& set w2=08000000
if /i "%1" == "DoNothing" (set v=0& set w2=00000080) else ^
if /i "%1" == "ShutDown" (set v=3& set w2=10000000) else ^
if /i "%1" == "StandBy" (set v=2& set w1=02000000& set w2=00000000)
for /f "tokens=2 delims=[]" %%V in ('ver') do ^
for /f "tokens=2 delims=. " %%A in ("%%V") do ^
call :SleepButton%%A
endLocal
goto :eof

:SleepButton5 - Windows XP
set key=HKCU\Control Panel\PowerCfg\GlobalPowerPolicy
set val=Policies
set type=REG_BINARY
for /f "skip=3 tokens=3" %%D in ('reg query "%key%" /v %val%') do set data=%%D
reg add "%key%" /v %val% /t %type% /d %data:~,56%%w1%%data:~64,8%%w2%%data:~80,16%%w2%%data:~104% /f
powercfg /globalPowerFlag off /option batteryIcon
goto :eof

:SleepButton6 - Windows 7
for /f "skip=2 tokens=2,4 delims=:()" %%G in ('powercfg -list') do (
	powercfg -setAcValueIndex %%G sub_buttons sButtonAction %v%
	powercfg -setDcValueIndex %%G sub_buttons sButtonAction %v%
	if "%%H" == " *" powercfg -setActive %%G
)
goto :eof

posted by flabdablet at 6:43 AM on July 11, 2011


Response by poster: ...except it doesn't, quite, because the button actions available to XP are not the same as those available to 7... grrrr. Here's an updated version. On XP, StandBy and Hibernate do the same thing; on 7, AskMe and DoNothing do the same thing.
call :SleepButton AskMe
pause
call :SleepButton DoNothing
pause
call :SleepButton StandBy
pause
call :SleepButton Hibernate
pause
call :SleepButton ShutDown
pause
goto :eof



:SleepButton
setLocal
set index=0& set w1=00000000& set w2=08000000
if /i "%1" == "DoNothing" (set index=0& set w2=00000080) else ^
if /i "%1" == "StandBy"   (set index=1& set w1=02000000& set w2=00000000) else ^
if /i "%1" == "Hibernate" (set index=2& set w1=02000000& set w2=00000000) else ^
if /i "%1" == "ShutDown"  (set index=3& set w2=10000000)
for /f "tokens=2 delims=[]" %%V in ('ver') do ^
for /f "tokens=2 delims=. " %%A in ("%%V") do ^
call :SleepButton%%A
endLocal
goto :eof

:SleepButton5 - Windows XP
set key=HKCU\Control Panel\PowerCfg\GlobalPowerPolicy
set val=Policies
set type=REG_BINARY
for /f "skip=3 tokens=3" %%D in ('reg query "%key%" /v %val%') do set data=%%D
reg add "%key%" /v %val% /t %type% /d %data:~,56%%w1%%data:~64,8%%w2%%data:~80,16%%w2%%data:~104% /f
powercfg /globalPowerFlag off /option batteryIcon
goto :eof

:SleepButton6 - Windows 7
for /f "skip=2 tokens=2,4 delims=:()" %%G in ('powercfg -list') do (
	powercfg -setAcValueIndex %%G sub_buttons sButtonAction %index%
	powercfg -setDcValueIndex %%G sub_buttons sButtonAction %index%
	if "%%H" == " *" powercfg -setActive %%G
)
goto :eof

posted by flabdablet at 7:26 AM on July 11, 2011


« Older New York Gifts?   |   Please help me deal with these movie location... Newer »
This thread is closed to new comments.