Lost keyPressed events

please format code with </> button * homework policy * asking questions

I’m using the SHIFT and ALT keys as modifiers on mouse operations, but seem to be losing keyPressed events on the ALT key. Simple code example below. After hitting the ALT key a few dozen times, the ‘down’ count end up much less (about half) than the ‘up’ count.

The problem only seems to occur with the default JAVA2D renderer. P2D seems OK, but has other issues for me.

Any help much appreciated.


int sD, sU, aD, aU;

void setup() {
  size(400, 200);
  textSize(24);
}

void draw() {
  background(0);
  text(sU, 40, 40);
  text(sD, 40, 80);
  text(aU, 120, 40);
  text(aD, 120, 80);
}

void keyPressed() {
  if (key == CODED) {
    switch (keyCode) {
      case SHIFT: sD++; break;
      case ALT  : aD++; break;
      default:
    }
  }
}

void keyReleased() {
  if (key == CODED) {
    switch (keyCode) {
      case SHIFT: sU++; break;
      case ALT:   aU++; break;
      default:
    }
  }  
}

Oh, and I’m running Processing 3.5.4, Java 8 Update 161, Windows 10.

OK, another symptom…
If I do a mouse click in the sketch window before each ALT key press, none of them get missed. It’s almost as though something in Processing gets confused about where the focus is - but just for ALT?

Hmm, looks like ALT toggles some boolean state in Processing/JAVA2D which when set blocks SHIFT, CTRL and ALT KeyEvents from reaching the sketch. A second ALT or a mouse click on the sketch window unblocks it.
Anyone have any ideas as to what’s going on? Or even better, how to prevent it?