Click here to Skip to main content
15,898,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello fellow users of CodeProject!

So I am working on a fun little project & I am very close to being done, the only problem I have been running into is getting the keys to bind to the buttons.
I might be too close to the issue since I am currently working on two projects at the same time, one web browser & the key binds works perfectly there but when I try to use the same concept it doesnt want to respond, Did I miss something?


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
/// <summary>
/// Version: 1.0
/// Farm SoundBoard
/// Created By Me
/// Date 2015-10-29
/// Category: Fun
/// </summary>
namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.cow;
            player.Play();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.bird;
            player.Play();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.bee;
            player.Play();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.elephant;
            player.Play();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.tiger;
            player.Play();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.cat;
            player.Play();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.dog;
            player.Play();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.lion;
            player.Play();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.flies;
            player.Play();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.fish;
            player.Play();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.parrot;
            player.Play();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.car;
            player.Play();
        }
        private void button14_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        private void button1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)ConsoleKey.F1)

                    {
                System.Media.SoundPlayer player = new System.Media.SoundPlayer();
                player.Stream = Properties.Resources.cow;
                player.Play();
            }
        }
    }
}
Posted

F1 is not a character at all. You need to handle the event KeyDown, not KeyPress and use the property KeyCode of System.Windows.Forms.KeyEventArgs.KeyCode, which is of the type System.Windows.Forms. This is the enumeration type with enumeration members like F1, F2, Left, Right, Enter, and so on…

Please see:
https://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keycode%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.keys%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
I'm not sure, but I don't think you want to use the KeyPress event on the Button. It would only be triggered if the button had focus. (E.g., you had tabbed to it.)
I think you should use the KeyPress event on the Form itself.
 
Share this answer
 
Comments
BladeLogan 9-Nov-15 21:14pm    
@Matt_T_Heffron So it would be the same code but I need to use it on the form.
BladeLogan 9-Nov-15 21:19pm    
Beucase if I do that it gives me the error

Severity Code Description Project File Line
Warning CS0642 Possible mistaken empty statement WindowsFormsApplication6 C:\Users\User\Documents\Visual Studio 2015\Projects\WindowsFormsApplication6\Form1.cs 125
BladeLogan 9-Nov-15 21:23pm    
Nevermind! I was pressing F1 while it was still bound to Enter Thank you!
BladeLogan 9-Nov-15 21:24pm    
Turns out it only worked because the button was selected while the software was started. Back to square one.
Set the Form 'KeyPreview property to 'true, and write an event-handler for the Keydown event on the Form:
C#
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.F1:
            // do whatever
            break;
    }
}
If that doesn't work for some reason (like you have some DirectX using Control which has the current focus and is not forwarding key-messages to the WinForms), then try this:
C#
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    switch (keyData)
    {
        case Keys.F1:
            // do whatever
            break;
    }

    return base.ProcessCmdKey(ref msg, keyData);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900