Jump to content

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

Can't make simple Caps Lock Toggle to axis work, please help!


  • Please log in to reply
2 replies to this topic
stringue
  • Members
  • 4 posts
  • Last active: Oct 29 2015 09:08 AM
  • Joined: 23 Mar 2015

I wish to bind Caps Lock to a pedal axis so that each time the axis is depressed that caps lock is then toggled.

 

Press pedal - > Caps Lock toggled only once

 

I have read the helpful tutorials and can construct a script to make caps lock be held while the pedal is held but regarding toggling the caps lock I cannot make a script to toggle it only once, all I have tried results in repeat toggling of caps lock.

 

Please Help point me to the correct commands!

 

I am using a script that also adds shift functionality to some other axis that works fine, and I posted a while ago, but wished for the stated caps lock functionality as well that I am having trouble with. The script is below.

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


#Persistent  ; Keep this script running until the user explicitly exits it.
SetTimer, WatchAxis, 5
SetStoreCapslockMode, Off
return

; AHK Script binding Caps and Shift to Pedal and Joy axis for Decouple Toggle and End Axis Boost

WatchAxis:
GetKeyState, 2JoyX, 2JoyX  ; T1600m H Strafe Axis
GetKeyState, 2JoyY, 2JoyY  ; T1600m V Strafe Axis
GetKeyState, 1JoyU, 1JoyU  ; G27 Clutch Axis
GetKeyState, 1JoyR, 1JoyR  ; G27 Brake Axis
GetKeyState, 1JoyZ, 1JoyZ  ; G27 Accelerator Axis

KeyToHoldDownPrev = %KeyToHoldDown%  ; Prev now holds the key that was down before (if any)

GetKeyState, state, CapsLock

pedaldown:
if (1JoyU < 30 and state = U)
{
	SetCapsLockState, AlwaysOn
	goto, pedalup
}
else if (1JoyU < 30 and state = D)
{
	SetCapsLockState, AlwaysOff
	goto, pedalup
}
else
{
	goto, pedalup
}
return

pedalup:
if (1JoyU = 100)
{
	goto, pedaldown
}
return



if 2JoyX = 0
	KeyToHoldDown = Shift
else if 2JoyX = 100
	KeyToHoldDown = Shift
else if 2JoyY = 0
	KeyToHoldDown = Shift
else if 2JoyY = 100
	KeyToHoldDown = Shift
else if 1JoyR < 30
	KeyToHoldDown = Shift
else if 1JoyZ = 0
	KeyToHoldDown = Shift
else
	KeyToHoldDown =


if KeyToHoldDown = %KeyToHoldDownPrev%
return

; Otherwise
SetKeyDelay -1 ; Avoids keystroke  delays
if KeyToHoldDownPrev   ; There is a previous key to release.
    Send, {Shift up}  ; Release it.
if KeyToHoldDown   ; If key to press down
    Send, {Shift down} ; press it down
return


I have tried using Send {CapsLock down}{CapsLock up} without the goto labels which I see the help pages advise against, but can't seem to stop that when the axis is depressed the capslock function keeps getting sent over and over again when I only want it to run once per depression. So I tried incorporating GetKeyState and hopefully selectively fire it off, but it's all been dismal failure so far.

 

Any pointers would be great.

 

Thanks!



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

I believe the problem comes about from your SetTimer being so fast, and just a simple check being performed. I recommend using some flags to wait for the pedal to be moved back to a the neutral position.Try this (the variable freeze is a variable I created to use as a flag:

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.


#Persistent ; Keep this script running until the user explicitly exits it.
SetTimer, WatchAxis, 5
SetStoreCapslockMode, Off
 
freeze=0 ; include this in your auto-execute section
return
 
WatchAxis:
GetKeyState, 2JoyX, 2JoyX ; T1600m H Strafe Axis
GetKeyState, 2JoyY, 2JoyY ; T1600m V Strafe Axis
GetKeyState, 1JoyU, 1JoyU ; G27 Clutch Axis
GetKeyState, 1JoyR, 1JoyR ; G27 Brake Axis
GetKeyState, 1JoyZ, 1JoyZ ; G27 Accelerator Axis

KeyToHoldDownPrev = %KeyToHoldDown% ; Prev now holds the key that was down before (if any)

; GetKeyState, state, CapsLock, Τ ; this syntax retrieves D and U
state:=GetKeyState("CapsLock","T")
 
; technically this label isn't necessary, but I've left it here for clarity
pedaldown: ; I assume 0 means it's all the way pressed and 100 means it's all the way released
if (1JoyU < 30 and state = 0 and freeze=0)
{
    SetCapsLockState, AlwaysOn
    freeze=1
}
else if (1JoyU < 30 and state = 1 and freeze=0)
{
    SetCapsLockState, AlwaysOff
    freeze=1
}
else if (1JoyU > 90 and freeze=1) ; could be 1JoyU=100 if you wish
{
freeze=0
}
 
; rest of your code. Notice I got rid of the Goto pedalup commands
; you should not need the pedalup label or its code anymore

Also, I noticed that you have many lines beneath your pedaldown: and pedalup: sections. Those lines aren't being executed right now because the WatchAxis: commands only run through to the first return it encounters.

 

Try the above changes out and see how things run. I don't have a pedal to try the script with.

 

Edit: I pulled out my Xbox controller and tried the script. I made a change to it. For one, you should have been using state="D" and state="U" because you have that equality inside of parentheses, so it was being evaluated as an expression on the right side.) However, I got rid of that in the code above because: I forgot about the CapsLock state being toggled, not held down. So I tried to change that into toggle mode. The GetKeyState, state, CapsLock, T command did not work for me, for whatever reason. But if I change it to the function state:=GetKeyState("CapsLock","T") it works for me, so I made that change above.

 

This is the script I tested. I changed what axis I play with, and have a tooltip for monitoring things, and it seems to work fine for me. It should be quite similar to the code above.

 

Spoiler

 

I am a bit concerned though that you wanted instead to have the capslock key held down and not just trigger being caps. Your code was meant to trigger or toggle the state of capslock, not hold down the key (which may be why you used the non-toggle version of GetKeyState). If you want that, use the non-toggle version of GetKeyState but also use Send {CapsLock down} and Send {CapsLock Up} in place of SetCapsLockState, AlwaysOn and SetCapsLockState, AlwaysOff respectively.



stringue
  • Members
  • 4 posts
  • Last active: Oct 29 2015 09:08 AM
  • Joined: 23 Mar 2015

Just a quick one to say thankyou for the fantastic help Exaskryz.

 

You were right I wanted caps toggled rather than pressed which your script provides.

 

Will spend a bit of time later understanding how it works right now, thanks for all the help!