c#键盘事件,为什么我的程序没反应

zzyook 2012-05-01 09:27:23
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 123
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyPress(object sender,KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
label1.Text = "你按下了〈Enter〉键";
}

}
}
...全文
347 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzyook 2012-05-01
  • 打赏
  • 举报
回复
不行啊[Quote=引用 4 楼 的回复:]
你把Form的KeyPreview属性改为true试一下
[/Quote]
zzyook 2012-05-01
  • 打赏
  • 举报
回复
不行啊[Quote=引用 5 楼 的回复:]
设置你的Form的KeyPreview属性。
[/Quote]
zzyook 2012-05-01
  • 打赏
  • 举报
回复
我改成这样的也不行啊
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 123{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//textBox1.Text = "你按下了〈Enter〉键";
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
textBox1.Text = "你按下了〈Enter〉键";
else
textBox1.Text = "";
}
}
}
  • 打赏
  • 举报
回复
设置你的Form的KeyPreview属性。
ditto0723 2012-05-01
  • 打赏
  • 举报
回复
你把Form的KeyPreview属性改为true试一下
zzyook 2012-05-01
  • 打赏
  • 举报
回复
楼上的我怎么感觉在答非所问呢,帮我改改行不啊
觉悟之时 2012-05-01
  • 打赏
  • 举报
回复
原来一直是为每个文本框的KeyPress增加:
if(e.KeyChar = '\r') SendKeys.Send("{TAB}");
最近想想,其实有更简单的方法,把Form的KeyPreView设为true,然后在Form的KeyPress中增加下列代码即可:
if (e.KeyChar == '\r')
this.SelectNextControl(this.ActiveControl, true, true, true, true);
rayyu1989 2012-05-01
  • 打赏
  • 举报
回复
用全局钩子 或者 在你所有的鼠标能点击到的控件上 都要加上监听

111,126

社区成员

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

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

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