Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,359 Location: Tampa, FL Thanks: 28 times Was thanked: 419 time(s) in 356 post(s)
|
The reason hot keys are separate is because originally they were officially registered hot keys with Windows, meaning only one instance of that hot key can be claimed. So application-level doesn't make sense for a global hot key. Starting with version 0.3.3.0, hot keys can be configured as Unregistered, where they are simply monitored by S+ and fired, though I don't really like the feature from a performance/UX perspective because it involves processing against every keystroke. This also happens if any Text Expansions are defined, and of course ignore keys are also checked. That is about as much keyboard interference as I would like to have, since it all adds up and has the potential to begin impacting the user when they're simply typing an email, as every keystroke has to pass through S+ and be evaluated. Adding applications to this mix add a significant increase of processing per keystroke. It's always a balancing act between features and performance/UX, since certain things have a cost for simply using the computer while S+ is running and there's only so many CPU cycles I can add to each keyboard and mouse event until it starts to become noticeable that these events are slower when S+ is running; which then defeats the purpose of this being a productivity enhancing application :) So yes, in these instances the recommendation is to use conditional execution within a single script. There are numerous ways to decentralize this or control via other methods. For example using the Global Actions > Window Events > Foreground Window Change script, you could set variables, activate/deactivate hot keys, etc. based on the current foreground window. Your hot key behavior recommendations can all be accomplished now via script logic, sp.GetKeyState, and variable storage. - Play while hotkey is pressed is the default behavior currently, the script will be continually repeated. However, depending on what you're doing in the script itself, you might have to add some extra lines to handle key states. This is also subject to standard Windows repeat key delay settings, etc. You could certainly create a faster repetition script by leveraging some of the logic shown below. - Play once hotkey is pressed - This script demonstrates how this can be accomplished: Code://This test hot key is defined as Shift+5
//Only execute if the hot key was not already pressed
if(!sp.GetStoredBool("hotkey_s5_pressed")) {
//Set a variable to say the hot key is down
//This ensures the next repeating keyboard event doesn't execute this script
//over and over
sp.StoreBool("hotkey_s5_pressed", true);
//---------------------------------------------------------------------------
//TODO: Put whatever you want this script to do here, replace my example code
//Below I'm sending the Shift key up so "6" is typed instead of Shift+6 ("^")
//because this hot key uses Shift and it would be physically held down while
//executing this hot key, but then I set the Shift key back to being down to
//return it to the original state
sp.SendShiftUp();
sp.SendKeys("6");
sp.SendShiftDown();
//---------------------------------------------------------------------------
//Create a timer that watches the key state of the key "5", as defined for this hot key
//Once the 5 key is no longer being held down, it clears the timer and stored variable
//so the next Shift+5 will execute the script once
sp.CreateTimer("hotkey_5_pressed_watcher", 0, 100, `if(!(sp.GetKeyState(vk.VK_5) & 0x8000)) {
sp.DeleteStoredBool("hotkey_s5_pressed");
sp.DeleteTimer("hotkey_5_pressed_watcher");
}`);
}
- Toggle when the hotkey is pressed - this script will handle toggling on/off: Code://This test hot key is defined as Shift+6
//Toggle the variable, setting it to the opposite state of whatever
//it currently holds (true/false) - the first time this runs, it will be
//false (toggled off) and will start the timer
sp.StoreBool("hotkey_s6_pressed", !sp.GetStoredBool("hotkey_s6_pressed"));
var repeatDelay = 5000; //Executes the timer script every 5000 ms (5 seconds)
//If the hotkey is in the toggled on state, create a timer to run
if(sp.GetStoredBool("hotkey_s6_pressed")) {
sp.CreateTimer("hotkey_s6_timer", 0, repeatDelay, `if(!sp.GetStoredBool("hotkey_s6_loop_executing")) {
//"hotkey_s6_loop_executing" is just used to make sure the
//timer executions don't step on each other and run in parallel
sp.StoreBool("hotkey_s6_loop_executing", true);
//TODO: Add your code here, replace the line below
sp.MessageBox("Hi, still running hotkey script", "Shift+6 Hot Key");
sp.DeleteStoredBool("hotkey_s6_loop_executing");
}`);
} else {
//Toggled off, delete the timer
sp.DeleteTimer("hotkey_s6_timer");
}
Again, these are just examples, some of this could be moved into functions (in Load Scripts tab) to simplify the hot key code and make more generic/reuseable code for other hot keys instead of duplicating the logic in each one.
|