You need to keep track of the pressed keys. Once a key is released, it will stop sending keyrepeats and you can conclude that it’s been released.
You’ll need to poll the active keys as well, using an idle notifier - this is futher down in the script:
https://github.com/renoise/xrnx/blob/master/Tools/com.renoise.Noodletrap.xrnx/classes/NTrapUI.lua#L1676
My example actually contains a decent amount of comments. Not sure I can explain it any better here.
But the essence is that you’ll get something that emulates a keypress. It’s not the real deal - the release happens slightly later.
Obviously, it would be way better and simpler to add real key-releases to the Renoise API. This code is just here to fill in that gap.
Thanks Danoise !
I do not quite understand how to trace the repetition, to execute a function after the repetition is over.
I have created a timer function to restore the color, but it does not obey the key.note released:
--WMP: usb keyboard, keys, edit, sound and color
for i = 0, 119 do
local tb_cl = {}
tb_cl.yl_1 = { 0,2,4,5,7,9,11, 12,14,16,17,19,21,23 }
tb_cl.yl_2 = { 1,3,6,8,10, 13,15,18,20,22 }
tb_cl.bl_1 = { 24,26,28,29,31,33,35, 36,38,40,41,43,45,47, 48,50,52,53,55,57,59 }
tb_cl.bl_2 = { 25,27,30,32,34, 37,39,42,44,46, 49,51,54,56,58 }
tb_cl.rd_1 = { 60,62,64,65,67,69,71, 72,74,76,77,79,81,83, 84,86,88,89,91,93,95 }
tb_cl.rd_2 = { 61,63,66,68,70, 73,75,78,80,82, 85,87,90,92,94 }
tb_cl.gy_1 = { 96,98,100,101,103,105,107, 108,110,112,113,115,117,119 }
tb_cl.gy_2 = { 97,99,102,104,106, 109,111,114,116,118 }
local function restore_color_piano()
for k = 0, 21 do
if tb_cl.yl_1[k] == i then return { 0xFF,0xFF,0xBF } end
if tb_cl.yl_2[k] == i then return { 0x60,0x60,0x15 } end
if tb_cl.bl_1[k] == i then return { 0xCF,0xCF,0xFF } end
if tb_cl.bl_2[k] == i then return { 0x26,0x26,0x76 } end
if tb_cl.rd_1[k] == i then return { 0xFF,0xCF,0xCF } end
if tb_cl.rd_2[k] == i then return { 0x56,0x26,0x26 } end
if tb_cl.gy_1[k] == i then return { 0xCF,0xFF,0xCF } end
if tb_cl.gy_2[k] == i then return { 0x26,0x46,0x26 } end
end
end
--timer for restart color
local function key_timer()
if ( renoise.tool():has_timer( key_timer ) ) then
renoise.tool():remove_timer( key_timer )
vb.views["WMP_KEY_"..i..""].color = restore_color_piano()
else
if not ( renoise.tool():has_timer( key_timer ) ) then
renoise.tool():add_timer( key_timer, 500 )
end
end
end
--octave
local function key_oct()
return song.transport.octave
end
song.transport.octave_observable:add_notifier( key_oct )
renoise.tool().app_new_document_observable:add_notifier( key_oct )
if ( key.note == i ) then
if not key.repeated then
i = i + 12 * key_oct()
vb.views["WMP_KEY_"..i..""].color = { 0xFF,0x00,0x00 } --red
key_timer()
end
--
return
key --> sound piano (usb keyboard)
--
end
end
With this at least the virtual piano work with sound, record the notes and mark the keys using the USB keyboard.I use 4 groups of colors on the piano, which complicates a bit, but the result is not entirely satisfactory.
Actually, I’m very surprised that the API does not have a note pressed and note released! Why is not it done like this? One key is a two position switch. Again, add a strange patch to fix it because something is missing.
Is possible to add it to the next Renoise API?To use repeated here is a real mess.How can I make a formal request so that this theme is not forgotten?
Soon I would like to build a virtual piano compatible with the usb keyboard, mouse, and MIDI input, and that it was not necessary to add strange code. I get the feeling that some things have been incomplete in the Renoise API.
For key, instead of “note” and “repeated”, It is not better “note_pressed” and “note_released” or similar?
- key.note_pressed
- key.note_released
In the end it is too complicated to build a simple custom virtual piano, for these details… It is a pity!