Please draw the structure of KCN.
Understand the structure of [CN]- first:
(Dots represent electrons not participating in bonding.)
Notice that when C forms a triple bond with N, N has 3 bond pairs and 1 lone pair of electrons. Thus it has 8 electrons in its valence shell, which is, a complete octet.
C, however, has 3 bond pairs and 1 odd(unpaired) electron. Thus, it has 7 electrons in its valence shell, which is, 1 electron shy of a complete octet.
Thus, carbon gains 1 electron to complete its octet and gains a negative charge. Thus, the [CN]- radical is formed.
Now, K+ forms an ionic bond with the negatively charged [CN]- radical.
Hence, KCN is formed. (Note, a KCN molecule contains both covelent bond, between C and N, and ionic bond, between K+ and [CN]- )