...
...passing the Variable Hotkey in question in the call, and then the C# would parse and check if any of the keys are in a PRESS state or not.
...gonna need to look for any open/close brackets and pull the in-between value, as well as value without open/close brackets.
...to use the virtual key code, then I'd need a mapping between that and the text(key) I pull out of the Variable Hotkey.
None of this is really needed for a
single key contained in the VK_FWD text variable. First of all, if you have a variable already containing the key you want to press, you can simply nest that into the state token (even if the value of VK_FWD is surrounded in square brackets):
{STATE_KEYSTATE:{TXT:VK_FWD}}
...all this is because (as noted in the VA Manual under "Key State Token Parameter Values" around page 200):
...if you wanted to see if the F10 key is down, simply use the following token: {STATE_KEYSTATE:F10}.
Note: For convenience, if the key parameter is surrounded by square brackets (‘[‘ and ‘]’),
the brackets will be automatically removed before processing.
meaning all of these are valid options..
{STATE_KEYSTATE:F1}
{STATE_KEYSTATE:[F1]}
{STATE_KEYSTATE:{TXT:VK_FWD}}
(which for the UP arrow key is the same as the following, too)
{STATE_KEYSTATE:[ARROWU]}
{STATE_KEYSTATE:38}
(and, as an example of actions in a command)
Begin Text Compare : [{STATE_KEYSTATE:{TXT:VK_FWD}}] Equals '1'
Start VoiceAttack listening
Else
Stop VoiceAttack listening
End Condition
(and also, as an example of an inline function executed from a command, running in the background so long as VoiceAttack is running)
using System;
using System.Threading;
public class VAInline
{
public void main()
{
while(true)
{
var keyDown = (VA.Utility.ParseTokens("{STATE_KEYSTATE:{TXT:VK_FWD}}") == "1");
Thread.Sleep(keyDown ? 150 : 500);
if (VA.State.GetListeningEnabled() != keyDown)
{
VA.State.SetListeningEnabled(keyDown);
}
// Add a small delay to reduce CPU usage
Thread.Sleep(25);
}
}
}
...although, as you stated previously that you might have more than just a single key, but like a key combo, you'd need to parse that out as you described.
...
As Variable Hotkeys could be a combination, like: RCTRL + F1
So the KEYSTATE would need to know if both the RCTRL and F1 keys are both pressed.
One way would be to use Regular Expressions (RegEx), and then you can simply monitor either one or both keys to have been pressed, and toggle the listening mode respectively (if not already in the correct mode).
Here's a working example you can test and tinker with, and/or alter as needed for different functionality -- uncomment various "TESTING" variables at the top of the main() method to see how these work, using the "Test Run" button to start/stop this script from within the inline function editor:
using System;
using System.Threading;
using System.Text.RegularExpressions;
public class VAInline
{
public string[] ParseVariableHotkeys(string input)
{
// Regex to capture either bracketed keys or single characters
var matches = Regex.Matches(input, @"(\[[A-Z0-9]+\])|(\d+)|(\w)");
string[] keys = new string[matches.Count];
for (int i = 0; i < matches.Count; i++)
{
keys[i] = matches[i].Value;
}
return keys;
}
public void main()
{
/// TESTING:
//VA.SetText("VK_FWD", "[RCTRL][F1]");
//VA.SetText("VK_FWD", "[LCTRL][ENTER]");
//VA.SetText("VK_FWD", "[ALT]3");
//VA.SetText("VK_FWD", "t");
//VA.SetText("VK_FWD", "[ARROWU]");
VA.SetText("VK_FWD", "38"); // The virtual key code for Arrow-UP is 38
// Gather the string value(s) contained in the Variable Hotkey "VK_FWD"
string[] keys = ParseVariableHotkeys(VA.GetText("VK_FWD") ?? string.Empty);
// Guard Clause to ensure there is at least one key and less than 3 set in "VK_FWD"
if (keys.Length == 0 || keys.Length > 2)
{
VA.WriteToLog("Invalid or no valid keys parsed from VK_FWD.", "red");
return;
}
bool hasSecondKey = keys.Length > 1;
VA.WriteToLog("Push to Talk Key Monitor is now Active", "green");
while (true)
{
bool key1Down = (VA.Utility.ParseTokens("{STATE_KEYSTATE:" + keys[0] + "}") == "1");
bool key2Down = hasSecondKey ? (VA.Utility.ParseTokens("{STATE_KEYSTATE:" + keys[1] + "}") == "1") : true;
bool keysPressed = key1Down && key2Down;
Thread.Sleep(keysPressed ? 150 : 500);
if (VA.State.GetListeningEnabled() != keysPressed)
{
VA.State.SetListeningEnabled(keysPressed);
}
// Add a small delay to reduce CPU usage
Thread.Sleep(25);
}
}
}
I hope that helps you accomplish your current goals, and gives you an idea on how you can accomplish similar advanced goals in future.
Best wishes and have fun!!
