方向键的响应与窗体上按钮响应冲突,急

hujiiori 2004-09-18 10:21:12
我想让我的窗体响应keydown事件,用到方向键,但发现方向键的响应与窗体上按钮响应冲突,变成按钮的焦点切换了,该怎么办,急
...全文
151 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nchen123 2004-09-22
  • 打赏
  • 举报
回复
那个函数是捕获 Tab 按键用的, 否则你会发现如果按 tab 会跳到按钮上
hujiiori 2004-09-21
  • 打赏
  • 举报
回复
首先感谢木野狐,不过我对你的例子还是不大理解,那个WindowsApplication4.KeyTrapTextBox textBox1 是干什么用的,我发现将IsInputKey那个函数去掉也可以完成那个功能的,好像textBox1一旦接受焦点(不让button接受焦点)就可以实现要求,不需要isinputKey,怎么回事
jamzh 2004-09-19
  • 打赏
  • 举报
回复
学习!~
nchen123 2004-09-19
  • 打赏
  • 举报
回复
给你做了一个例子,试试便知:

/*******************************
* 木野狐, 2004-9-19
*******************************/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication4
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label1;
private WindowsApplication4.KeyTrapTextBox textBox1;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.textBox1.Focus();

}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new WindowsApplication4.KeyTrapTextBox();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(56, 88);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "button2";
//
// button3
//
this.button3.Location = new System.Drawing.Point(56, 128);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "button3";
//
// button4
//
this.button4.Location = new System.Drawing.Point(56, 160);
this.button4.Name = "button4";
this.button4.TabIndex = 4;
this.button4.Text = "button4";
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.Desktop;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Location = new System.Drawing.Point(200, 48);
this.label1.Name = "label1";
this.label1.TabIndex = 5;
this.label1.Text = "Test";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(0, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(216, 240);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(216, 48);
this.label2.TabIndex = 6;
this.label2.Text = "按上下左右,上面那个 Label 会移动,不会变成控件焦点的切换。 (木野狐)";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 309);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}


protected override bool IsInputKey(Keys keyData)
{
switch (keyData)
{
case Keys.Tab:
case Keys.Up:
case Keys.Down:
case Keys.Left:
case Keys.Right:
return true;
}
return base.IsInputKey (keyData);
}


protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
this.label1.Top -= 10;
break;
case Keys.Down:
this.label1.Top += 10;
break;
case Keys.Left:
this.label1.Left -= 10;
break;
case Keys.Right:
this.label1.Left += 10;
break;
}

base.OnKeyDown (e);
}



}

public class KeyTrapTextBox : TextBox
{
public KeyTrapTextBox()
{

}

const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const int WM_CHAR = 0x0102;

public override bool PreProcessMessage( ref Message msg )
{
Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode;
if(msg.Msg == WM_KEYDOWN)
{
Console.WriteLine("TextBox.WM_KEYDOWN key: "+keyCode.ToString());
}

if((msg.Msg == WM_KEYDOWN)
&& keyCode == Keys.Tab)
{
return true;
}
return base.PreProcessMessage(ref msg);
}
}
}
Firestone2003 2004-09-19
  • 打赏
  • 举报
回复
学习
nchen123 2004-09-19
  • 打赏
  • 举报
回复
By default, the arrow keys are not handled by a control's key processing code, but instead are filtered out for focus management. Hence, the control's KeyDown, KeyUp and KeyPressed events are not hit when you press an arrow. If you want your control to handle these keyboard events, you tell the framework by overriding your control's IsInputKey method.

protected override bool IsInputKey(Keys key)

{

switch(key)

{

case Keys.Up:

case Keys.Down:

case Keys.Right:

case Keys.Left:

return true;

}

return base.IsInputKey(key);

}
hujiiori 2004-09-19
  • 打赏
  • 举报
回复
我想当键按着的时候连续响应,keyup不合需要吧,还有更好的办法吗
BearRui 2004-09-18
  • 打赏
  • 举报
回复
改为在KeyUp事件下响应!!!

110,566

社区成员

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

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

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