|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis one was just for fun; the article describes a project used to build a simple piano keyboard that plays some not too terrific sounding notes, courtesy of the Kernel32.dll’s
Figure 1: A Small Keyboard I suppose that if you were to look hard enough, you could probably come up with something useful that you can do with this keyboard (like annoy your cat). It could probably be turned into something instructional that could be used to teach someone the notes associated with the keys or some swell thing like that (of course, that may well take the fun out of messing with it). Further, if you were to dodge using the Getting StartedIn order to get started, unzip the attachment, and load the solution into Visual Studio 2005. Examine the Solution Explorer, and note that the project contains a class:
Figure 2: The Solution Explorer Showing the Project Files The small keyboard project’s single class is a Windows form; that form contains a collection of buttons used to simulate the appearance (if not the sound) of a piano keyboard. The CodeI’m sure that it seems inconceivable that this much raw power can be packed into a single class, but it is true; I just suppose that is part of the awe-inspiring thing we know as .NET. To have a look at the magic going on behind the scenes, open up the class into the code view window, and take a look. The first thing that you will note is that the code imports Imports System.Runtime.InteropServices
Public Class frmKeyboard
<DllImport("KERNEL32.DLL")> _
Public Shared Sub Beep(ByVal freq As Integer, ByVal dur As Integer)
End Sub
The DLL import call made here is used to bring the Kernel32.dll library into the project; the Kernel32.dll contains the Now onto the next bit of magic; the button handlers. This bit of code makes it possible to produce beautiful strains of high quality music through the keyboard: Private Sub Play_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles btnMC.KeyDown, btnHA.KeyDown, btnHAs.KeyDown, _
btnHB.KeyDown, btnHC.KeyDown, btnHCs.KeyDown, _
btnHD.KeyDown, btnHDs.KeyDown, btnHE.KeyDown, _
btnHF.KeyDown, btnHFs.KeyDown, btnHG.KeyDown, _
btnHGs.KeyDown, btnLA.KeyDown, btnLAs.KeyDown, _
btnLB.KeyDown, btnLC.KeyDown, btnLCs.KeyDown, _
btnLD.KeyDown, btnLDs.KeyDown, btnLE.KeyDown, _
btnLF.KeyDown, btnLFs.KeyDown, btnLG.KeyDown, _
btnLGs.KeyDown, btnMA.KeyDown, btnMAs.KeyDown, _
btnMB.KeyDown, btnMC.KeyDown, btnMCs.KeyDown, _
btnMD.KeyDown, btnMDs.KeyDown, btnME.KeyDown, _
btnMF.KeyDown, btnMFs.KeyDown, btnMG.KeyDown, _
btnMGs.KeyDown, Me.KeyDown
Me.Focus()
Select Case e.KeyData.ToString()
Case "A"
Me.btnMC_Click(sender, e)
Case "S"
Me.btnMD_Click(sender, e)
Case "D"
Me.btnME_Click(sender, e)
Case "F"
Me.btnMF_Click(sender, e)
Case "G"
Me.btnMG_Click(sender, e)
Case "H"
Me.btnMA_Click(sender, e)
Case "J"
Me.btnHC_Click(sender, e)
Case "K"
Me.btnHD_Click(sender, e)
Case "L"
Me.btnHE_Click(sender, e)
Case "Z"
Me.btnHF_Click(sender, e)
Case "X"
Me.btnHG_Click(sender, e)
Case "C"
Me.btnHA_Click(sender, e)
End Select
End Sub
Taking a look at this code, you will note that it handles all of the key down events associated with each of the buttons. A Next up is the button click event handlers; since they are all pretty much the same, I will only show one in this document, the event handler for middle C: Private Sub btnMC_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnMC.Click
' middle C
Beep(261, 150)
End Sub
As advertised, this event handler evokes the SummaryThe project demonstrates a few useful things like using the DLL Import function supported by the InteropServices library, but overall, it is just for fun.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||