Click here to Skip to main content
11,927,422 members (46,808 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked

A Piano Key Control in C#

, 22 Feb 2004
Rate this:
Please Sign up or sign in to vote.
A Piano Key Control for the .NET Framework


Many months ago, I created a piano control using MFC. Having begun using C# over the past year, I decided recently to recreate the control for the .Net framework. However, after taking another look at the challenge of representing the piano keyboard as a GUI control, I decided to take a different approach.

Piano Keys

One of the challenges of creating the piano control for MFC was deciding how to represent the piano keys. The first version of the control divided each key into one or more rectangles. In the second version, I switched to using regions. With both approaches, each piano key was simply a set of coordinates encapsulated in piano key classes and held by the CPianoCtrl class. The task of determining which key is being played fell to this class and had to be done manually. This involved a certain amount of grunt work with capturing mouse events and using hit detection. I wanted to avoid this with the C# version/.Net version.

To overcome having to detect manually which key is being played, I made the piano key itself a control. Each piano key, then, is responsible for responding to mouse events; each key knows when it is being held and released. All that is necessary is for the piano key control to listen for mouse events and respond appropriately when they occur. This made the task of managing the piano keys and mouse events much easier.


There are four basic piano key shapes:

  • L Shaped (notes C and F)
  • Backwards L Shaped (notes E and B)
  • Upside down T Shaped (notes D, G, and A)
  • Rectangled Shaped (all flat keys)

With the MFC piano control, I created a hierarchy of piano key classes representing these different shapes. In retrospect, this may have been overkill. With the C# version, I created a single piano key control class and made the key shape a property of this class.


In addition to shapes, piano keys, at least the GUI version of them, also have orientation. A piano key can be oriented vertically or horizontally. When oriented horizontally, the back of the key can can face either to the left or right. Orientation is another aspect I made a property of the piano key control class.

Piano Key Events

The StateChanged event is the only event that is generated when a piano key is triggered or released. After receiving this event, you can query the state of the key using the IsKeyOn() method. This method returns true if the key has been turned on or false if it is off.

Demonstration Application

The demo shows the PianoKey control with various shapes and orientations. There is a collection of PianoKey controls grouped together to form a piano keyboard. One approach that I have found useful is to use the Tag property of the Control class for storing data relevant to an individual control. I used this approach with the group of piano keys giving each one of them an id number that can be retrieved when the StateChanged event is triggered.


In the future, I may create a piano control class which manages a collection of piano keys. But for now, I think the PianoKey control class can be useful in and of itself. I hope you enjoy this class, and I welcome any suggestions or comments.


  • 02/09/2004 Article Submitted


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Leslie Sanford
United States United States
Aside from dabbling in BASIC on his old Atari 1040ST years ago, Leslie's programming experience didn't really begin until he discovered the Internet in the late 90s. There he found a treasure trove of information about two of his favorite interests: MIDI and sound synthesis.

After spending a good deal of time calculating formulas he found on the Internet for creating new sounds by hand, he decided that an easier way would be to program the computer to do the work for him. This led him to learn C. He discovered that beyond using programming as a tool for synthesizing sound, he loved programming in and of itself.

Eventually he taught himself C++ and C#, and along the way he immersed himself in the ideas of object oriented programming. Like many of us, he gotten bitten by the design patterns bug and a copy of GOF is never far from his hands.

Now his primary interest is in creating a complete MIDI toolkit using the C# language. He hopes to create something that will become an indispensable tool for those wanting to write MIDI applications for the .NET framework.

Besides programming, his other interests are photography and playing his Les Paul guitars.

You may also be interested in...

Comments and Discussions

GeneralAssociate Sound Pin
rowiz23-Mar-07 2:54
memberrowiz23-Mar-07 2:54 
GeneralRe: Associate Sound Pin
Leslie Sanford23-Mar-07 4:14
memberLeslie Sanford23-Mar-07 4:14 
QuestionPiano Control? Pin
sj266-Jun-04 7:31
membersj266-Jun-04 7:31 
AnswerRe: Piano Control? Pin
Leslie Sanford6-Jun-04 7:36
memberLeslie Sanford6-Jun-04 7:36 
GeneralRe: Piano Control? Pin
shofb3-Jun-05 19:36
membershofb3-Jun-05 19:36 
GeneralRe: Piano Control? Pin
Leslie Sanford4-Jun-05 0:53
memberLeslie Sanford4-Jun-05 0:53 
QuestionI suppose just making them overlapping rectangles was out of the question? Pin
Anonymous24-Feb-04 11:46
sussAnonymous24-Feb-04 11:46 
AnswerRe: I suppose just making them overlapping rectangles was out of the question? Pin
Leslie Sanford24-Feb-04 12:29
memberLeslie Sanford24-Feb-04 12:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 23 Feb 2004
Article Copyright 2004 by Leslie Sanford
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid