Start the instrument with an infinite duration (negative p3), or a really long duration. And then stop the instrument whenever you release a key.
<Cabbage>
form caption("Note State Example") size(300, 200), guiMode("queue"), pluginId("def1")
keyboard bounds(10, 10, 280, 100), channel("noteState")
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -+rtmidi=NULL -M0 -dm0 --midi-key-cps=4 --midi-velocity-amp=5
</CsOptions>
<CsInstruments>
ksmps = 32
nchnls = 2
0dbfs = 1
instr 1
event_i "i", 2, 0, -1, p4
if release() == 1 then
turnoff2 2, 0, 1
endif
endin
instr 2
k1 madsr .1, .2, .8, .5
a2 oscili k1, p4
outs a2, a2
endin
</CsInstruments>
<CsScore>
f0 z
</CsScore>
</CsoundSynthesizer>