Click here to Skip to main content
Licence 
First Posted 22 Feb 2004
Views 129,277
Bookmarked 23 times

A Piano Key Control in C#

By | 22 Feb 2004 | Article
A Piano Key Control for the .NET Framework

Introduction

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.

Shapes

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.

Orientation

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.

Conclusion

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.

History

  • 02/09/2004 Article Submitted

License

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

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAssociate Sound Pinmemberrowiz1:54 23 Mar '07  
GeneralRe: Associate Sound PinmemberLeslie Sanford3:14 23 Mar '07  
Hi,
 
First off, be sure to check out the 5th[^] version of my MIDI toolkit. It has a piano control that's easier to use than my piano key control; I should probably delete this article.
 
The piano control in my MIDI toolkit can be used without having to use MIDI, so you can use it for wave playback, which sounds like what you're doing.
 
As far as associating sounds, just respond to the key up and down events and map the key ID given to you to the sounds you wish to play. You could use one sample per key, or map one sample to cover several keys and use some type of pitch transpose algorithm (or just playback the sound at a different sample rate) to get the sound to match the pitch of the key. Basic sample playback.
QuestionPiano Control? Pinmembersj266:31 6 Jun '04  
AnswerRe: Piano Control? PinmemberLeslie Sanford6:36 6 Jun '04  
GeneralRe: Piano Control? Pinmembershofb18:36 3 Jun '05  
GeneralRe: Piano Control? PinmemberLeslie Sanford23:53 3 Jun '05  
QuestionI suppose just making them overlapping rectangles was out of the question? PinsussAnonymous10:46 24 Feb '04  
AnswerRe: I suppose just making them overlapping rectangles was out of the question? PinmemberLeslie Sanford11:29 24 Feb '04  

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

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

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