# Kontakt 4 find out if key has samples mapped



## d.healey (Jul 27, 2013)

Is there a way to get Kontakt to tell me which keys in a group have samples mapped to them?

Thanks.


----------



## mk282 (Jul 27, 2013)

Yes, but not during init, and if you have overlapping zones within one group, it will report only the first zone ID it encounters.


```
on init
    declare const $KEY := 60
    declare const $GROUP := 0
    declare $i
    declare $ID
    declare ui_button $test
end on

on ui_control ($test)
    $ID := play_note($KEY,1,0,10)
    $i := 0
    while ($i < $NUM_GROUPS)
        if ($i # $GROUP)
            set_event_par_arr($ID,$EVENT_PAR_ALLOW_GROUP,0,$i)
        else
            set_event_par_arr($ID,$EVENT_PAR_ALLOW_GROUP,1,$i)
        end if
        inc($i)
    end while
    wait(1)
    message(get_event_par($ID,$EVENT_PAR_ZONE_ID))
    $test := 0
end on
```

It will return -1 if a zone is not found.


----------



## d.healey (Jul 27, 2013)

Aha, this looks good, thanks


----------



## d.healey (Jul 29, 2013)

Hi mk

This almost does what I want but I can't work out the last bit so maybe you can help again 

I have say 10 groups, with some samples in each, mapped to different keys in different groups. How can I check which keys in group 5 have samples mapped to them?


----------



## mk282 (Jul 29, 2013)

I just gave you a proof of concept there.  Change the value of $GROUP constant to test only notes in that group. Change the $KEY constant to test a different key. You could make a nested while loop that passes all keys in every group ($GROUP and $KEY shouldn't be const in that case) and returns something if the value returned from find_zone() is anything other than -1.


----------



## d.healey (Jul 30, 2013)

I tried this with a nested loop but it doesn't seem to be 100% accurate. I'm trying to get it to set the key colours based on if the key has a sample mapped to it or not. I've tried different wait times and note durations and I get varying results but never the correct one.



```
declare i 
    declare ii
    declare note_id
    declare keys[128] := (-1)
            
    for i := 0 to 127

        set_key_color(i, KEY_COLOR_WHITE)
        
        note_id := play_note(i, 1, 0, 10)

        wait(100)
        for ii := 0 to NUM_GROUPS
            set_event_par_arr(note_id, EVENT_PAR_ALLOW_GROUP, 0, ii) 
            wait(1000)
        end for
        
        set_event_par_arr(note_id, EVENT_PAR_ALLOW_GROUP, 1, 0)

        if (get_event_par(note_id, EVENT_PAR_ZONE_ID) > -1)
            set_key_color(i, KEY_COLOR_BLUE)
        end if
        
    end for
    
    message('DONE')
```


----------



## mk282 (Aug 3, 2013)

Try this. Just need to think logically. 


```
on init
    declare i
    declare j
    declare ID

    declare ui_menu Group

    for i := 0 to NUM_GROUPS - 1
        add_menu_item(Group,group_name(i),i)
    end for

    make_persistent(Group)
    read_persistent_var(Group)

    pgs_create_key(INIT,1)
    pgs_set_key_val(INIT,0,1)

    message("")
end on


function DetectZones()
    for i := 0 to 127
        ID := play_note(i,1,0,1)
        change_vol(ID,-200000,0)
        for j := 0 to NUM_GROUPS - 1
            if j # Group
                set_event_par_arr(ID,EVENT_PAR_ALLOW_GROUP,0,j)
            else
                set_event_par_arr(ID,EVENT_PAR_ALLOW_GROUP,1,j)
            end if
        end for
        wait(1)
        if get_event_par(ID,EVENT_PAR_ZONE_ID) = -1
            set_key_color(i,KEY_COLOR_WHITE)
        else
            set_key_color(i,KEY_COLOR_RED)
        end if
    end for
end function


on ui_control (Group)
    call DetectZones()
end on


on pgs_changed
    if pgs_key_exists(INIT) and pgs_get_key_val(INIT,0) = 1
        call DetectZones()
        pgs_set_key_val(INIT,0,0)
    end if
end on
```


----------



## d.healey (Aug 3, 2013)

Thanks, I'll give it a whirl tomorrow


----------

