问题描述
我要做一个小车的上位机,同过键盘代替鼠标点击窗体内的button。可是刚入门不懂啊,要定义什么事件什么的。求给个示范
解决方案
解决方案二:
1.窗体KeyPreview=true2.窗体注册KeyUp或者KeyDown事件3.把你要实现的功能写里边儿
解决方案三:
据说alt+x系统会当作控件Text属性中包涵&x的快捷键
解决方案四:
还要重载ProcessDialogKey事件处理方法
解决方案五:
不过既然重载了ProcessDialogKey那么窗体KeyPreview=true、.窗体注册KeyUp或者KeyDown事件就都不需要了通过按钮名.PerformClick()就可以调用已定义好的按钮的Click处理程序
解决方案六:
引用1楼Imcx_的回复:
1.窗体KeyPreview=true2.窗体注册KeyUp或者KeyDown事件3.把你要实现的功能写里边儿
那个键盘控制我搞定了,可是我的窗体里面有combox,我设置为下拉模式,我按方向键盘的时候combox也会调,我在属性设置里面找不到能够不被键盘控制的设置啊,求
解决方案七:
引用3楼xuzuning的回复:
还要重载ProcessDialogKey事件处理方法
不是很懂。。。。
解决方案八:
publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}protectedoverrideboolProcessDialogKey(KeyskeyData){if(keyData==Keys.Up||keyData==Keys.Down||keyData==Keys.Left||keyData==Keys.Right){if(keyData==Keys.Up)button1.PerformClick();if(keyData==Keys.Left)button2.PerformClick();if(keyData==Keys.Right)button3.PerformClick();if(keyData==Keys.Down)button4.PerformClick();returnfalse;}elsereturnbase.ProcessDialogKey(keyData);}privatevoidForm1_KeyUp(objectsender,KeyEventArgse){label1.Text=e.KeyCode.ToString()+":"+e.KeyValue.ToString();if(e.KeyValue==38)button1.PerformClick();if(e.KeyValue==37)button2.PerformClick();if(e.KeyValue==39)button3.PerformClick();if(e.KeyValue==40)button4.PerformClick();}privatevoidbutton1_Click(objectsender,EventArgse){MessageBox.Show("上");}privatevoidbutton4_Click(objectsender,EventArgse){MessageBox.Show("下");}privatevoidbutton2_Click(objectsender,EventArgse){MessageBox.Show("左");}privatevoidbutton3_Click(objectsender,EventArgse){MessageBox.Show("右");}}
解决方案九:
privatevoidForm1_KeyUp(objectsender,KeyEventArgse){label1.Text=e.KeyCode.ToString()+":"+e.KeyValue.ToString();if(e.KeyValue==38)button1.PerformClick();if(e.KeyValue==37)button2.PerformClick();if(e.KeyValue==39)button3.PerformClick();if(e.KeyValue==40)button4.PerformClick();}是无效代码,请删去
解决方案十:
引用7楼xuzuning的回复:
publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}protectedoverrideboolProcessDialogKey(KeyskeyData){if(keyData==Keys.Up||keyData==Keys.Down||keyData==Keys.Left||keyData==Keys.Right){if(keyData==Keys.Up)button1.PerformClick();if(keyData==Keys.Left)button2.PerformClick();if(keyData==Keys.Right)button3.PerformClick();if(keyData==Keys.Down)button4.PerformClick();returnfalse;}elsereturnbase.ProcessDialogKey(keyData);}privatevoidForm1_KeyUp(objectsender,KeyEventArgse){label1.Text=e.KeyCode.ToString()+":"+e.KeyValue.ToString();if(e.KeyValue==38)button1.PerformClick();if(e.KeyValue==37)button2.PerformClick();if(e.KeyValue==39)button3.PerformClick();if(e.KeyValue==40)button4.PerformClick();}privatevoidbutton1_Click(objectsender,EventArgse){MessageBox.Show("上");}privatevoidbutton4_Click(objectsender,EventArgse){MessageBox.Show("下");}privatevoidbutton2_Click(objectsender,EventArgse){MessageBox.Show("左");}privatevoidbutton3_Click(objectsender,EventArgse){MessageBox.Show("右");}}
好感动