@Kitch I found this script a long time back on the forum here :
const modifierState = event.keyboardState
if (modifierState.hasShift) {
log("Shift is held down");
//Add code here for when Shift is held down...
} else if (modifierState.hasControl) {
log("Control is held down");
//Add code here for when Control is held down...
} else if (modifierState.hasCommand) {
log("Command is held down");
//Add code here for when Command is held down...
} else if (modifierState.hasAlt) {
log("Option is held down");
//Add code here for when Option is held down...
} else {
log("No modifiers are held down");
//Add code here where no modifiers are held down///
}
With the goal of making it more accessible to us code Illiterate I'm working on making it into a template you can simply copy command ID's into so users can make their own multi-funtion buttons without needing to code.
const modifierState = event.keyboardState
const { holdControl, holdOption, holdShfit, holdCommand, noModifier } = event.props;
if (modifierState.hasShift) {
sf.soundflow.runCommand({
commandId: holdShfit,
});
} else if (modifierState.hasControl) {
sf.soundflow.runCommand({
commandId: holdControl,
});
} else if (modifierState.hasCommand) {
sf.soundflow.runCommand({
commandId: holdCommand,
});
} else if (modifierState.hasAlt) {
sf.soundflow.runCommand({
commandId: holdOption,
});
} else {
sf.soundflow.runCommand({
commandId: noModifier,
});
}

However for some reason the HOLD SHIFT modifer and the NO MODIFER are not working. All three of the }else if{
work but the if
and the } else {
are bugging out.
What simple code am I getting totally wrong?
Linked from:
- Chris Shaw @Chris_Shaw2021-11-04 20:37:24.031Z
You might want to try a switch case instead.
Additionally, instead of usinghasAlt
,hasControl
. etc, try usingevent.keyboardState.asString
. This way you'll be able to detect modifier combinations like shift-cmd. (I put a logging function to demonstrate.)const modifierState = event.keyboardState.asString log (modifierState) switch (true) { case (modifierState == "shift"): log("Shift is held down"); break case (modifierState == "ctrl"): log("Control is held down"); break case (modifierState == "cmd"): log("Command is held down"); break case (modifierState == "alt"): log("Option is held down"); break case (modifierState == ""): log("No Modifiers are held down"); break case (modifierState == "alt+shift"): // two modifiers! log("Alt & Shift are held down"); break };
- OOwen Granich-Young @Owen_Granich_Young
Sweet, I'll swap this out and try it!
- OOwen Granich-Young @Owen_Granich_Young
OH... MY .... GOD.... It was me all along. I had mispelled SHIFT and MODIFER in the SCRIPT vs the PROPERTIES.
Your way gave me the same error Chris and it took me trying a diffrent verison to realize my mistake!
User Fail. But I will use yours so people can have EVEN MORE modifer possiblites.
Thanks!
OwenChris Shaw @Chris_Shaw2021-11-04 20:59:53.302Z
glad I could help :)
- In reply toOwen_Granich_Young⬆:
Kitch Membery @Kitch2021-11-04 21:21:52.645Z
Ha! @Chris_Shaw beat me to it. Nice one Chris!.. And yes the Switch case is preferable for this scenario.
Glad you two worked it out. :-)
Rock on!- OOwen Granich-Young @Owen_Granich_Young
BUT HAVE I TAKEN IT TOO FAR 😂
Thanks again both of you, I'm going to publish 'Multi-Function Buttons for Dummys' to the store for all of us coding impaired.
Kitch Membery @Kitch2021-11-04 21:44:32.644Z
Nice one @Owen_Granich_Young!
Sounds good! But be sure to read my advice in post #7
- In reply toOwen_Granich_Young⬆:
Chris Shaw @Chris_Shaw2021-11-04 22:58:08.058Z
BUT HAVE I TAKEN IT TOO FAR 😂
Not really - there are 13 possible combos - you missed 2:
ctl-alt-cmd
shift-ctrl-alt-cmdKitch Membery @Kitch2021-11-04 22:59:48.230Z
BUT HAVE I TAKEN IT TOO FAR 😂
Hahahahaha no but @Chris_Shaw did!
- In reply toChris_Shaw⬆:OOwen Granich-Young @Owen_Granich_Young
Ha now I have to put those in later but also jeeez the quad might be a bit much for my fingers lol! Control option command is an important one tho!
- In reply toOwen_Granich_Young⬆:
Kitch Membery @Kitch2021-11-04 21:31:27.148Z
A side note on this, if you are going to be sharing it on the store is that reading modifier states should maybe be discouraged if the user is triggering the command presets via "Keyboard Triggers" as they may overlap with other SoundFlow command triggers (and SoundFlow has no way of knowing if there is a clash), they do however work quite well in conjunction with Stream Decks.
Let me know if that makes sense. I can give an example if needed.
Rock on!
- OOwen Granich-Young @Owen_Granich_Young
I will make a note this is designed for streamdeck use, good point!
- In reply toKitch⬆:
Chris Shaw @Chris_Shaw2021-11-04 21:59:35.630Z2021-11-04 22:24:51.731Z
Just to confirm what @Kitch says:
I tried to do something similar to this using keyboard triggers and the only modifiers a script like this will recognize are the ones assigned to the keyboard trigger. In order to recognize all available modifiers the user would have to assign multiple keyboard triggers (one for each modifier combo).So I don't think that it would necessarily conflict with SF but rather it would be rather awkward / limiting.
So this script is best suited for decks or surfaces.
- OOwen Granich-Young @Owen_Granich_Young
Yeah that all makes sense, tbh I only have built it thinking about streamlining my deck. And for friends who also want each button on their decks to be able to do more.
Kitch Membery @Kitch2021-11-04 22:11:05.826Z
Awesome!
- OOwen Granich-Young @Owen_Granich_Young
The @raphaelsepulveda multi-click one should work fine with keybinding though, nifty alternative I also included in the package.
Kitch Membery @Kitch2021-11-04 22:18:01.339Z
Which one is that? Is there a link?
- OOwen Granich-Young @Owen_Granich_Young
- In reply toOwen_Granich_Young⬆:
Kitch Membery @Kitch2021-11-04 22:17:28.796Z
A small refactor of the switch case;
const modifierState = event.keyboardState.asString switch (modifierState) { case "shift": log("Shift is held down"); break case "ctrl": log("Control is held down"); break case "cmd": log("Command is held down"); break case "alt": log("Option is held down"); break case "": log("No Modifiers are held down"); break case "alt+shift": // two modifiers! log("Alt & Shift are held down"); break };
Chris Shaw @Chris_Shaw2021-11-04 22:51:48.736Z
of course!