MidiKeyboard response to held computer keys

When I press certain letters on the computer keyboard it triggers notes in the MidiKeyboardComponent (an excellent feature, thanks Jules!)

However, if I hold any of those computer keys down for more than a couple seconds I begin to get the “dong” sound that is normally generated when you hit a key that doesn’t have a shortcut. And it happens over and over in a machine-gun fashion. This doesn’t happen when I hold down shortcut keys assigned to other functionalities in my program, only the MidiKeyboardComponent’s. Even stranger, if I press another key to trigger a MidiKeyboard note, WHILE holding down the other “dong” generating key, the dongs go away.

Is this a bug, or is there an easy way to disable this that I’m missing?

Yeah, I do know about that, just never got around to looking into it. It’s probably just because the keyPressed method is returning false or something simple like that.

Looks like it is returning false. I created my own KeyboardComponent subclass and overrode the KeyPress method:

bool MyKeyboardComponent::keyPressed (const KeyPress &key)
{
	bool result = false;
	
	if (key == KeyPress('a') || key == KeyPress('w') || key == KeyPress('s') || key == KeyPress('e') || 
		key == KeyPress('d') || key == KeyPress('f') || key == KeyPress('t') || key == KeyPress('g') || 
		key == KeyPress('y') || key == KeyPress('h') || key == KeyPress('u') || key == KeyPress('j') || 
		key == KeyPress('k') || key == KeyPress('o') || key == KeyPress('l') || key == KeyPress('p') || 
		key == KeyPress(';')) 
	{
		result = true;
	}
	
	return result;
}

and now the “donging” has stopped. Thanks Jules.

For a D.R.Y. fanatic like me, it’s painful to just look at that code!

Why not just “return true”?

In other words, you can skip the variable declaration to further refine your code:

bool MyKeyboardComponent::keyPressed (const KeyPress &key)
{   
   if (key == KeyPress('a') || key == KeyPress('w') || key == KeyPress('s') || key == KeyPress('e') || 
      key == KeyPress('d') || key == KeyPress('f') || key == KeyPress('t') || key == KeyPress('g') || 
      key == KeyPress('y') || key == KeyPress('h') || key == KeyPress('u') || key == KeyPress('j') || 
      key == KeyPress('k') || key == KeyPress('o') || key == KeyPress('l') || key == KeyPress('p') || 
      key == KeyPress(';')) 
   {
      return true;
   }
   
   return false;
}

No, the unnecessary variable is the least of the problems there!

I’d have written something more like:

When you look at your code, always ask yourself “how well could this be gzipped?” In general, if gzip would achieve a high compression ratio, it’s a bad sign!


Hah that's awesome. DRY indeed!

Hah that’s awesome. DRY indeed!

Before I used that very WET technique, I tried just returning true. But it messed with the keypresses that triggered the octave switch, so I had to get more specific. Your solution is much more graceful than mine though, thanks.