Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C#] Keyboard (Piano)
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sat Oct 13, 2007 11:26 pm    Post subject: [C#] Keyboard (Piano) Reply with quote

So basically, this is a Piano thing, that allows you to click a button, and that makes a sound corresponding to a specific note on a real piano.

This makes an API call to the kernel32.dll, and uses the BEEP function.

And sure, I could have dragged out every single button, and what not, but why do that when I can generate it? =D

~~~

Also, I made a menu, from which to load up the actual keyboard, and to load up the about, so here it is:

Code:

//Menu
using System;
using System.Windows.Forms;

namespace Keyboard
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        public static Keyboard keys = null;
        public static Main main = new Main();

        private void zeKeyboard_Click(object sender, EventArgs e)
        {
            keys = new Keyboard();
            this.Hide();
            keys.Show();
            keys.Focus();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            main = this;
        }

        private void zeAbout_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.Show();
        }
    }
}


~~~
The actual keyboard:

Code:

//Keyboard
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Keyboard
{
    public partial class Keyboard : Form
    {
        public Keyboard()
        {
            InitializeComponent();

            _fipairs.Add(new FIPair(110, "A2"));
            _fipairs.Add(new FIPair(116, "As2"));
            _fipairs.Add(new FIPair(123, "B2"));
            _fipairs.Add(new FIPair(130, "C3"));
            _fipairs.Add(new FIPair(138, "Cs3"));
            _fipairs.Add(new FIPair(146, "D3"));
            _fipairs.Add(new FIPair(155, "Ds3"));
            _fipairs.Add(new FIPair(164, "E3"));
            _fipairs.Add(new FIPair(174, "F3"));
            _fipairs.Add(new FIPair(185, "Fs3"));
            _fipairs.Add(new FIPair(196, "G3"));
            _fipairs.Add(new FIPair(207, "Gs3"));
            _fipairs.Add(new FIPair(220, "A3"));
            _fipairs.Add(new FIPair(233, "As3"));
            _fipairs.Add(new FIPair(246, "B3"));
            _fipairs.Add(new FIPair(261, "C4"));
            _fipairs.Add(new FIPair(277, "Cs4"));
            _fipairs.Add(new FIPair(293, "D4"));
            _fipairs.Add(new FIPair(311, "Ds4"));
            _fipairs.Add(new FIPair(329, "E4"));
            _fipairs.Add(new FIPair(349, "F4"));
            _fipairs.Add(new FIPair(369, "Fs4"));
            _fipairs.Add(new FIPair(392, "G4"));
            _fipairs.Add(new FIPair(415, "Gs4"));
            _fipairs.Add(new FIPair(440, "A4"));
            _fipairs.Add(new FIPair(466, "As4"));
            _fipairs.Add(new FIPair(493, "B4"));
            _fipairs.Add(new FIPair(523, "C5"));
            _fipairs.Add(new FIPair(554, "Cs5"));
            _fipairs.Add(new FIPair(587, "D5"));
            _fipairs.Add(new FIPair(622, "Ds5"));
            _fipairs.Add(new FIPair(659, "E5"));
            _fipairs.Add(new FIPair(698, "F5"));
            _fipairs.Add(new FIPair(739, "Fs5"));
            _fipairs.Add(new FIPair(783, "G5"));
            _fipairs.Add(new FIPair(830, "Gs5"));
            _fipairs.Add(new FIPair(880, "A5"));
            _fipairs.Add(new FIPair(932, "As5"));
            _fipairs.Add(new FIPair(987, "B5"));
            _fipairs.Add(new FIPair(1046, "C6"));


            int XPos = 0;
            Size rsize = new Size(20, 100); //regular key size
            Size ssize = new Size(18, 60); //sharp size

            foreach (FIPair f in _fipairs)
            {
                Button button = new Button();
                button.Location = new System.Drawing.Point(XPos, 0);
                button.Name = f.Name;
                button.TabIndex = 0;
                button.Font = new Font("OCR A Extended", 6, FontStyle.Bold);
                button.Tag = f.Frequency;
                button.Text = f.Name.Remove(1);
                button.UseVisualStyleBackColor = true;
                button.MouseDown += new MouseEventHandler(button_MouseDown);
                button.MouseUp += new MouseEventHandler(button_MouseUp);

                if (f.Sharp)
                {
                    button.Size = ssize;
                    button.BackColor = Color.Black;
                    button.ForeColor = Color.White;
                    button.Text += "#";
                }
                else
                {
                    button.Size = rsize;
                    button.BackColor = Color.White;
                    button.ForeColor = Color.Black;
                }

                XPos += button.Size.Width - 1;
                this.Controls.Add(button);
            }

            this.Width = XPos + 10; //Dunno why, but it works! =)
        }

        private void Keyboard_FormClosed(object sender, FormClosedEventArgs e)
        {
            Main.main.Show();
            Main.main.Focus();
        }

        //Things relating the actual keyboard are below

        private List<FIPair> _fipairs = new List<FIPair>();
        private Sounds sound = new Sounds();
        private UInt32 Frequency = 0;


        private void timer1_Tick(object sender, EventArgs e)
        {
            sound.PlaySound(Frequency);
        }

        private void Go()
        {
            timer1.Enabled = true;
        }

        private void Stop()
        {
            timer1.Enabled = false;
        }

        private void button_MouseDown(object sender, MouseEventArgs e)
        {
            Button button = (Button)sender;
            Frequency = UInt32.Parse(button.Tag.ToString());
            Go();
            sound.PlaySound(Frequency);
        }

        private void button_MouseUp(object sender, MouseEventArgs e)
        {
            Stop();
        }
    }

    public class FIPair //Originally named Frequency-Identifier pair, but we added a sharp boolean,
    { //and by this time, it would have been a major pain to go back and rename everything, so we just left it
        public FIPair(uint frequency, string name)
        {
            _frequency = frequency;
            _name = name;
        }

        private uint _frequency;
        public uint Frequency
        {
            get { return _frequency; }
            set { _frequency = value; }
        }

        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public bool Sharp
        {
            get { if (Name.Contains("s")) return true;
                    else return false; }
        }

    }

}


~~~

The place where I call the BEEP function

Code:

//Sounds
using System;
using System.Runtime.InteropServices;

namespace Keyboard
{
    public class Sounds
    {

        [DllImport("kernel32.dll")]
        public static extern bool Beep(UInt32 dwFreq, UInt32 dwDuration);

        public void PlaySound(UInt32 Frequency)
        {
            Beep(Frequency,300);
        }
    }
}


~~~

I'm not going to add the About class because it'd be a waste of both of our time--seeing as how I didn't make any changes to it.

~~~

Here is the compiled version.

If you have any questions/comments about the code or anything, please post! =)
Back to top
View user's profile Send private message
appalsap
Moderator
Reputation: 0

Joined: 27 Apr 2006
Posts: 6753
Location: Pakistan

PostPosted: Sat Oct 13, 2007 11:56 pm    Post subject: Reply with quote

It's a shame you can't actually use the keyboard to make the piano sounds.
Back to top
View user's profile Send private message
compactwater
I post too much
Reputation: 8

Joined: 02 Aug 2006
Posts: 3923

PostPosted: Sun Oct 14, 2007 1:10 am    Post subject: Reply with quote

appalsap wrote:
It's a shame you can't actually use the keyboard to make the piano sounds.
With MIDI you can.
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sun Oct 14, 2007 9:58 am    Post subject: Reply with quote

If you're talking about the keyboard that you type with, it is possible, but unfortunately, I've had bad experiences with things like that;

for example, hotkeys don't work when a button is on the form. I'll see what I can do, though.

Edit:

Now that I think about it, even if I could get it to work like that, there wouldn't be enough keys on the keyboard to reall be able to manuever (sp?)--there aren't enough keys on the actual keyboard (the one that we type with) so that they could be on a straight line.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Oct 14, 2007 12:31 pm    Post subject: Reply with quote

You could try overriding ProcessCmdKey for the whole keyboard thing.
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sun Oct 14, 2007 3:34 pm    Post subject: Reply with quote

ProcessCmdKey?

o.O

I've never heard of that--and it doesn't sound like it has anything to do with syncing a keyboard to an application.

Mind explaining?
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Oct 14, 2007 3:47 pm    Post subject: Reply with quote

Code:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
     switch (keyData)
     {
         case Keys.Control|Keys.X:
             MessageBox.Show("omgwtf");
             return true;
     }
     return false;
}
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Mon Oct 15, 2007 2:10 pm    Post subject: Reply with quote

I see...

So I could make that instead of doing the message box, make it call the button down event... and the button_down event require a variable, which would be the tag for most things, but what would I do for detecting the button_up/key_up?
Back to top
View user's profile Send private message
GFCM
Advanced Cheater
Reputation: 0

Joined: 03 Oct 2007
Posts: 62

PostPosted: Mon Oct 15, 2007 2:17 pm    Post subject: Reply with quote

sorry if im being retarded, i am not sure (100%) if slovac said just what im about to say.. if he said that, then sorry, he has the credit for it Very Happy

well lets say you got 21 keys Q -> U + A -> J + Z -> M

with ALT pressed + something, u get 42 notes

with CTRL pressed + something, u get 63 notes

with Shift pressed + something, u get 84 notes

guess you got the idea..

sorry about my english too, not my native language..

(i guess its not a problem but i could give u the case code for that in C#)
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Oct 17, 2007 8:25 pm    Post subject: Reply with quote

I'm not quite getting you--do you mean like do:

For example,

Type - Note
A = C
S = D
D = E

However, with like CTRL pressed, or something, it would be like an octave higher?

(I'm sorry if you don't speak music--an octave is a set of notes, {C,D,E,F,G,A,B,C} + their sharps/flats, and then it repeats on)
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Wed Oct 17, 2007 8:58 pm    Post subject: Reply with quote

YOu know what I Realized. I saw the exact same thing on the code project. Weird huh? Wink
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Thu Oct 18, 2007 4:25 am    Post subject: Reply with quote

mind showing us? i just googled 3 snippets of his source, all would seem common, 0 results

if you make accusations back it up with a link

_________________
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Thu Oct 18, 2007 6:37 am    Post subject: Reply with quote

I assure you that I didn't take this from anyone; my dad and I worked on this--and he showed me the dynamic button creation, from (and I quote him here), "the bad ol' days".

If you really DID find someone's thing like this, I'd like to see a page.

Edit:

I just searched CodeProject (C# of course) for anything that might have to do with this (e.g., "dynamic button generation", "keyboard", "music", etc...), and nothing has turned up fruitful.

Please don't make such accusations; if you really have found something, I'm quite sorry, but I've seriously found nothing.
Back to top
View user's profile Send private message
GFCM
Advanced Cheater
Reputation: 0

Joined: 03 Oct 2007
Posts: 62

PostPosted: Thu Oct 18, 2007 12:58 pm    Post subject: Reply with quote

samuri, can you have like more then one octave for each note?

then you could do something like:

+ increase octave (for example.. CTRL key)
0 regular notes (anykey you would like to be a regular note..)
- decrease octave (for example.. SHIFT key)

(my biggest problem is my english, lol..)

Wink
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Thu Oct 18, 2007 3:13 pm    Post subject: Reply with quote

Sorry, it was in the vb section though. It was just that it described how it works and what it is the same way. Well almost the same way.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites