C# 关于Form中键盘事件的问题。

spade24 2010-04-30 10:44:33
1.在form1中添加一键盘事件:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.Up)
{……}
if(e.KeyCode==Keys.Down)
{……}
}


上述操作,只使用上下箭头不好用,必须是其它键(比如Control或Alt)+上下键这样组合使用!很奇怪!这是为什么呢?
...全文
256 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangwenzhuang 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 spade24 的回复:]
1.我在form中做的键盘事件,然后在Picturebox中使用,这样用应该也没问题的吧?
[/Quote]
不会触发吧
spade24 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yangfan1981 的回复:]
另外,是否已经定义了相同的按键事件?
[/Quote]

是有定义。但是我根据不同条件分开的,比如:

bool b1=false,b2=false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(b1)
{
if(e.KeyCode==Keys.Up)
{……}
if(e.KeyCode==Keys.Down)
{……}
}
if(b2)
{
if(e.KeyCode==Keys.Up)
{……}
if(e.KeyCode==Keys.Down)
{……}
}

}



这样有问题吗?
yangfan1981 2010-04-30
  • 打赏
  • 举报
回复
另外,是否已经定义了相同的按键事件?
zrrsj 2010-04-30
  • 打赏
  • 举报
回复
.................
yangfan1981 2010-04-30
  • 打赏
  • 举报
回复
不应该啊!检查下键盘上的ctrl 是不是卡住了。 呵呵!
spade24 2010-04-30
  • 打赏
  • 举报
回复
1.我在form中做的键盘事件,然后在Picturebox中使用,这样用应该也没问题的吧?
Justin-Liu 2010-04-30
  • 打赏
  • 举报
回复
很诡异。。。 你给分儿不公平
充值内卷 2010-04-30
  • 打赏
  • 举报
回复
我刚才试了 没这个问题 你键盘是不是坏了 代码贴给你
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestKeyDown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode==Keys.Up)
{
int i = 1;
}
}
}
}


Form1.Designer.cs
namespace TestKeyDown
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "Form1";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.ResumeLayout(false);

}

#endregion
}
}
spade24 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wangfenfen 的回复:]
因为winform中的键盘事件大多数都是以组合键的形式出现的
如:F1。。。F12
ShiftF1。。。。shiftF12
Ctrl0。。。。Ctrl9
CtrlA。。。。CtrlZ
CtrlF1。。。。CtrlF12
CtrlShift0。。。。CtrlShift9
CtrlShiftA。。。。CtrlShiftZ
Alt0。。。。Alt9
Al……
[/Quote]


谢谢你的回答。但是你的问答很牵强。
wangfenfen 2010-04-30
  • 打赏
  • 举报
回复
因为winform中的键盘事件大多数都是以组合键的形式出现的
如:F1。。。F12
ShiftF1。。。。shiftF12
Ctrl0。。。。Ctrl9
CtrlA。。。。CtrlZ
CtrlF1。。。。CtrlF12
CtrlShift0。。。。CtrlShift9
CtrlShiftA。。。。CtrlShiftZ
Alt0。。。。Alt9
AltF1。。。。Altf12
A_A66 2010-04-30
  • 打赏
  • 举报
回复
支持一个!
spade24 2010-04-30
  • 打赏
  • 举报
回复

莫非此贴注定没人回答!
spade24 2010-04-30
  • 打赏
  • 举报
回复

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧