Media Script - Strange Behaviours

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Jademalo
Posts: 4
Joined: 27 May 2018, 18:03

Media Script - Strange Behaviours

27 May 2018, 18:17

I have a script that's seemingly fairly simple, but is coming up with no end of really weird issues.

It should work like this;
Scroll Lock is changed from it's normal function into a toggle key. When it's toggled, it enables/disables changing the home/end etc bank of 6 keys into media keys. Insert is Previous track, Home is Play/Pause, Pg Up is Next Track, Del is Mute, End is Volume Up, and Pg Dn is Volume Down.

I also wanted the scroll lock light to indicate if I've got the media keys enabled or disabled. I copied an LED control script, and it seems to work well - http://www.autohotkey.com/forum/viewtop ... 000#468000


Problem 1; Caps Lock.
So the Caps Lock key is weird. When I have the media keys enabled, pressing Caps Lock enables it's own light but disables the light for Scroll Lock. I've managed to figure out a workaround to this, but it involves totally taking control of the caps lock key, and then setting the scroll lock light depending on the state of the caps lock key. Weirdly, this isn't an issue the other way round. Regardless of what the caps lock key is set to, changing the scroll lock light doesn't affect the caps lock key.
In addition, I've had to add a weird 10ms wait on the light changing as it sometimes doesn't set the capslock state fast enough before setting the scroll lock light.
I'd much rather have a more elegant solution to this, this is super dodgy.

Problem 2; Media Play/Pause.
This is an issue I have absolutely no idea how to fix, or where even to begin.
When media keys are enabled, if I highlight a file in Explorer then it doesn't play/pause. If I click elsewhere on my PC, then play/pause works perfectly again. In addition, when playing Guild Wars 2, pressing play/pause presses the G key. Like, I'm assuming when a folder is highlighted for some reason home overrides, but I have NO idea how in GW2 it presses the G keybind.

Code: Select all

/*

    Keyboard LED control for AutoHotkey_L
        http://www.autohotkey.com/forum/viewtopic.php?p=468000#468000

    KeyboardLED(LEDvalue, "Cmd", Kbd)
        LEDvalue  - ScrollLock=1, NumLock=2, CapsLock=4
        Cmd       - on/off/switch
        Kbd       - index of keyboard (probably 0 or 2)

*/

KeyboardLED(LEDvalue, Cmd, Kbd=0)
{
  SetUnicodeStr(fn,"\Device\KeyBoardClass" Kbd)
  h_device:=NtCreateFile(fn,0+0x00000100+0x00000080+0x00100000,1,1,0x00000040+0x00000020,0)
  
  If Cmd= switch  ;switches every LED according to LEDvalue
   KeyLED:= LEDvalue
  If Cmd= on  ;forces all choosen LED's to ON (LEDvalue= 0 ->LED's according to keystate)
   KeyLED:= LEDvalue | (GetKeyState("ScrollLock", "T") + 2*GetKeyState("NumLock", "T") + 4*GetKeyState("CapsLock", "T"))
  If Cmd= off  ;forces all choosen LED's to OFF (LEDvalue= 0 ->LED's according to keystate)
    {
    LEDvalue:= LEDvalue ^ 7
    KeyLED:= LEDvalue & (GetKeyState("ScrollLock", "T") + 2*GetKeyState("NumLock", "T") + 4*GetKeyState("CapsLock", "T"))
    }
  
  success := DllCall( "DeviceIoControl"
              ,  "ptr", h_device
              , "uint", CTL_CODE( 0x0000000b     ; FILE_DEVICE_KEYBOARD
                        , 2
                        , 0             ; METHOD_BUFFERED
                        , 0  )          ; FILE_ANY_ACCESS
              , "int*", KeyLED << 16
              , "uint", 4
              ,  "ptr", 0
              , "uint", 0
              ,  "ptr*", output_actual
              ,  "ptr", 0 )
  
  NtCloseFile(h_device)
  return success
}

CTL_CODE( p_device_type, p_function, p_method, p_access )
{
  Return, ( p_device_type << 16 ) | ( p_access << 14 ) | ( p_function << 2 ) | p_method
}


NtCreateFile(ByRef wfilename,desiredaccess,sharemode,createdist,flags,fattribs)
{
  VarSetCapacity(objattrib,6*A_PtrSize,0)
  VarSetCapacity(io,2*A_PtrSize,0)
  VarSetCapacity(pus,2*A_PtrSize)
  DllCall("ntdll\RtlInitUnicodeString","ptr",&pus,"ptr",&wfilename)
  NumPut(6*A_PtrSize,objattrib,0)
  NumPut(&pus,objattrib,2*A_PtrSize)
  status:=DllCall("ntdll\ZwCreateFile","ptr*",fh,"UInt",desiredaccess,"ptr",&objattrib
                  ,"ptr",&io,"ptr",0,"UInt",fattribs,"UInt",sharemode,"UInt",createdist
                  ,"UInt",flags,"ptr",0,"UInt",0, "UInt")
  return % fh
}

NtCloseFile(handle)
{
  return DllCall("ntdll\ZwClose","ptr",handle)
}


SetUnicodeStr(ByRef out, str_)
{
  VarSetCapacity(out,2*StrPut(str_,"utf-16"))
  StrPut(str_,&out,"utf-16")
}







; This is where my script starts!
; Media Key Bank

mediaState := 0

ScrollLock:: ; Scroll lock will toggle the state
if (mediaState = 1)
{
   mediaState := 0
   KeyboardLED(1,"off")
}
else
{
   mediaState := 1
   KeyboardLED(1,"on")
}
return


CapsLock:: 
if (mediaState = 0)
{
	if GetKeyState("CapsLock", "T") = 1
		SetCapsLockState, off
	else
		SetCapsLockState, on
}
else if (mediaState = 1)
{
	if GetKeyState("CapsLock", "T") = 1
	{
		SetCapsLockState, off
		sleep 10
		KeyboardLED(1,"on")
		return
	}
	else
	{
		SetCapsLockState, on
		sleep 10
		KeyboardLED(1,"on")
		return
		}
}
return






;Here be Keybinds

Insert:: 
if mediaState = 1
{
Send {Media_Prev}
return
}
else
{
Send {Insert}
return
}

Home:: 
if mediaState = 1
{
Send {Media_Play_Pause}
return
}
else
{
Send {Home}
return
}

PgUp:: 
if mediaState = 1
{
Send {Media_Next}
return
}
else
{
Send {PgUp}
return
}

Delete:: 
if mediaState = 1
{
Send {Volume_Mute}
return
}
else
{
Send {Delete}
return
}

End:: 
if mediaState = 1
{
Send {Volume_Down}
return
}
else
{
Send {End}
return
}

PgDn:: 
if mediaState = 1
{
Send {Volume_Up}
return
}
else
{
Send {PgDn}
return
}
Any advice?
Thanks!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: GEOVAN, mihai369, norot41087, Rohwedder and 130 guests