# [Solved] Is there a way to let the keyboard color (in Kontakt) follow key switch?



## AK11 Lin (Jul 20, 2019)

Hello,

I'm a newer in Kontakt scripting and sorry for my poor English first.
May I ask a question about color on kontakt keyboard?

Is there a way to let the keyboard color follow key switch? 

For example,
When I hit C#0, I have mapped sound between G0 and C7.
So keyboard color becomes BLUE.
That’s great.

But when I hit C0, I have mapped sound just between C1 and B4.
But C5 to C7 still appear BLUE.
Is there a way to let the C5 to C7 color turn back to black&white?

I have tried scripting myself to solve this problem.
My code is :

------------
on init

{*********
* Variable *
**********}

declare $counter

{*********
* Array *
**********}

declare %key_color[128]
{ initialize key colors }
$counter := 0
while ($counter <= 127)
%key_color[$counter] := $KEY_COLOR_NONE
inc($counter)
end while

end on

on note

{Ins1 Key Color}

while($EVENT_NOTE = 24)

{ initialize key colors }
$counter := 0
while ($counter <= 127)
%key_color[$counter] := $KEY_COLOR_NONE
inc($counter)
end while

{ set Ins1 key range }
$counter := 36
while ($counter <= 83)
%key_color[$counter] := $KEY_COLOR_PLUM
inc($counter)
end while

{ set Ins1 keyswitch }
%key_color[24] := $KEY_COLOR_RED

{ Unused Key turn INACTIVE}
$counter := 31
while ($counter <= 35)
%key_color[$counter] := $KEY_COLOR_INACTIVE
inc($counter)
end while

$counter := 84
while ($counter <= 127)
%key_color[$counter] := $KEY_COLOR_INACTIVE
inc($counter)
end while

{ set key colors }
$counter := 0
while ($counter <= 127)
set_key_color($counter, %key_color[$counter])
inc($counter)
end while

end while

{Ins2 Key Color}

while($EVENT_NOTE = 26)

{ initialize key colors }
$counter := 0
while ($counter <= 127)
%key_color[$counter] := $KEY_COLOR_NONE
inc($counter)
end while

{ set Ins2 key range }
$counter := 31
while ($counter <= 108)
%key_color[$counter] := $KEY_COLOR_PLUM
inc($counter)
end while

{ set Ins2 keyswitch }
%key_color[26] := $KEY_COLOR_RED

{ Unused Key turn INACTIVE}
$counter := 109
while ($counter <= 127)
%key_color[$counter] := $KEY_COLOR_INACTIVE
inc($counter)
end while

{ set key colors }
$counter := 0
while ($counter <= 127)
set_key_color($counter, %key_color[$counter])
inc($counter)
end while

end while
end on
------------

This code solve my problem. 
But when I test to changing key switch and play notes , CPU will overload and clip.

Please Help me.
Thank you so much.


----------



## AK11 Lin (Jul 20, 2019)

Hello,

I find this video:


I make adjustments based on this instructional video,
and it's work!!!!!!!


----------



## EvilDragon (Jul 20, 2019)

This question is in the wrong subforum


----------



## AK11 Lin (Jul 20, 2019)

EvilDragon said:


> This question is in the wrong subforum



Wow, sorry.
I see the correct subforum.
How do I delete this post or move to the right place?
(I have just reported it by using "Report" button.)


----------



## Jdiggity1 (Jul 20, 2019)

Thread moved as requested


----------



## AK11 Lin (Jul 21, 2019)

Jdiggity1 said:


> Thread moved as requested



Thank you a lot.


----------



## geronimo (Jul 21, 2019)

AK11 Lin said:


> Is there a way to let the keyboard color follow key switch?
> 
> For example,
> When I hit C#0, I have mapped sound between G0 and C7.
> ...



Make it a habit to use the code insert function to make the KSP text appear; it is more readable for other members.

I was inspired by another KSP Script; I did not write it, but this example with two Keyswitches will certainly help you.


```
on init
    make_perfview
    declare const $NUM_ARTICS := 2

    SET_CONDITION(NO_SYS_SCRIPT_GROUP_START)

    declare %artic_grp_start[$NUM_ARTICS] := (0, 12)
    declare %artic_key_min[$NUM_ARTICS] := (48, 60)    { set keyrange for articulations here, this is lowest key used for all artics }
    declare %artic_key_max[$NUM_ARTICS] := (84, 96)    { set keyrange for articulations here, this is highest key used for all artics }
    declare %KS[$NUM_ARTICS] := (36, 37)
    declare %NUM_RR[$NUM_ARTICS] := (12, 10)
    declare %RR_count[$NUM_ARTICS]

    declare $i
    declare $search_KS
    declare $sel_KS

    make_persistent($sel_KS)
end on


function KeyColor()
    $i := 0
    while ($i < 128)
        set_key_color($i, $KEY_COLOR_INACTIVE)
        set_key_type($i, $NI_KEY_TYPE_NONE)

        if (in_range($i, %artic_key_min[$sel_KS], %artic_key_max[$sel_KS]))
            set_key_color($i, $KEY_COLOR_BLUE)
            set_key_type($i, $NI_KEY_TYPE_DEFAULT)
        end if

        inc($i)
    end while

    $i := 0
    while ($i < $NUM_ARTICS)
        if ($i = $sel_KS)
            set_key_color(%KS[$i], $KEY_COLOR_GREEN)
        else
            set_key_color(%KS[$i], $KEY_COLOR_RED)
        end if

        set_key_type(%KS[$i], $NI_KEY_TYPE_CONTROL)

        inc($i)
    end while
end function


on persistence_changed
    call KeyColor()
end on


on note
    { process keyswitches }
    $search_KS := search(%KS, $EVENT_NOTE)
    if ($search_KS # -1)
        $sel_KS := $search_KS
        call KeyColor()

        exit
    end if

    { process playable keyrange }
    if (in_range($EVENT_NOTE, %artic_key_min[$sel_KS], %artic_key_max[$sel_KS]))
        disallow_group($ALL_GROUPS)
        allow_group(%RR_count[$sel_KS])

        if (%RR_count[$sel_KS] >= %NUM_RR[$sel_KS])
            %RR_count[$sel_KS] := 0
        else
            inc(%RR_count[$sel_KS])
        end if
    end if
end on
```


----------

