if(e.KeyChar ==(char)ConsoleKey.F1)没有回应 [英] if (e.KeyChar == (char)ConsoleKey.F1) Doesnt Respond
问题描述
您好CodeProject的用户!
所以我正在开发一个有趣的小项目&我非常接近完成,我遇到的唯一问题就是让键绑定到按钮。
我可能太接近问题,因为我目前正在处理两个项目同时,一个网络浏览器&关键绑定在那里工作得很好但是当我尝试使用相同的概念它不想回应时,我是否错过了什么?
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?
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();
}
}
}
}
推荐答案
F1是根本不是一个角色。您需要处理事件KeyDown
,而不是KeyPress
并使用属性KeyCode
ofSystem.Windows.Forms.KeyEventArgs.KeyCode
,其类型为System.Windows.Forms
。这是具有枚举成员的枚举类型,如F1,F2,Left,Right,Enter等等......
请参阅:
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.m icrosoft.com/en-us/library/system.windows.forms.keys%28v=vs.110%29.aspx [ ^ ]。
-SA
F1 is not a character at all. You need to handle the eventKeyDown
, notKeyPress
and use the propertyKeyCode
ofSystem.Windows.Forms.KeyEventArgs.KeyCode
, which is of the typeSystem.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
我不确定,但我认为你不想在Button上使用KeyPress事件。只有按钮具有焦点时才会触发。 (例如,你已经选中了它。)
我认为你应该在Form本身上使用KeyPress事件。
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.
将Form'KeyPreview属性设置为' true,并在Form上为Keydown事件编写事件处理程序:
Set the Form 'KeyPreview property to 'true, and write an event-handler for the Keydown event on the Form:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.F1:
// do whatever
break;
}
}
如果由于某种原因这不起作用(比如你有一些使用Control的DirectX,它具有当前焦点而不是将密钥消息转发给WinForms) ,然后试试这个:
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:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.F1:
// do whatever
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
这篇关于if(e.KeyChar ==(char)ConsoleKey.F1)没有回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!