Keyboard focus - only on hover?

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?


I believe that this is a deliberate limitation in Rack.

If more than module wanted to do something with the space bar, how would you control which module responded?

If I was typing into a text box, I wouldn’t necessarily want triggers going off every time I put a space between words.

1 Like

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.

You can find most of the code here. It should give you some idea how to do this:

1 Like

Thanks everyone!

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).

They ‘see’ the keyboard differently, because they Midi-cv/gate are reading a midi driver. Its the driver, not the module which is reading the keyboard.

Ah, interesting, thanks. I guess the question then is if other modules can access that driver (seems like probably yes?), and whether that driver can see the space bar.

Any module can access the same drivers that VCV can. I don’t know of any VCV modules that do something that ours can’t.

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.

That is not the whole truth: You can write your own MIDI driver in Rack v1 and it will work as expected but Rack will crash on exit. What you can’t do is write your own audio driver.

1 Like

I’m kind of late on the response, but I have a module based on a similar idea. It is called ‘Shift Pedal’ in the ‘Alikins’ plugin.

Doing something like but using the GLFW_KEY_SPACE instead of GLFW_KEY_RIGHT_SPACE seems to work.

The gist is on every Widget step() it’s checking the main APP’s window to see if a key is pressed. As opposed to handling key press events that make it to module which runs into focus issues.

(Adding support for ‘space’ bar in that plugin has been on my TODO list for a while but haven’t gotten around to it yet).

1 Like