VB编程中的Keydown事件不让其反复触发的办法?
VB编程中编写如下一段程序:PrivateSubForm_KeyDown(KeyCodeAsInteger,ShiftAsInteger)SelectCaseKeyCod...
VB编程中编写如下一段程序:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Le"
txtlinkmessage.Text = "向左"
Case 38 'up
MSComm1.Output = "Go"
txtlinkmessage.Text = "向前"
Case 39 'right
MSComm1.Output = "Ri"
txtlinkmessage.Text = "向右"
Case 40 'down
MSComm1.Output = "Ba"
txtlinkmessage.Text = "向后"
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Tu"
Case 39 'right
MSComm1.Output = "Tu"
Case 38 'up
MSComm1.Output = "Di"
Case 40 'down
MSComm1.Output = "Di"
End Select
End Sub
意图就是让按住某个方向键,就会串口发送一个数据命令,方向键抬起时,串口发送一个恢复命令
但是问题来了,我按住方向键时,串口会不断发送数据,如我按下左方向键时,会持续不断发送“Le”,但我只想让其发送一次就好了,怎么简单的做到呢?
第一个回答中的“设置一个变量,在KEYDOWN事件中=TRUE,在KEYUP中=FALSE;在KEYDOWN中判断如为真就不要再发送”我已经想到并考虑过,如果是单独为每个控制命令设一个变量的话可以实现需要的功能,但控制码比较多的时候,还是会很烦琐,第二个回答中加入“Dim bSend”,只用添加一个变量,但是如果比如我先一直按着“上”键,再按“左”键,此时左键按下的事件会不响应,必须等到先把第二个按下的按键先抬起再第二次按下时才会相应~第三种回答加入“Dim Num”,方法与第二个回答类似,但还是会在执行的时候出现如第二种方法的问题,请问有更好的改进的方法吗? 展开
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Le"
txtlinkmessage.Text = "向左"
Case 38 'up
MSComm1.Output = "Go"
txtlinkmessage.Text = "向前"
Case 39 'right
MSComm1.Output = "Ri"
txtlinkmessage.Text = "向右"
Case 40 'down
MSComm1.Output = "Ba"
txtlinkmessage.Text = "向后"
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Tu"
Case 39 'right
MSComm1.Output = "Tu"
Case 38 'up
MSComm1.Output = "Di"
Case 40 'down
MSComm1.Output = "Di"
End Select
End Sub
意图就是让按住某个方向键,就会串口发送一个数据命令,方向键抬起时,串口发送一个恢复命令
但是问题来了,我按住方向键时,串口会不断发送数据,如我按下左方向键时,会持续不断发送“Le”,但我只想让其发送一次就好了,怎么简单的做到呢?
第一个回答中的“设置一个变量,在KEYDOWN事件中=TRUE,在KEYUP中=FALSE;在KEYDOWN中判断如为真就不要再发送”我已经想到并考虑过,如果是单独为每个控制命令设一个变量的话可以实现需要的功能,但控制码比较多的时候,还是会很烦琐,第二个回答中加入“Dim bSend”,只用添加一个变量,但是如果比如我先一直按着“上”键,再按“左”键,此时左键按下的事件会不响应,必须等到先把第二个按下的按键先抬起再第二次按下时才会相应~第三种回答加入“Dim Num”,方法与第二个回答类似,但还是会在执行的时候出现如第二种方法的问题,请问有更好的改进的方法吗? 展开
展开全部
可以加一个标记Num,如果Num=0,说明是第一次按下,按下后置NUm=Num+1,如果Num=0,则发送,否则不发送。修改如下:
dim Num as integer'定义一个全局变量
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
if num =0 then
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Le"
txtlinkmessage.Text = "向左"
Case 38 'up
MSComm1.Output = "Go"
txtlinkmessage.Text = "向前"
Case 39 'right
MSComm1.Output = "Ri"
txtlinkmessage.Text = "向右"
Case 40 'down
MSComm1.Output = "Ba"
txtlinkmessage.Text = "向后"
Case Else
KeyAscii = 0
End Select
end if
num=num+1
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Tu"
Case 39 'right
MSComm1.Output = "Tu"
Case 38 'up
MSComm1.Output = "Di"
Case 40 'down
MSComm1.Output = "Di"
End Select
num=0
End Sub
dim Num as integer'定义一个全局变量
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
if num =0 then
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Le"
txtlinkmessage.Text = "向左"
Case 38 'up
MSComm1.Output = "Go"
txtlinkmessage.Text = "向前"
Case 39 'right
MSComm1.Output = "Ri"
txtlinkmessage.Text = "向右"
Case 40 'down
MSComm1.Output = "Ba"
txtlinkmessage.Text = "向后"
Case Else
KeyAscii = 0
End Select
end if
num=num+1
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Tu"
Case 39 'right
MSComm1.Output = "Tu"
Case 38 'up
MSComm1.Output = "Di"
Case 40 'down
MSComm1.Output = "Di"
End Select
num=0
End Sub
展开全部
定义一个窗体变量
Dim KeyValue As Integer
Private Sub Form_Load()
KeyValue = -1
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyValue = KeyCode Then Exit Sub'添加
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Le"
txtlinkmessage.Text = "向左"
Case 38 'up
MSComm1.Output = "Go"
txtlinkmessage.Text = "向前"
Case 39 'right
MSComm1.Output = "Ri"
txtlinkmessage.Text = "向右"
Case 40 'down
MSComm1.Output = "Ba"
txtlinkmessage.Text = "向后"
Case Else
KeyAscii = 0
End Select
KeyValue = KeyCode'添加
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Tu"
Case 39 'right
MSComm1.Output = "Tu"
Case 38 'up
MSComm1.Output = "Di"
Case 40 'down
MSComm1.Output = "Di"
End Select
If KeyValue = KeyCode Then KeyValue = -1'添加
End Sub
Dim KeyValue As Integer
Private Sub Form_Load()
KeyValue = -1
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyValue = KeyCode Then Exit Sub'添加
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Le"
txtlinkmessage.Text = "向左"
Case 38 'up
MSComm1.Output = "Go"
txtlinkmessage.Text = "向前"
Case 39 'right
MSComm1.Output = "Ri"
txtlinkmessage.Text = "向右"
Case 40 'down
MSComm1.Output = "Ba"
txtlinkmessage.Text = "向后"
Case Else
KeyAscii = 0
End Select
KeyValue = KeyCode'添加
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37 'left
MSComm1.Output = "Tu"
Case 39 'right
MSComm1.Output = "Tu"
Case 38 'up
MSComm1.Output = "Di"
Case 40 'down
MSComm1.Output = "Di"
End Select
If KeyValue = KeyCode Then KeyValue = -1'添加
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建一个按键asc码的映射表,对每个键设置一个bool类型的标志表示是否按下,每次按键事件先查表,确定其按下与否,在决定是否响应keydown事件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
设置一个变量,在KEYDOWN事件中=TRUE,在KEYUP中=FALSE;在KEYDOWN中判断如为真就不要再发送了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询