Skip to content

Commit

Permalink
Removed need for controller function
Browse files Browse the repository at this point in the history
Merged both on-input functions into one. Made a bound function object to handle controller input, so it can be used with the Hotkey command.
  • Loading branch information
Green-Bat authored Mar 16, 2020
1 parent f52d078 commit 7ae0f2a
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions OBS_Scene_Switcher.ahk
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#Warn
#NoEnv
#SingleInstance, Force

keybdkey := "F13" ; This determines what gets sent from keyboard/mouse inputs
controllerkey := "F14" ; This determines what gets sent from controller inputs

; From the AHK documentation, used to auto-detect the joystick number
JoystickNumber := 0
if (JoystickNumber <= 0){
Expand All @@ -12,7 +16,7 @@ if (JoystickNumber <= 0){
}

if (JoystickNumber <= 0 ){
MsgBox, Could not detect any joysticks!
MsgBox, 16, OBS Scene Switcher, % "ERROR: Could not detect any joysticks! Program will exit."
ExitApp
}
}
Expand All @@ -35,15 +39,17 @@ if (axis_3 != 0){ ; Only create the variables if the axis exists for the control
previousJoyR := ""
}

cInput := Func("OnInput").Bind(controllerkey) ; Create bound function object and bind the controller key to it, so it can be used with the Hotkey command

Loop, % joy_buttons { ; Turns all the controller buttons into hotkeys
Hotkey, % JoystickNumber "Joy" A_Index, OnGamepadUsed, On
Hotkey, % JoystickNumber "Joy" A_Index, % cInput, 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("OnKeyPressed")
ih.OnKeyDown := Func("OnInput").Bind(keybdkey)
ih.Start()
return

Expand All @@ -53,7 +59,7 @@ return
~RButton::
~LButton::
Sleep, 150
OnKeyPressed()
OnInput(keybdkey)
return

;**********************************/Timers/*******************************************************
Expand All @@ -62,26 +68,26 @@ 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)){
OnKeyPressed()
OnInput(keybdkey)
MouseGetPos, sx, sy
}
}
return

check_axes:
check_axes: ; The subroutine that checks controller axes
joyX := GetKeyState(JoystickNumber . "JoyX")
joyY := GetKeyState(JoystickNumber . "JoyY")
if (!IsValueSimilar(previousJoyX, joyX) || !IsValueSimilar(previousJoyY, joyY)){
OnGamepadUsed()
cInput.Call()
previousJoyX := joyX
previousJoyY := joyY
}

if (axis_3 != 0){ ; Only check the state if the axis exists
joyZ := GetKeyState(JoystickNumber . "JoyZ")
joyR := GetKeyState(JoystickNumber . "JoyR")
if (!IsValueSimilar(previousJoyZ, joyZ) || !IsValueSimilar(previousJoyR, joyR)){
OnGamepadUsed()
if (!IsValueSimilar(previousJoyR, joyR) || !IsValueSimilar(previousJoyZ, joyZ)){
cInput.Call()
previousJoyZ := joyZ
previousJoyR := joyR
}
Expand All @@ -90,29 +96,22 @@ check_axes:
if (dpad != 0){ ; Only check POV state if it exists
joy_p := GetKeyState(JoyStickNumber . "JoyPOV")
if(joy_p != -1 && joy_p != "")
OnGamepadUsed()
cInput.Call()
}
return

;**********************************/Functions/*******************************************************

OnKeyPressed(){ ; A function that sends a keystroke to OBS when a keyboard key is pressed/the mouse has moved
Critical
SetKeyDelay, 10
SetTitleMatchMode, 2
ControlSend, ahk_parent, {Blind}{F13}, OBS ; 'F13' can be changed to whatever key you want
}

OnGamepadUsed(){ ; A function that sends a keystroke to OBS when a controller button is pressed/an analog stick has moved
OnInput(key){
Critical
SetKeyDelay, 10
SetTitleMatchMode, 2
ControlSend, ahk_parent, {Blind}{F14}, OBS ; 'F14' can be changed to whatever key you want
ControlSend, ahk_parent, {Blind}{%key%}, OBS
}

IsValueSimilar(var1, var2){ ; A function that compares the previous and current states of the controller axes
return ((var1 - 7) <= var2) && ((var1 + 7) >= var2)
}

;****************************************************************************************************
; Kill-switch Shift+F4
+F4::ExitApp

0 comments on commit 7ae0f2a

Please sign in to comment.