Reputation: 925
I am making a piano with JFrame but I have a little problem. I want the key of the piano to turn green when it's pressed, and a note to play.
b.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('q'), "playD");
b.getActionMap().put("playD", playC); //playC refers to another Action class
b.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('q'), "SetBg");
b.getActionMap().put("SetBg", db); //db refers to another Action class
However, these methods override eachother. Right now, only the colour changes, and the note isn't played. If I remove the "SetBg" method, the note does play.
Is there any way to fix this?
A second problem I have is that I can't seem to get it to work to know when a key is actually released again.
I tried .put(Keystroke.getKeyStroke("released q"), "DoSomething");
But that doesn't seem to do anything.
Thanks in advance!
Upvotes: 1
Views: 296
Reputation: 285403
I'm not an expert on this, but I don't think that you can add two key bindings on the same key stroke without the 2nd binding blocking the first and all previous bindings. In other words, I believe that only a single binding is possible for each specific keystroke and input map.
Having said that, I'd do this differently:
Upvotes: 1