if (e.KeyChar == (char)ConsoleKey.F1) 在 C# .Net 中不响应 Visual Studio

if (e.KeyChar == (char)ConsoleKey.F1) Doesnt respond in C# .Net Visual Studio

你好 Whosebug 的各位用户

所以我正在做一个有趣的小项目,我非常接近完成,我 运行 遇到的唯一问题是让键绑定到按钮。 我可能离这个问题太近了,因为我目前正在同时处理两个项目,一个网络浏览器和键绑定在那里工作得很好但是当我尝试使用相同的概念时它不想回应,我错过了什么吗?

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();
            }
        }
    }
}

我认为你使用了错误的比较,ConsoleKey 通常用于 Console.ReadKey。请尝试使用 Keys 枚举。