Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Change laptop PowerPlan with shortcut Key (via registry)



  • Please log in to reply
3 replies to this topic
AbbasKhan
  • Members
  • 11 posts
  • Last active: Aug 13 2013 07:45 AM
  • Joined: 22 Jun 2013

Hey, I'm trying to create a hotkey to change laptop power plan (from HighPerformance to Power-Saver - where CPU is locked to 50%, when using backup power - which i have to do several times a day)

my code doesn't seem to update the registry:/

https://ahknet.autoh...m/paste/1s7kpyl

;;Change Laptop PowerPlan - shortcut key
+^!e::
    ;RegWrite, REG_SZ, HKEY_LOCAL_MACHINE, SOFTWARE\TestKey, MyValueName, Test Value
    ; raw key name: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\Default\PowerSchemes
    RegWrite, REG_SZ, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Control\Power\User\Default\PowerSchemes, ActivePowerScheme, a1841308-3541-4fab-bc81-f71556f20b4a
    RegWrite, REG_SZ, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes, ActivePowerScheme, a1841308-3541-4fab-bc81-f71556f20b4a
    RegWrite, REG_SZ, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Control\Power\User\Default\PowerSchemes\Abbas, AbbasTestPower, AbbasTestValue
return

I'm trying to update these vaues inside registry (screenshot of registry editor) http://i.imgur.com/WR1qauV.jpg

 

P.S.: I tried quoting the value also and then running it (as administrative rights) - but still no luck



Linear Spoon
  • Members
  • 842 posts
  • Last active: Sep 29 2015 03:56 AM
  • Joined: 29 Oct 2011
✓  Best Answer

Okay if you have Vista+, this is a much better way to work with power schemes.

 

First step - run this snippet of code.

VarSetCapacity(guid,szguid := 16)
VarSetCapacity(desc, szdesc := 1024)

Loop
{
  r := Dllcall("powrprof.dll\PowerEnumerate", "ptr", 0, "ptr", 0, "ptr", 0, "uint", 16, "uint", A_Index-1, "ptr", &guid, "uint*", 16)
  if (r != 0)
    break
  r2 := Dllcall("powrprof.dll\PowerReadFriendlyName", "ptr", 0, "ptr", &guid, "ptr", 0, "ptr", 0, "str", desc, "uint*", 1024)
  if (r2 != 0)
  {
    Msgbox % "PowerReadFriendlyName error: " r2
    ExitApp
  }
  plan .= A_Index-1 " - " desc "`n"
}

if (r != 259)  ;ERROR_NO_MORE_ITEMS
{
  Msgbox % "PowerEnumerate error: " r
  Exitapp
}

Msgbox Available power schemes:`n%plan%

If there are no errors, it will produce a message box listing your available power schemes like so:

9Olwdvg.png

The number to the left of the scheme name is what you'll need for the next bit. Here's the function and example hotkeys:

F1::SetScheme(1) ;Enable "High performance" plan on pressing F1
F2::SetScheme(2) ;Enable "Power saver" plan on pressing F2

;SchemeIndex is the number you got from running the first script
SetScheme(SchemeIndex)
{
  VarSetCapacity(guid, 16)
  Dllcall("powrprof.dll\PowerEnumerate", "ptr", 0, "ptr", 0, "ptr", 0, "uint", 16, "uint", SchemeIndex, "ptr", &guid, "uint*", 16)
  r := Dllcall("powrprof.dll\PowerSetActiveScheme", "ptr", 0, "ptr", &guid)
  if r
    Msgbox % "PowerSetActiveScheme error: " r
}

Again, this requires Vista+ and a Unicode version of AHK. Also, make sure you actually use the first script as your power schemes may be numbered differently than mine. Best of luck.

 

Edit: There's a different set of functions that will probably work for windows XP if needed but I won't write another script unless it's necessary.

Edit2: By the way it seems there's already a bunch of scripts out there for this exact problem... search the forum for "power scheme" and see...


Join us at the new forum - http://www.ahkscript.org/

 


AbbasKhan
  • Members
  • 11 posts
  • Last active: Aug 13 2013 07:45 AM
  • Joined: 22 Jun 2013

Hey, thank you for replying - linear spoon.

I'm on WIndows 8. What do you mean by Unicode version of AHK? I have the latest AHK installed. I tried pasting some ascii emoticons but they appear as squared blocks.

 

THANK YOU - so much Linear Spoon, it worked great on my Windows 8 x64.

Now I can switch Laptop Power Plans with ctrl+f1 and ctrl+f2   grin.png - just adding ^ symbol in front of f1, f2 key calls in your code above.

and some toolTip balloons for confirmation messages -- very convenient.

 

Working Code:

;;Change Laptop PowerPlan - shortcut key
^F1::SetScheme(2) ;Enable "High performance" plan on pressing ctrol+F1
^F2::SetScheme(3) ;Enable "Power saver" plan on pressing ctrl+F2

;SchemeIndex is the number you got from running the first script
SetScheme(SchemeIndex)
{
  VarSetCapacity(guid, 16)
  Dllcall("powrprof.dll\PowerEnumerate", "ptr", 0, "ptr", 0, "ptr", 0, "uint", 16, "uint", SchemeIndex, "ptr", &guid, "uint*", 16)
  r := Dllcall("powrprof.dll\PowerSetActiveScheme", "ptr", 0, "ptr", &guid)
  if r
    Msgbox % "PowerSetActiveScheme error: " r
	if(SchemeIndex==3)
		TrayTip, PowerSaver mode, is now active, 20, 17
	else
		TrayTip, HighPerformance mode, is now active, 20, 17
}

 

I'd still love a simpler implementation - that newbies like me can understand  - if that's possible. Like using that RegisteryWriting function of ahk in my first code - but so far your code gets the job done grin.png



Linear Spoon
  • Members
  • 842 posts
  • Last active: Sep 29 2015 03:56 AM
  • Joined: 29 Oct 2011

I'd still love a simpler implementation - that newbies like me can understand  - if that's possible.

In plain English, the PowerEnumerate function returns  GUIDs of each power scheme you have. A GUID is just a number that identifies a resource. The number you pass as the index determines which scheme you get (This is the same as "SchemeIndex" in my function).

PowerReadFriendlyName takes the GUID of a power scheme and returns its description such as "High performance".

The first script loops through the possible index values until PowerEnumerate says there are no more schemes and PowerReadFriendlyName gets their names.

 

The Set Scheme function takes one of the index values found before and uses PowerEnumerate to get that scheme's GUID. The GUID is passed to PowerSetActiveScheme to enable it.

 

I know it seems complicated, but this is honestly probably how Windows sets the scheme behind the scenes. If you just muck around changing registry values, it might not update until you restart, or you might miss some settings that need to be changed for it to work properly. Hang around these forums long enough and keep learning about AutoHotkey and this sort of scripting will come more easily to you.

 

What do you mean by Unicode version of AHK?

Unicode is a character format. The other version of AutoHotkey is ANSI. The simplest way to explain the difference is that ANSI uses 1 byte per character in strings while Unicode uses 2. PowerReadFriendlyName only returns Unicode strings. It could be adapted to work with an ANSI version, but it would take more code.


Join us at the new forum - http://www.ahkscript.org/