-
Notifications
You must be signed in to change notification settings - Fork 0
/
OBS_Scene_Switcher.ahk
558 lines (505 loc) · 19.1 KB
/
OBS_Scene_Switcher.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#Include <JSON>
/*
* OBS Scene Switcher
* By GreenBat
* Version:
* 2.7.4 (Last updated 30/10/2023)
* https://github.com/Green-Bat/OBS-Scene-Switcher
* Requirements:
* AutoHotkey v1.1.32.00+
* JSON.ahk
*/
#NoEnv
#SingleInstance, Force
ListLines, Off
SetBatchLines, 20ms
CoordMode, Mouse, Screen
SetWorkingDir, % A_ScriptDir
settingsfile := FileOpen(A_ScriptDir "\settings.JSON" , "r")
if !(IsObject(settingsfile)){
MsgBox, 16, OBS Scene Switcher, ERROR: Failed to load settings file! Please make sure it's in the same directory as the program's exe file.
ExitApp
}
global settings := JSON.Load(settingsfile.Read())
, HasStarted := false, keybdkey := controllerkey := ""
, JoystickNumber := settings.JoyNum
settingsfile.Close()
GroupAdd, obs, OBS ahk_exe obs32.exe
GroupAdd, obs, OBS ahk_exe obs64.exe
OnMessage(0x44, "CenterMsgBox") ; Center any MsgBox before it appears
Menu, OptionsMenu, Add, Create profile, CreateProfile
Menu, OptionsMenu, Add, Edit profile, EditProfile
Menu, OptionsMenu, Add, Delete profile, DeleteProfile
Menu, OptionsMenu, Add, Hotkey Setter, HotkeySetter
Menu, MenuBar, Add, &Options, :OptionsMenu
Gui, Main:New, +HwndMainHwnd, OBS Scene Switcher
Gui, Main:Menu, MenuBar
Gui, Font, s11
Gui, Main:Add, Text, xm ym+5, Profiles
Gui, Font
Gui, Main:Add, DDL, xp yp+20 vprofile gchangeprofile
for savedprofile in settings.Profiles { ; Fill up the DDL with the saved profiles
GuiControl,, profile, % savedprofile
}
GuiControl, ChooseString, profile, % settings.LastProfile ; Choose whichever profile was last chosen
Gui, Main:Add, Text, xp yp+30, Keyboard Key
Gui, Main:Add, Edit, xp yp+20 w140 +Disabled vsavedkbd, % ChangeHkey(keybdkey := settings.Profiles[settings.LastProfile][1])
Gui, Main:Add, Text, xp yp+30 wp, Controller Key
Gui, Main:Add, Edit, xp yp+20 wp +Disabled vsavedctrlr, % ChangeHkey(controllerkey := settings.Profiles[settings.LastProfile][2])
Gui, Font, s11
Gui, Main:Add, Button, xp+170 yp-90 wp-20 h30 vStartButton gStart, Start
Gui, Main:Add, Button, xp yp+50 wp hp +Disabled vStopButton gStop, Stop
Gui, Main:Add, Text, xp-70 yp-80, Controller:
Gui, Main:Add, DDL, xp+70 yp-2.5 wp+125 vnames gchangectr
Gui, Main:Show, W380 H175
if !(CheckController())
ExitApp
SetTimer, UpdateController, 3000
return
; Make the mouse buttons hotkeys only if the start button was pressed
#If HasStarted
~XButton2::
~XButton1::
~MButton::
~RButton::
~LButton::
Sleep, 150
OnInput(keybdkey)
return
#If
;****************************************************************** - G-LABELS - ******************************************************************
;******************************************************************| - PROFILE CREATION/DELETION - |******************************************************************
CreateProfile:
Gui, Main:+OwnDialogs
; Stop the scene switcher if it was started
if (HasStarted)
gosub, Stop
WinGetPos, x, y, w, h, ahk_id %MainHwnd%
Loop {
InputBox, ProfileName, Profile Creation, Enter a name for the profile,, 200, 150, x + ((w/2) - 100), y + ((h/2) - 75)
if (settings.Profiles.HasKey(ProfileName)){
MsgBox, 48, OBS Scene Switcher, This profile name already exists, please choose another one
ProfileName := ""
continue
}
} until (ProfileName || ErrorLevel)
if (ErrorLevel == 1)
return
Gui, Main:+Disabled
Gui, HKey:New, +HwndHwnd2 +OwnerMain, Hotkey Selection
Gui, Font, s9
Gui, HKey:Add, Text, xm ym, Keyboard/mouse hotkey ( to be sent with keyboard/mouse input )
Gui, HKey:Add, Hotkey, xp yp+20 w110 h20 vkbdHkey +HwndKID
Gui, Hkey:Add, Edit, xp yp wp hp Hidden vAltKHkey
Gui, Hkey:Add, Checkbox, xp+130 yp+5 vKWin, Use Windows key
Gui, Hkey:Add, Button, xp+115 yp-5 vTypeK gTypeHkey, Type manually
Gui, HKey:Add, Text, xp-245 yp+25, Controller hotkey ( to be sent with controller input )
Gui, HKey:Add, Hotkey, xp yp+20 w110 h20 vCtrlrHkey +HwndCID
Gui, Hkey:Add, Edit, xp yp wp hp Hidden vAltCHkey
Gui, Hkey:Add, Checkbox, xp+130 yp+5 vCWin, Use Windows key
Gui, Hkey:Add, Button, xp+115 yp-5 vTypeC gTypeHkey, Type manually
Gui, HKey:Add, Button, xp-120 yp+30 wp hp gSetHKeys, Submit
Gui, HKey:Show, % "X" (x+(w/2) - 175) " Y" (y + (h/2) - 65) " W350 H130"
Save()
return
SetHKeys:
Gui, HKey:+OwnDialogs
Gui, HKey:Submit, NoHide
key1 := (kbdHkey) ? kbdHkey : AltKHkey
key2 := (CtrlrHkey) ? CtrlrHkey : AltCHkey
if !(key1){
MsgBox, 48, Hotkey Selection, You forgot to add a hotkey for keyboard input
return
}
if !(key2){
MsgBox, 48, Hotkey Selection, You forgot to add a hotkey for controller input
return
}
; Add the windows key if the user checked the checkbox
if (KWin)
key1 := "#" . key1
if (CWin)
key2 := "#" . key2
settings.Profiles[ProfileName] := [keybdkey := key1, controllerkey := key2]
, settings.LastProfile := ProfileName
; Update the main GUI
GuiControl, Main:, savedkbd, % ChangeHkey(key1)
GuiControl, Main:, savedctrlr, % ChangeHkey(key2)
; Empty the DDL, refill it, and choose the profile the user just created
GuiControl, Main:, profile, |
for savedprofile in settings.Profiles {
GuiControl, Main:, profile, % (savedprofile == ProfileName) ? ProfileName "||" : savedprofile
}
gosub, HkeyGuiClose
return
TypeHkey(CtrlHwnd, GuiEvent, EventInfo, Errlvl:=""){
GuiControlGet, BtnV, Name, % CtrlHwnd ; Get the associated var of the button
GuiControlGet, BtnTxt,, % CtrlHwnd ; Get the text of the button
; Figure out which button was pressed using it's associated var and set the variables accordingly
Switch (BtnV)
{
Case "TypeK":
Control1 := "kbdHkey"
Control2 := "AltKHkey"
Check := "KWin"
Case "TypeC":
Control1 := "CtrlrHkey"
Control2 := "AltCHkey"
Check := "CWin"
}
; Hide and uncheck the checkbox
GuiControl, Hide, % Check
GuiControl,, % Check, 0
; Revert the changes if the button is pressed again
if (BtnTxt == "Type as Hotkey"){
Revert(Control1, Control2, BtnV, Check)
return
}
; Change the text of the button
GuiControl,, %BtnV%, Type as Hotkey
GuiControl, Hide, % Control1
GuiControl,, % Control1 ; Empty the hotkey control
GuiControl, Show, % Control2
GuiControl, Focus, % Control2
return
}
Revert(Control1, Control2, BtnV, Check){
GuiControl,, %BtnV%, Type manually
GuiControl, Show, % Check
GuiControl, Show, % Control1
GuiControl, Hide, % Control2
GuiControl,, % Control2 ; Empty the edit control
GuiControl, Focus, % Control1
return
}
HKeyGuiClose:
Gui, HKey:Destroy
Gui, Main:-Disabled
WinActivate, ahk_id %MainHwnd%
return
;***************************************************************************************************************************************************
EditProfile:
Gui, Main:+OwnDialogs
if !(settings.LastProfile){
MsgBox, 48, OBS Scene Switcher, No profile is selected
return
}
if (HasStarted)
gosub, Stop
WinGetPos, x, y, w, h, ahk_id %MainHwnd%
Gui, Main:+Disabled
Gui, HKey:New, +HwndHwnd2 +OwnerMain, Hotkey Selection
Gui, Font, s9
Gui, HKey:Add, Text, xm ym, Keyboard/mouse hotkey ( to be sent with keyboard/mouse input )
Gui, HKey:Add, Hotkey, xp yp+20 w110 h20 vkbdHkey +HwndKID, % settings.Profiles[settings.LastProfile][1]
Gui, Hkey:Add, Edit, xp yp wp hp Hidden vAltKHkey
Gui, Hkey:Add, Checkbox, xp+130 yp+5 vKWin, Use Windows key
Gui, Hkey:Add, Button, xp+115 yp-5 vTypeK gTypeHkey, Type manually
Gui, HKey:Add, Text, xp-245 yp+25, Controller hotkey ( to be sent with controller input )
Gui, HKey:Add, Hotkey, xp yp+20 w110 h20 vCtrlrHkey +HwndCID, % settings.Profiles[settings.LastProfile][2]
Gui, Hkey:Add, Edit, xp yp wp hp Hidden vAltCHkey
Gui, Hkey:Add, Checkbox, xp+130 yp+5 vCWin, Use Windows key
Gui, Hkey:Add, Button, xp+115 yp-5 vTypeC gTypeHkey, Type manually
Gui, HKey:Add, Button, xp-120 yp+30 wp hp gSetHKeys, Submit
Gui, HKey:Show, % "X" (x+(w/2) - 175) " Y" (y + (h/2) - 65) " W350 H130"
ProfileName := settings.LastProfile
Save()
return
;***************************************************************************************************************************************************
DeleteProfile:
Gui, Main:+OwnDialogs
if !(settings.LastProfile){
MsgBox, 48, OBS Scene Switcher, No profile is selected
return
}
MsgBox, 52, OBS Scene Switcher, % "Are you sure you want to delete this profile: """ settings.LastProfile """"
IfMsgBox, No
return
if (HasStarted)
gosub, Stop
Gui, Main:Submit, NoHide
settings.Profiles.Delete(profile)
settings.LastProfile := ""
; Empty the DDL and refill it
GuiControl,, profile, |
for savedprofile in settings.Profiles {
GuiControl,, profile, % savedprofile
}
GuiControl,, savedkbd, % ""
GuiControl,, savedctrlr, % ""
Save()
return
;***************************************************************************************************************************************************
Start:
Gui, Main:Submit, NoHide
Gui, Main:+OwnDialogs
if !(settings.Profiles.Count()){
gosub, CreateProfile
return
} else if !(profile){
MsgBox, 0, OBS SceneSwitcher, % "You forgot to choose a profile silly"
return
}
GuiControlGet, controller,, names
if !(controller)
return
GuiControl, Disable, StartButton
GuiControl, Enable, StopButton
HasStarted := true
SetTimer, check_mouse, 60 ; A subroutine that checks mouse movement
SetTimer, check_axes, 90 ; A subroutine that checks the state of the various axes/ POV buttons of the controller
MouseGetPos, sx, sy ; Get the mouse coords for later
joy_info := GetKeyState(JoystickNumber . "JoyInfo")
, axis_3 := InStr(joy_info, "Z", true) ; Checks if the third axis exists for the controller
, dpad := InStr(joy_info, "P", true) ; Checks if the POV buttons exist fot the controller
, previousJoyX := previousJoyY := ""
; Only create the variables if the axis exists for the controller
if (axis_3)
previousJoyZ := previousJoyR := ""
; Turns all the controller buttons into hotkeys
funcobj := Func("OnInput").Bind(controllerkey)
Loop, % GetKeyState(JoystickNumber . "JoyButtons") {
Hotkey, % JoystickNumber "Joy" A_Index, % funcobj, On
}
; An input hook used for intercepting all keyboard keys (excluding modifiers)
ih := InputHook("V L0 I")
ih.KeyOpt("{All}", "N")
ih.KeyOpt("{LCtrl}{RCtrl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}", "-N")
ih.OnKeyDown := Func("OnInput").Bind(keybdkey)
ih.Start()
return
;***************************************************************************************************************************************************
Stop: ; Turns off all timers and hotkeys, and stops the input hook
GuiControl, Disable, StopButton
GuiControl, Enable, StartButton
HasStarted := false
SetTimer, check_mouse, Off
SetTimer, check_axes, Off
Loop, % GetKeyState(JoystickNumber . "JoyButtons") {
Hotkey, % JoystickNumber "Joy" A_Index, % funcobj, Off
}
ih.Stop()
return
;***************************************************************************************************************************************************
changeprofile:
if (HasStarted)
gosub, Stop
Gui, Main:Submit, NoHide
settings.LastProfile := profile
GuiControl,, savedkbd, % ChangeHkey(keybdkey := settings.Profiles[profile][1])
GuiControl,, savedctrlr, % ChangeHkey(controllerkey := settings.Profiles[profile][2])
Save()
return
;******************************************************************| - HOTKEY SETTER - |******************************************************************
; Used to send unusal hotkeys, for example, F13-F24
; It'll bind whatever key the user enters to the F1 key and unbind it once the user closes the window
HotkeySetter:
WinGetPos, x, y, w, h, ahk_id %MainHwnd%
Gui, HkeySet:New, +AlwaysOnTop +HwndHwnd3, Hotkey Setter
Gui, Font, s10
Gui, HkeySet:Add, Text, xm ym,
( LTrim
This is used to send unusual keys (e.g. F13-F24).
It will temporarily remap the F1 key to whatever key you type in.
)
Gui, HkeySet:Add, Edit, xp yp+40 w80 vOddHkey
Gui, HkeySet:Add, Button, xp+190 yp-2.5 wp-10 gSet, Set
Gui, HkeySet:Show, % "X" (x+(w/2) - 200) " Y" (y + (h/2) - 40) " W400 H80"
count := 0
return
Set:
count++
Gui, HkeySet:+OwnDialogs
MsgBox,, Hotkey Setter, The key you just entered will now be temporarily remapped to the F1 key
Hotkey, $F1, SendOddKey, On
return
SendOddKey:
Gui, HkeySet:Submit, NoHide
Send, {%OddHkey%}
return
HkeySetGuiClose:
Gui, HkeySet:+OwnDialogs
Hotkey, F1, SendOddKey, Off
if (count > 0){
MsgBox,, Hotkey Setter, The F1 key now has its normal functionality
count := ""
}
Gui, HkeySet:Destroy
WinActivate, ahk_id %MainHwnd%
return
;***************************************************************************************************************************************************
changectr:
GuiControlGet, newname,, names
settings.JoyName := newname
GuiControl, +AltSubmit, names
GuiControlGet, newnum,, names
if !(GetKeyState(settings.JoyNums[newnum] . "JoyName")){
CheckController()
return
}
JoystickNumber := settings.JoyNums[newnum]
GuiControl, -AltSubmit, names
Save()
return
;****************************************************************** - TIMERS - ******************************************************************
check_mouse: ; The subroutine that checks mouse movement
MouseGetPos, cx, cy
if (cx != sx or cy != sy){
if (cx > (sx+50) or cx < (sx-50) or cy > (sy+50) or cy < (sy-50)){
OnInput(keybdkey)
MouseGetPos, sx, sy
}
}
return
;***************************************************************************************************************************************************
check_axes:
joyX := GetKeyState(JoystickNumber . "JoyX")
, joyY := GetKeyState(JoystickNumber . "JoyY")
if !(IsValueSimilar(previousJoyX, joyX) && IsValueSimilar(previousJoyY, joyY)){
OnInput(controllerkey)
previousJoyX := joyX
, previousJoyY := joyY
}
; Only check the state if the axis exists
if (axis_3){
joyZ := GetKeyState(JoystickNumber . "JoyZ")
, joyR := GetKeyState(JoystickNumber . "JoyR")
if !(IsValueSimilar(previousJoyR, joyR) && IsValueSimilar(previousJoyZ, joyZ)){
OnInput(controllerkey)
previousJoyZ := joyZ
, previousJoyR := joyR
}
}
; Only check POV state if it exists
if (dpad){
joy_p := GetKeyState(JoyStickNumber . "JoyPOV")
if (joy_p != -1 && joy_p != "")
OnInput(controllerkey)
}
return
;****************************************************************** - FUNCTIONS - ******************************************************************
OnInput(key){
Critical
Lastkey := StrSplit(key, ["^", "!", "+", "#"]) ; Split the key by modifier to get the key without any modifiers
key := StrReplace(key, Lastkey[Lastkey.MaxIndex()]) ; Remove the key from the string and keep the modifiers
SetTitleMatchMode, 2
ControlSend, ahk_parent, % "{Blind}" key "{" Lastkey[Lastkey.MaxIndex()] "}", OBS ahk_group obs
}
;***************************************************************************************************************************************************
CheckController(){ ; From the AHK documentation, used to auto-detect the joystick number
name := "", settings.JoyNums := []
Loop, 16 {
if (GetKeyState(A_Index . "JoyName")){
name .= GetKeyState(A_Index . "JoyName") "|"
settings.JoyNums.Push(A_Index)
}
}
if !(name){
Gui, +OwnDialogs
MsgBox, 16, OBS Scene Switcher, % "ERROR: Could not detect any joysticks! Please connect one and try again"
return false
}
GuiControl,, names, % name
JoystickNumber := settings.JoyNums[1]
if (settings.JoyName)
GuiControl, ChooseString, names, % settings.JoyName
else {
GuiControl, Choose, names, 1
GuiControlGet, firstctrl,, names
settings.JoyName := firstctrl
}
return true
}
UpdateController(){
Gui, Main:Default
name := "", current := [] ; current controllers recognized by the pc itself not the ones in the list
Loop, 16 {
controller := GetKeyState(A_Index . "JoyName")
if (controller){
name .= controller "|"
current.Push(A_Index)
}
}
if (current.Length() != settings.JoyNums.Length()){
gosub, update
return
}
for k, v in settings.JoyNums {
if (v != current[k]){
gosub, update
break
}
}
return
update:
GuiControl,, names, % "|" name
JoystickNumber := current[1]
for k, v in current {
settings.JoyNums[k] := v
}
GuiControl, Choose, names, 1
GuiControlGet, firstctrl,, names
settings.JoyName := firstctrl
return
}
;***************************************************************************************************************************************************
ChangeHkey(RawHkey){ ; Function that changes modifier symbols into literal text for display purposes in the main GUI window
static Modifiers := {"^": "Ctrl + ", "!": "Alt + ", "#": "Win + ", "+": "Shift + "}
ChangedHkey := ""
, LastKey := StrSplit(RawHkey, ["^", "!", "+", "#"]) ; Split the key by modifier to get the key without any modifiers
for symbol, modifier in Modifiers {
if (InStr(RawHkey, symbol))
ChangedHkey .= modifier
}
ChangedHkey .= LastKey[LastKey.MaxIndex()]
return ChangedHkey
}
;***************************************************************************************************************************************************
IsValueSimilar(var1, var2){ ; A function that compares the previous and current states of the controller axes
return ((var1 - 7) <= var2) && ((var1 + 7) >= var2)
}
;***************************************************************************************************************************************************
CenterMsgBox(P){
global MainHwnd
if (P == 1027){
Process, Exist ; Get the PID of program, which is set to ErrorLevel
DetectHiddenWindows, On
WinGetPos, x1, y1, w1, h1, ahk_id %MainHwnd%
if WinExist("ahk_class #32770 ahk_pid " ErrorLevel){
WinGetPos,,, w2, h2 ; Get dimensions of the MsgBox
WinMove, x1 + ((w1/2) - (w2/2)), y1 + ((h1/2) - (h2/2))
}
}
DetectHiddenWindows, Off
}
;***************************************************************************************************************************************************
ToggleStart(){
static toggle := true
Gui, Main:Default
gosub, % (toggle := !toggle) ? "Stop" : "Start"
}
;***************************************************************************************************************************************************
Save(){
global settingsfile
settingsfile := FileOpen(A_ScriptDir "\settings.JSON" , "w")
if !(IsObject(settingsfile)){
MsgBox, 16, Savefile Replacer,
( LTrim
ERROR: Failed to load settings file! Please make sure it's in the correct directory.
You can use Shift+F4 to force close the program, but any changes you made will not be saved.
)
return
}
settingsfile.Seek(0)
settingsfile.Write((JSON.Dump(settings,, 4)))
settingsfile.Close()
}
;***************************************************************************************************************************************************
MainGuiClose:
Save()
ExitApp
; Toggle start/stop
!s::ToggleStart()
;~ !r::Reload
; Kill-switch Shift+F4
+F4::ExitApp