Perfecting a AHK Toggle Crouch System
Posted: 2014-09-12 04:24
LATEST VERSIONS
[R-CON]Mats391: link to download
Use config.ini to set your keybinds. Refer to AutoHotkey documentation for denoting special keys like 'LCtrl', 'RAlt', 'RWin' if you are going to use any of those keys. This is the best solution to the crouch toggle problem I have seen yet.
============
OLD STUFF (Skip everything below if you just want to use the most up-to-date crouch toggle solutions)
(For those who don't know what this thread is about: AutoHotkey is a piece of software that allows the user to alter the way that keyboard/mouse inputs are directed into BF2.exe without actually changing the operation of BF2.exe. It allows us to "fake" holding down a keystroke for a certain amount of time without actually doing it. It has a lot of other uses as well for other games/software and is very customizable.)
I have tried out the AHK toggle crouches that some other people have made for PR. A lot of these AHKs make the crouch toggle work in the most basic form, but they all invariably cause some other aspect of the game to malfunction in some way. Most of them cause the in-game chat to seize up and become unusable, or to get stuck into other kinds of loops. With the community's help, I want to make a fully functioning crouch toggle for PR:BF2 that does not interfere with in-game chatting or using applications outside of BF2.exe. When it is perfected, we can post the code and format it so that laymen can easily customize it to their own personal choice of keybindings.
The best .ahk I have seen made is the one created by [R-CON]Mats391: link to post. It's pretty impressive.
There were some lines in Mats391's code that I didn't understand entirely, but I was able to adapt it to my own layout without much trouble. (I use a QWERTY keyboard. I use 'C' for crouch, and 'LAlt' for prone.)
The only problem I had with Mats391's AHK is that sometimes the in-game chat totally seizes up, even though it works perfectly fine at the start. For example, when I press 'K' to type in team chat, I can only type letters by pressing 'T', 'Enter', 'E', 'Enter', 'S', 'Enter', 'T', 'Enter', and then a final 'Enter' to put it in chat. The end result in chat is: "t?st". Why it behaves like this is a total mystery to me. Once the problem arises, it stays for as long as 2 hours and goes away with no discernible pattern. It happens basically every match. Also, for some reason, BF2 doesn't understand the 'e' keystroke when this thing happens and makes it a '?' mark instead.
??:
This kind of problem happens to me with basically any kind of autohotkey crouch toggle that I try, and it's a complete mystery to me why it happens and behaves in such an odd way.
Now let me share with you the AHK I am currently working on:(I use the 'C' key for crouch, 'LAlt' for prone, and 'v' for map. To allow the code to work properly, 'RShift' is assigned in-game as my crouch key and the code simulates a crouch using 'RShift' instead of 'C'.)
If you are also a crouch toggle user, feel free to try this out, customize it to your keybinds, and let me know what works and what doesn't.
I'm not totally familiar with all of the AutoHotkey language so I'm not totally sure what the best way is to suspend the AHK outside of BF2.exe
Thanks for reading!
============================
EXPLANATION OF THE AHK
[R-CON]Mats391: link to download
Use config.ini to set your keybinds. Refer to AutoHotkey documentation for denoting special keys like 'LCtrl', 'RAlt', 'RWin' if you are going to use any of those keys. This is the best solution to the crouch toggle problem I have seen yet.
============
OLD STUFF (Skip everything below if you just want to use the most up-to-date crouch toggle solutions)
(For those who don't know what this thread is about: AutoHotkey is a piece of software that allows the user to alter the way that keyboard/mouse inputs are directed into BF2.exe without actually changing the operation of BF2.exe. It allows us to "fake" holding down a keystroke for a certain amount of time without actually doing it. It has a lot of other uses as well for other games/software and is very customizable.)
I have tried out the AHK toggle crouches that some other people have made for PR. A lot of these AHKs make the crouch toggle work in the most basic form, but they all invariably cause some other aspect of the game to malfunction in some way. Most of them cause the in-game chat to seize up and become unusable, or to get stuck into other kinds of loops. With the community's help, I want to make a fully functioning crouch toggle for PR:BF2 that does not interfere with in-game chatting or using applications outside of BF2.exe. When it is perfected, we can post the code and format it so that laymen can easily customize it to their own personal choice of keybindings.
The best .ahk I have seen made is the one created by [R-CON]Mats391: link to post. It's pretty impressive.
There were some lines in Mats391's code that I didn't understand entirely, but I was able to adapt it to my own layout without much trouble. (I use a QWERTY keyboard. I use 'C' for crouch, and 'LAlt' for prone.)
The only problem I had with Mats391's AHK is that sometimes the in-game chat totally seizes up, even though it works perfectly fine at the start. For example, when I press 'K' to type in team chat, I can only type letters by pressing 'T', 'Enter', 'E', 'Enter', 'S', 'Enter', 'T', 'Enter', and then a final 'Enter' to put it in chat. The end result in chat is: "t?st". Why it behaves like this is a total mystery to me. Once the problem arises, it stays for as long as 2 hours and goes away with no discernible pattern. It happens basically every match. Also, for some reason, BF2 doesn't understand the 'e' keystroke when this thing happens and makes it a '?' mark instead.
This kind of problem happens to me with basically any kind of autohotkey crouch toggle that I try, and it's a complete mystery to me why it happens and behaves in such an odd way.
Now let me share with you the AHK I am currently working on:
Code: Select all
Suspend, on
SetTitleMatchMode 2
SetTimer, KeepRunning, 500
return
KeepRunning:
WinWaitActive, BF2, ,
if ErrorLevel = 0
{
Suspend, off
}
WinWaitNotActive, BF2, ,
if ErrorLevel = 0
{
SendInput {RShift up}
ctoggle = 0
ptoggle = 0
chatting = 0
Suspend, on
}
return
~*c::
if chatting = 1
{
return
}
else
{
if ptoggle = 1
{
SendInput {RShift down}
ptoggle = 0
ctoggle = 1
}
else
{
if ctoggle = 1
{
SendInput {Rshift up}
ctoggle = 0
}
else
{
SendInput {RShift down}
ctoggle = 1
}
}
}
return
~*LAlt::
if ptoggle = 1
{
ptoggle = 0
}
else
{
ptoggle = 1
}
if ctoggle = 1
{
SendInput {Shift up}
ctoggle = 0
}
return
~*j::
~*k::
~*l::
if ctoggle = 1
{
SendInput {Rshift up}
ctoggle = 0
}
if chatting = 1
{
return
}
chatting = 1
KeyWait, Enter
chatting = 0
return
*~Esc::
*~Space::
*~e::
if ctoggle = 1
{
SendInput {RShift Up}
ctoggle = 0
}
return
*~$RCtrl::
ctoggle = 0
ptoggle = 0
chatting = 0
SendInput {RShift up}
SendInput {LAlt up}
SendInput {LShift up}
SendInput {Space up}
SendInput {j up}
SendInput {k up}
SendInput {l up}
SendInput {e up}
return
return
If you are also a crouch toggle user, feel free to try this out, customize it to your keybinds, and let me know what works and what doesn't.
I'm not totally familiar with all of the AutoHotkey language so I'm not totally sure what the best way is to suspend the AHK outside of BF2.exe
Thanks for reading!
============================
EXPLANATION OF THE AHK
Code: Select all
Suspend, on
SetTitleMatchMode 2
SetTimer, KeepRunning, 500
return
KeepRunning:
WinWaitActive, BF2, ,
if ErrorLevel = 0
{
Suspend, off
}
WinWaitNotActive, BF2, ,
if ErrorLevel = 0
{
SendInput {RShift up}
ctoggle = 0
ptoggle = 0
chatting = 0
Suspend, on
}
return
[color=RoyalBlue]~*c::
; If the player is chatting, then the autohotkey does nothing when C is pressed.
if chatting = 1
{
return
}
; This part releases the ptoggle if the player is trying to rise to crouch from prone.
else
{
if ptoggle = 1
{
SendInput {RShift down}
ptoggle = 0
ctoggle = 1
}
else
{
if ctoggle = 1
{
SendInput {Rshift up}
ctoggle = 0
}
else
{
SendInput {RShift down}
ctoggle = 1
}
}
}
return[/color]
[color=Gray]This is the "core" of the AHK. When 'C' is pressed, it first checks if the player is currently chatting. If the player is chatting, then nothing special happens and the AHK just passes through a simple 'C' keystroke into the chat. If the player is not chatting, then the AHK checks if the player is currently prone. If he is prone, then the AHK tells itself that ptoggle = 0 (meaning that the player is not prone) and then holds down 'C' to rise the player from prone and keep him crouched. If the player is also not prone, then the AHK checks to see if the player is currently crouched. If ctoggle = 1, then the AHK sends out a 'C' key is up signal so that the player stands back up. If ctoggle = 0 (ie. the player is standing) then 'C' is held down to put him into crouch.[/color]
[color=Purple]~*LAlt::
if ptoggle = 1
{
ptoggle = 0
}
else
{
ptoggle = 1
}
if ctoggle = 1
{
SendInput {Shift up}
ctoggle = 0
}
return[/color]
[color=Gray]This part of the AHK only keeps track of whether or not the player is prone. It could be possible that it is a redundant part of the AHK, but I'm keeping it for now because it doesn't cause any problems.[/color]
[color=SeaGreen]
~*j::
~*k::
~*l::
if ctoggle = 1
{
SendInput {Rshift up}
ctoggle = 0
}
if chatting = 1
{
return
}
chatting = 1
KeyWait, Enter
chatting = 0
return[/color]
[color=Gray]This is the chatting part of the AHK, and it is the most problematic part. First, it checks if the player is currently crouched. If he is, then the AHK uncrouched him. Then it tells itself that it is in "chat mode", so that the ~*c:: branch is able to function properly (see above). Then it waits for the user to press 'Enter', to signal that he is done chatting. Only then does the AHK tell itself that it is not chatting, and allows the other keys to resume normal function. [/color]
[color=SandyBrown]
*~Esc::
*~Space::
*~e::
if ctoggle = 1
{
SendInput {RShift Up}
ctoggle = 0
}
return[/color]
[color=Gray]This uncrouches the player if he tries to jump, enter a vehicle, or accesses the options menu. Very simple.[/color]
[color=Red]*~$RCtrl::
ctoggle = 0
ptoggle = 0
chatting = 0
SendInput {RShift up}
SendInput {LAlt up}
SendInput {LShift}
SendInput {Space up}
SendInput {e up}
SendInput {j up}
SendInput {k up}
SendInput {l up}
SendInput {v up}
SendInput {e up}
SendInput {b up}
return[/color]
[color=Gray]I made this as a backup mechanism to the AHK in case a problem occurs. It just resets all the values to 0, and stops all keys from being held down. An ugly solution, but it works sometimes when I can't get out of prone.[/color]
return