Skip to content

Instantly share code, notes, and snippets.

@KawaiiKraken
Last active April 1, 2024 05:33
Show Gist options
  • Save KawaiiKraken/9ab320e57e88990f39ae271dbc22e223 to your computer and use it in GitHub Desktop.
Save KawaiiKraken/9ab320e57e88990f39ae271dbc22e223 to your computer and use it in GitHub Desktop.
#NoEnv
#SingleInstance Force
#KeyHistory 0
#MaxThreadsPerHotkey 2
#HotkeyModifierTimeout -1
SetBatchLines, -1
ListLines, Off
SendMode Input
SetTitleMatchMode, 2
; spotify hotkey definitions
global TOGGLE_PAUSE := "{space}"
global VOLUME_UP := "^{Up}"
global VOLUME_DOWN := "^{Down}"
global NEXT_TRACK := "^{Right}"
global PREV_TRACK := "^{Left}"
global LIKE_TRACK := "!+{b}"
global TOGGLE_SHUFFLE := "^{s}"
global TOGGLE_REPEAT := "^{r}"
global SEEK_FORWARD:= "+{Right}"
global SEEK_BACKW := "+{Left}"
; hotkeys
global toggle_pause_bind
global vol_up_bind
global vol_down_bind
global next_track_bind
global prev_track_bind
global like_track_bind
global toggle_shuffle_bind
global toggle_repeat_bind
global seek_forwd_bind
global seek_backw_bind
global settingsGuiBind
global binds := ["toggle_pause_bind", "vol_up_bind", "vol_down_bind", "next_track_bind", "prev_track_bind", "like_track_bind", "toggle_shuffle_bind", "toggle_repeat_bind", "seek_forwd_bind", "seek_backw_bind", "settingsGuiBind"]
if FileExist("spotifyControls.ini")
{
for index, bind in binds
{
IniRead, key, spotifyControls.ini, Script Settings, %bind%
if (key != "ERROR")
{
Hotkey, %key%, %bind%
}
}
}
else
{
settingsGUI()
}
return
ButtonSave:
Gui, Submit
for index, bind in binds
{
actual_bind := % %bind%
if (actual_bind)
{
IniWrite, %actual_bind%, spotifyControls.ini, Script Settings, %bind%
}
}
Gui, Destroy
reload
return
GuiClose:
Gui, Destroy
ExitApp
return
GuiEscape:
Gui, Cancel
Gui, Destroy
reload
return
; actual hotkey section
toggle_pause_bind:
sendKeyTospotify(TOGGLE_PAUSE)
return
vol_up_bind:
sendKeyTospotify(VOLUME_UP)
return
vol_down_bind:
sendKeyTospotify(VOLUME_DOWN)
return
next_track_bind:
sendKeyTospotify(NEXT_TRACK)
return
prev_track_bind:
sendKeyTospotify(PREV_TRACK)
return
like_track_bind:
sendKeyTospotify(LIKE_TRACK)
return
toggle_shuffle_bind:
sendKeyTospotify(TOGGLE_SHUFFLE)
return
toggle_repeat_bind:
sendKeyTospotify(TOGGLE_REPEAT)
return
seek_forwd_bind:
sendKeyTospotify(SEEK_FORWARD)
return
seek_backw_bind:
sendKeyTospotify(SEEK_BACKW)
return
settingsGuiBind:
settingsGUI()
return
; functions
sendKeyTospotify( key )
{
; if spotify window doesnt exist, launch
if (!WinExist("ahk_exe spotify.exe"))
{
; TODO fix issue where if spotify doesnt pop up in time focus is lost
prev := WinActive("A")
RunWait, "C:\Users\win10\AppData\Roaming\Spotify\Spotify.exe"
SendInput, %key%
; WinSet, Bottom,, ahk_exe spotify.exe
Sleep, 150 ; wait for spotify to pop up
WinActivate, ahk_id %prev%
}
else
{
minimized := -1
WinGet, min_max_state, MinMax, ahk_exe spotify.exe
if (win_max_state = minimized)
{
prev := WinActive("A")
; WinSet, Bottom,, ahk_exe spotify.exe
WinRestore, ahk_exe spotify.exe ; ?
WinActivate, ahk_id %prev%
ControlFocus, Chrome_RenderWidgetHostHWND1, ahk_exe spotify.exe
ControlSend,, %key%, ahk_exe spotify
}
; ControlSend breaks for some reason if the window is focused so separate check for that
else if (WinActive("ahk_exe spotify.exe"))
{
SendInput, %key%
}
else
{
; TODO fix bug that makes a window affected by "WinSet, Bottom.." not recieve tabbed out keystrokes... in that case controlfocus raises errorlevel
ControlFocus, Chrome_RenderWidgetHostHWND1, ahk_exe spotify.exe
ControlSend,, %key%, ahk_exe spotify.exe
}
}
}
settingsGUI()
{
Gui, Font, s11 q5 cc2c5c9, Whitney
Gui, Color, c1e2124
Gui, Add, Button, x100 y350 w120 h25 +Default, Save
for index, bind in binds
{
result := "y" . (30*(index-1)+10)
Gui Add, Text, x10 %result% w150 h25 +0x200, %bind%
result2 := "v" . bind
Gui Add, Hotkey, x220 %result% w75 h25 %result2%, %bind%
}
Gui, Show, w320, Settings
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment