5,696,576 members and growing! (16,066 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Miscellaneous Controls     Intermediate

A Piano Key Control in C#

By Leslie Sanford

A Piano Key Control for the .NET Framework
C#, Windows, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 22 Feb 2004
Updated: 22 Feb 2004
Views: 96,624
Bookmarked: 14 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 4.64 Rating: 3.77 out of 5
3 votes, 17.6%
1
0 votes, 0.0%
2
2 votes, 11.8%
3
6 votes, 35.3%
4
6 votes, 35.3%
5

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


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.

Location: United States United States

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralAssociate Soundmemberrowiz2:54 23 Mar '07  
GeneralRe: Associate SoundmemberLeslie Sanford4:14 23 Mar '07  
GeneralPiano Control?membersj267:31 6 Jun '04  
GeneralRe: Piano Control?memberLeslie Sanford7:36 6 Jun '04  
GeneralRe: Piano Control?membershofb19:36 3 Jun '05  
GeneralRe: Piano Control?memberLeslie Sanford0:53 4 Jun '05  
GeneralI suppose just making them overlapping rectangles was out of the question?sussAnonymous11:46 24 Feb '04  
GeneralRe: I suppose just making them overlapping rectangles was out of the question?memberLeslie Sanford12:29 24 Feb '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Feb 2004
Editor: Nishant Sivakumar
Copyright 2004 by Leslie Sanford
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project