I’m taking my first baby steps into VCV Rack development.
The first module I’m trying to make is very simple - something that will send triggers / gates when the space bar is pressed (for starting/stopping clocks and sequencers). I got a very basic version of it working (sets output to 10v when space bar is pressed, 0v when released). It only works when the mouse is hovered over the module. I’m wondering if that is expected, and a limitation of the way Rack deals with the keyboard (maybe Rack doesn’t want modules to get keyboard events unless hovered)? Or is there a way to get a module to listen to keyboard events no matter what?
Indeed, for this type of functionality I much prefer mapping a midi controller button. Ideally, once I’ve set a patch up and am playing it, I don’t want to have to go anywhere near PC keyboard / mouse.
You can capture focus, once you have it, and steal the space bar. But you will lose focus when the user clicks on a different module. Our sequencer, Seq++ does exactly that. We also sometimes “steal” keys that VCV uses itself, like the cursor keys.In general taking keyboard focus works fine for modules where it’s reasonable to expect a user to stay in your module for a while. Fighting with VCV for shared keys is less good - so we offer alternative keys that don’t conflics.
I’m not sure if this module really makes sense unless it can always hear the space bar. The idea is to make it easy to start/stop clocks or sequences with the space bar, no matter where you are in the patch. I have a habit of this (and I know some other people do too) from working with other DAWs. Having to navigate back to the module to give it focus defeats the purpose (and Impromptu Clocked already starts/stops with space bar on hover - but you have to hover).
I’m curious how MIDI-CV and MIDI-GATE handle it when using the computer keyboard or numpad as the “MIDI” source. Those modules don’t need seem to need focus to hear the keyboard. Do they have a special status? I’m taking a look at the code (but I’m really just learning my way around so I don’t understand it yet).
The keyboard MIDI driver doesn’t watch for the space bar. Its behavior is defined in this file. If you forked this driver, you could make your own version which watches for GLFW_KEY_SPACE, and then you could code your module to output a gate when it is pressed (something like MIDI-CV or MIDI-GATE). Or you could just use one of those two modules in conjunction with your plugin’s MIDI driver. You won’t actually be able to implement your own MIDI driver in a plugin until Rack v2, though:
Out of curiosity I tried simply replacing GLFW_KEY_Z with GLFW_KEY_SPACE in keyboard.cpp and building Rack. As expected this maps Space to MIDI note C (previously mapped to the Z key), allowing MIDI-CV and MIDI-GATE to output a gate when it is pressed. So, for your own use before Rack v2, you could simply do something like this.