Mini Keyboard Source (Piano)

If you have completed an application and wish to share the complete source/project files with everyone then please post it in here. Source-code files only, no tutorials.
8 posts Page 1 of 1
Contributors
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Mini Keyboard Source (Piano)
smashapps
Here is a small app I have thrown together, I have no piano sounds so I couldn't finish it but I'm sure if you want to use this you could find some keyboard sound effects, I might have updated versions in the future, who knows.

The keyboard has mouse over effects so the keys change colors when you move the mouse over.

Screenshot:

Image

(Ps if you like my source please +rep :D)

Source:


(Check out SmashApps's website at http://smashapps.net for more tutorials and downloads)
You do not have the required permissions to view the files attached to this post.
Last edited by smashapps on Sat Apr 02, 2016 2:26 pm, edited 2 times in total.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Mini Keyboard Source (Piano)
mandai
You can generate keyboard sounds by using MIDI commands.
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Mini Keyboard Source (Piano)
smashapps
Oh really, Could you give me a code sample by any chance?

Thanks in advance.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Mini Keyboard Source (Piano)
mandai
You can use this:
Code: Select all
    <DllImport("winmm.dll")> Shared Function midiOutOpen(ByRef lphMidiOut As IntPtr, ByVal uDeviceID As UInteger, ByVal dwCallback As IntPtr, ByVal dwInstance As IntPtr, ByVal dwFlags As UInteger) As UInteger
    End Function

    <DllImport("winmm.dll")> Shared Function midiOutShortMsg(ByVal hMidiOut As IntPtr, ByVal dwMsg As UInteger) As UInteger
    End Function

    <DllImport("winmm.dll")> Shared Function midiOutReset(ByVal hMidiOut As IntPtr) As UInteger
    End Function

    <DllImport("winmm.dll")> Shared Function midiOutClose(ByVal hMidiOut As IntPtr) As UInteger
    End Function

    Dim hMidi As IntPtr

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        midiOutOpen(hMidi, 0, IntPtr.Zero, IntPtr.Zero, 0)
    End Sub

    Dim down As Boolean = False

    Private Sub Panel1_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
        down = False
        midiOutReset(hMidi) 'release any held keys
    End Sub

    Private Sub Panel1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
        down = True
    End Sub

    Private Sub Panel1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
        If down Then

            If e.X > 0 And e.X < Panel1.Width  Then

                Dim command As Byte = &H90 'note on
                Dim note As Byte = e.X / (Panel1.Width / 127)
                Dim volume As Byte = 127

                Dim msgBytes As Byte() = {command, note, volume, 0}

                Dim msg As UInteger = BitConverter.ToUInt32(msgBytes, 0)
                midiOutShortMsg(hMidi, msg)

            End If

        End If
    End Sub

    Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        midiOutClose(hMidi)
    End Sub
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Mini Keyboard Source (Piano)
smashapps
I got your code to work but its hard to use for what I need, your code uses a Panel and every key is inside of that panel.

How would I use this code for each key?

Thanks.

EDIT: I'm using 29 pictureboxes.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Mini Keyboard Source (Piano)
mandai
You could either code in 29 different notes per picturebox, or you could use a single panel and calculate/draw bars using the Paint event.
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Mini Keyboard Source (Piano)
smashapps
I would rather do it for the 29 keys, would be able to give me an example for the first key and the second key? then I work it out there

Thanks.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Mini Keyboard Source (Piano)
mandai
This would do 2 keys:
Code: Select all
    Dim command As Byte = &H90 'note on
    Dim volume As Byte = 127

    Private Sub PictureBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        Dim note As Byte = 20

        Dim msgBytes As Byte() = {command, note, volume, 0}

        Dim msg As UInteger = BitConverter.ToUInt32(msgBytes, 0)
        midiOutShortMsg(hMidi, msg)

    End Sub

    Private Sub PictureBox2_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown

        Dim note As Byte = 40

        Dim msgBytes As Byte() = {command, note, volume, 0}

        Dim msg As UInteger = BitConverter.ToUInt32(msgBytes, 0)
        midiOutShortMsg(hMidi, msg)

    End Sub

    Private Sub PictureBox_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp, PictureBox1.MouseUp
        midiOutReset(hMidi)
    End Sub
8 posts Page 1 of 1
Return to “Source-Code”