# Kontakt Scripting Keyswitches



## mrazz (Jan 21, 2019)

Hello- 

I'm new to the KSP language. I've tried to create a script that creates keyswitiching. Can someone help me better understand this? I've got 12 groups of sustain samples (0-11) that need to be accessible from a keyswitch on note 48 and eight staccato groups when keyswitch using key/note 50. I've tried to script it and even though it compiles and Kontakt finds no errors with my script, it doesn't work. A second question... before scripting this should I turn off any key switches I already set in the group start options?


----------



## d.healey (Jan 21, 2019)

Why do you need to script it? What's wrong with the group start options?


----------



## mrazz (Jan 21, 2019)

Thanks for your reply d.healey! Nothing is wrong with the group start functions except I may need one key switch for the sustain groups, a second key switch for the staccato groups and a third key switch for both sustain and staccato at the same time. Without scripting, I assume I would have to copy and paste both my sustain and staccato groups and put them on a third key switch using group start functions but I would prefer not doing that.


----------



## mrazz (Jan 21, 2019)

This is my attempt- however when I run the script, something must be missing as I don't hear any group!


```
on init

declare const $keyswitch1 := 48
declare const $keyswitch2 := 50
set_key_color($keyswitch1, $KEY_COLOR_RED)
set_key_color($keyswitch2, $KEY_COLOR_RED)

end on

on note
disallow_group(ALL_GROUPS)

    if (EVENT_NOTE = $keyswitch1)
        allow_group(0)
        allow_group(1)
        allow_group(2)
        allow_group(3)
        allow_group(4)
        allow_group(5)
        allow_group(6)
        allow_group(7)
        allow_group(8)
        allow_group(9)
        allow_group(11)
    end if

    if (EVENT_NOTE = $keyswitch2)
        allow_group(12)
        allow_group(13)
        allow_group(14)
        allow_group(15)
        allow_group(16)
        allow_group(17)
        allow_group(18)
        allow_group(29)
    end if

end on
```


----------



## d.healey (Jan 21, 2019)

I don't get why you can't use group start options. Your code looks fine by the way so the lack of sound must be caused by something else.

Have a look at the example I've attached, first 4 groups are triggered by C0, the next 4 are triggered by C#0


----------



## mrazz (Jan 21, 2019)

Thanks for the example. I already know how to do that though. To use your example, how to I set a third keyswitch (let's say D0) that turns on a combination those groups or all of them? That's what I need to do. Let's say in your example your first four groups were dynamic layers of sustains and the second four were dynamic layers of staccato and then you wanted a third key switch that enabled ALL eight groups so you hear sustains and staccatos. Seems like a script is necessary. I would rather do that than copy the groups again in the group editor so I can use group start options to create a third keyswitched mix.


----------



## polypx (Jan 21, 2019)

You are only allowing the groups when the keyswitch itself is played. You need to allow the correct groups for other notes, after you've made the keyswitch happen.

Typically we use the keyswitch to change the STATE of a variable, and then allow the necessary groups based on that variable. 


```
if (EVENT_NOTE = $keyswitch1)
 current_state := 0
end if
if (EVENT_NOTE = $keyswitch2)
 current_state := 1
end if

if (current_state = 0)
allow_group(all those groups)
end if
if(current_state=1)
allow+group(all those other groups)
end if
```


----------



## mrazz (Jan 21, 2019)

Ok- this is helpful. When I use the updated script below, Kontakt shows an error on "disallow_group(ALL_GROUPS). pollpx do you mind perusing my scripting and enlightening me?


```
on init

declare const $keyswitch1 := 48
declare const $keyswitch2 := 50
set_key_color($keyswitch1, $KEY_COLOR_RED)
set_key_color($keyswitch2, $KEY_COLOR_RED)
declare $current_state
end on

on note
disallow_group(ALL_GROUPS)

if (EVENT_NOTE = $keyswitch1)
 current_state := 0
end if
if (EVENT_NOTE = $keyswitch2)
 current_state := 1
end if

if (current_state = 0)
    allow_group(0)
    allow_group(1)
    allow_group(2)
    allow_group(3)
    allow_group(4)
    allow_group(5)
    allow_group(6)
    allow_group(7)
    allow_group(8)
    allow_group(9)
    allow_group(11)
end if
if(current_state=1)
    allow_group(12)
    allow_group(13)
    allow_group(14)
    allow_group(15)
    allow_group(16)
    allow_group(17)
    allow_group(18)
    allow_group(19)
end if

end on
```


----------



## polypx (Jan 21, 2019)

If you're not using Sublime_KSP, you probably just need to put the dollar sign in front of all your variables... ie. $ALL_GROUPS, $EVENT_NOTE, etc.


----------



## mrazz (Jan 21, 2019)

Thanks polypx. That works. I'm using Sublime_KSP but for some reason was missing those dollar signs. It works now. One more thing for a newbie- will you tell me how to properly paste my code so that it appears like in your first reply to my post?


----------



## polypx (Jan 21, 2019)

In this forum there's a bunch of icons above where I'm typing right now.

The plus sign, four from the right end, has a "Code" option. 


```
You can insert code like that.  And people on this forum really love it, so you should.  :-)
```


----------



## mrazz (Jan 21, 2019)

Thanks! For anyone who is interested the corrected script is:

```
on init

declare const $keyswitch1 := 48
declare const $keyswitch2 := 50
set_key_color($keyswitch1, $KEY_COLOR_RED)
set_key_color($keyswitch2, $KEY_COLOR_RED)
declare $current_state
end on

on note
disallow_group($ALL_GROUPS)

if ($EVENT_NOTE = $keyswitch1)
 $current_state := 0
end if

if ($EVENT_NOTE = $keyswitch2)
 $current_state := 1
end if

if ($current_state = 0)
    allow_group(0)
    allow_group(1)
    allow_group(2)
    allow_group(3)
    allow_group(4)
    allow_group(5)
    allow_group(6)
    allow_group(7)
    allow_group(8)
    allow_group(9)
    allow_group(10)
    allow_group(11)
end if

if($current_state = 1)
    allow_group(12)
    allow_group(13)
    allow_group(14)
    allow_group(15)
    allow_group(16)
    allow_group(17)
    allow_group(18)
    allow_group(19)
end if

end on
```


----------



## mrazz (Jan 21, 2019)

But why didn't this work? I tried to add a third "current_state" variable. When I press that keyswitch I expect all groups, but instead get the same groups for current state 1 (keyswitch 2).

```
it

declare const $keyswitch1 := 48
declare const $keyswitch2 := 50
declare const $keyswitch3 := 52
set_key_color($keyswitch1, $KEY_COLOR_RED)
set_key_color($keyswitch2, $KEY_COLOR_RED)
set_key_color($keyswitch3, $KEY_COLOR_RED)
declare $current_state
end on

on note
disallow_group($ALL_GROUPS)

if ($EVENT_NOTE = $keyswitch1)
 $current_state := 0
end if

if ($EVENT_NOTE = $keyswitch2)
 $current_state := 1
end if

if ($EVENT_NOTE = $keyswitch3)
 $current_state := 2
end if

if ($current_state = 0)
    allow_group(0)
    allow_group(1)
    allow_group(2)
    allow_group(3)
    allow_group(4)
    allow_group(5)
    allow_group(6)
    allow_group(7)
    allow_group(8)
    allow_group(9)
    allow_group(10)
    allow_group(11)
end if

if($current_state = 1)
    allow_group(12)
    allow_group(13)
    allow_group(14)
    allow_group(15)
    allow_group(16)
    allow_group(17)
    allow_group(18)
    allow_group(19)
end if

if($current_state = 2)
    allow_group(ALL_GROUPS)
end if

end on
```


----------



## polypx (Jan 21, 2019)

That should work, I think --- other than that you forgot the dollar sign again.


----------



## mrazz (Jan 21, 2019)

Yes I think Sublime is adding it for me when I compile. Is this because my first 12 groups are set to cycle round robin and my second eight groups are set to cycle round robin random? That's the next thing I have to learn how to script since Kontakt only seems to have one round robin chain. Feel free to suggests some resources to learn that part of KSP!


----------



## mrazz (Jan 21, 2019)

I figured it out. I had designed key switching into my group start options. This was interfering with the script for some reason. When I removed these key switches from group start options, the script works as expected. Thanks for all your help!


----------



## polypx (Jan 21, 2019)

Yes, Sublime will add that if you compile. And as you discovered, you cannot combine this with the group options. Great it's working!


----------



## mrazz (Jan 25, 2019)

Is there a better way to describe a list of groups in one line than what I'm doing below? My scripts are getting LONG 

```
if ($current_state = 0)
    allow_group(0)
    allow_group(1)
    allow_group(2)
    allow_group(3)
    allow_group(4)
    allow_group(5)
    allow_group(6)
    allow_group(7)
    allow_group(8)
    allow_group(9)
    allow_group(10)
    allow_group(11)
    allow_group(32)
    allow_group(33)
    allow_group(34)
    allow_group(35)
    allow_group(36)
    allow_group(37)
    allow_group(38)
    allow_group(39)
    allow_group(40)
    allow_group(41)
    allow_group(42)
    allow_group(43)
    allow_group(44)
    allow_group(45)
    allow_group(46)
    allow_group(47)
```


----------



## EvilDragon (Jan 25, 2019)

Yes, do a while loop.


----------



## mrazz (Jan 25, 2019)

EvilDragon thanks for the reply. I've read so much of your helpful posts I feel like I'm chatting with royalty. Can you post an example? Basically I'm just creating key switches that turn on groups and want to avoid the long list of groups. The 6.02 KSP manual is a little vague on this to me. The only example they list makes sense but I'm not sure how to apply while in my case. Here's the example the manual gave for using while to cause other notes to play (I think.)

```
while($NOTE_HELD = 1)
        play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$DURATION_QUARTER/2)
        wait($DURATION_QUARTER)
    end while
```


----------



## EvilDragon (Jan 25, 2019)

There are other examples of while loops in KSP reference, do a search


----------



## ScoringFilm (Jan 26, 2019)

mrazz said:


> Is there a better way to describe a list of groups in one line than what I'm doing below? My scripts are getting LONG



Something like this is what you need:


```
if ($current_state = 0)
   $i := 0
   while ($i < 47)
      allow_group($i)     
      inc($i)
   end while
end if
```


----------



## mrazz (Jan 26, 2019)

Thank you for this. That is starting to make sense I think. Pardon my ignorance, but is 47 The number of groups and this code would turn on groups 0 through 47? I guess the arbitrary number of 47 is throwing me a little.


----------



## robgb (Jan 26, 2019)

I bought a library that has scripted key switches and no way to change them. I'm used to being able to go into the group start options, but no. It's all scripted. Annoying as hell. So, instead, I have to recode my articulation mapper to include the new key switches. Ugh. I'm halfway tempted to get a cracked version of Kontakt and change the damn script.


----------



## EvilDragon (Jan 26, 2019)

You can use the Change Keys multiscript to remap keys outside of the NKI itself.



mrazz said:


> Pardon my ignorance, but is 47 The number of groups and this code would turn on groups 0 through 47? I guess the arbitrary number of 47 is throwing me a little.



Yes. 47 is the limit of the while loop, so it will run from 0 to 46 (because it goes < 47, not <= 47, which would also include group ID 47).


----------



## ScoringFilm (Jan 27, 2019)

mrazz said:


> Thank you for this. That is starting to make sense I think. Pardon my ignorance, but is 47 The number of groups and this code would turn on groups 0 through 47? I guess the arbitrary number of 47 is throwing me a little.


What Mario said!


----------



## KrisY (Feb 4, 2019)

mrazz said:


> EvilDragon thanks for the reply. I've read so much of your helpful posts I feel like I'm chatting with royalty. Can you post an example? Basically I'm just creating key switches that turn on groups and want to avoid the long list of groups. The 6.02 KSP manual is a little vague on this to me. The only example they list makes sense but I'm not sure how to apply while in my case. Here's the example the manual gave for using while to cause other notes to play (I think.)
> 
> ```
> while($NOTE_HELD = 1)
> ...




Hello mrazz. Here I´ve made 2 versions of the while loop you´ll need to complete your script. This makes the long list obsolete.

Firstly, you can just "disallow" all groups if the button is not on (assuming you´re using a ui_button element):


```
on init

  { declare elements }
  declare ui_button $current_state
  declare $a

  $current_state := 0

  make_persistent ($current_state)    {just making it save the button state}

end on

on note

  allow_group($ALL_GROUPS)

  $a := 0

  if ($current_state = 0)       { if button is off }

    while($a < $NUM_GROUPS)     { for all groups }

      disallow_group($a)

      inc($a)

    end while

  else

  { if button is not off, allow all groups, like before the while loop }

    allow_group($ALL_GROUPS)

  end if

end on
```

That one is useful. But for most situations you will need to use an interval of groups. So for example between group 0-2, and also between group 4-5 do...

(here I used 2 separate while loops that turn off separate segments of groups, on ui_button = 0, which is off.)


```
on init

  { declare elements }
  declare ui_button $current_state
  declare $a
  declare $b

  $current_state := 0

  make_persistent ($current_state)    {just making it save the button state}

end on

on note

  allow_group($ALL_GROUPS)

  $a := 0
  $b := 3   { note that b has to start where the second range is set to begin. }

  if ($current_state = 0)     { if button is off }

  { two while loops in a row, one disallowing groups 1-2, the other groups 4-5 }

    while($a < 2)      { ranges 1-2 (group ID 0-1) }

      disallow_group($a)

      inc($a)

    end while

    while($b < 5)      { ranges 4-5 (group ID 3-4, so less than 5) }

      disallow_group($b)

      inc($b)

    end while

  else

  { if button is on, allow all groups, like before the while loop }

    allow_group($ALL_GROUPS)


  end if

end on
```

This second one is vastly more useful. Note that *disallow/allow groups* does not work for "*on release*" without _disabling_ the *internal native kontakt release script*. Sucks. But for all *on note* and *on controller* (_I believe_), it works.


----------



## ScoringFilm (Feb 4, 2019)

How to control release groups is here:
https://vi-control.net/community/th...sallow_groups-not-working.71354/#post-4228968


----------

