Rumesh Madhusanka
Rumesh Madhusanka

Reputation: 1415

How to implement key listener?

For educational purposes, I am building a MIDI piano applet in Java. For that I want to:

  1. Capture key presses and releases
  2. Calculate the time between the same key's press and release.

What is the best way to implement key listener satisfying the above criteria?

Having only two key listeners (for key press and release) or having key listener per key?

Upvotes: 0

Views: 129

Answers (1)

Stephen C
Stephen C

Reputation: 718708

Having only two key listeners (for key press and release) or having key listener per key?

Neither. I think you need a single listener that handles both key press and key release events ... for all of the keys. Each event includes a when attribute that you can use to compute how long a key was held down.

Finally, note that applets are now deprecated technology, so the educational value of doing this is limited, as is the practical value of the code you write. Most browsers removed all Java plugin / applet support some time ago.

Upvotes: 1

Related Questions