0

I am making a program that will have rectanglesin the shape of a piano. I wanted to write to the console when one key is pressed, but when I press a key, nothing happens. In the fxml I have each rectangle's onMouseClicked equal to #keyPressed, so it should activate the code.

@FXML
    public void keyPressed(Event e) {
        String keyPressed = ((Control)e.getSource()).getId();

        System.out.println(keyPressed + " Key");
        playNote(60, 1);
    }

Rectangle code:

<Rectangle id="A0" arcHeight="5.0" arcWidth="5.0" fill="WHITE" height="336.0" onMouseClicked="#keyPressed" stroke="BLACK" strokeType="INSIDE" styleClass="whiteKey" width="37.0" />

I couldn't get the solution here to work. Thanks for your help.

  • _When I press a key, nothing happens._ And, yet, you have **onMouseClicked**. Mouse event isn't the same as keyboard events. – zlakad Jul 14 '18 at 19:56
  • I want a mouse event. I'm pressing a piano key in the program, not on the keyboard. – BlackVoltageBelt Jul 14 '18 at 23:21
  • 1
    Since the `Rectangle` is the source of the event and `Rectangle` is not a subtype of `Control`, `((Control)e.getSource())` yields a `ClassCastException`. If the error is a different one, you've certainly not added enough information to identify/reproduce the issue. – fabian Jul 15 '18 at 02:14

0 Answers0