设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

如何自定义三个键的组合键

1970-1-1 08:00| 发布者: 李寻欢『文章』| 查看: 1790| 评论: 0

利用KeyDown事件,我们很轻易地就可以编出让程序响应两个按键的组合键的事件,如:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
if Shift=1 and KeyCode=VbKeyM then
msgbox "您按下了Shift+M键"
end if
end sub

如果要让程序响应三个键的组合键就要麻烦一点,比如现在要用shift+b+m打开"部门"窗体。
首先要声明两个窗体级变量mSta/bSta保存M及B键的按键状态,然后在窗体的Load事件里设置KeyPreview(键预览)为True,以便窗体先于控件接收键盘事件。
在KeyDown事件里判断按键,如果按下M或B,就把mSta或bSta置为True。
在KeyUp事件里判断按键,如果放开M或B,就把mSta或bSta置为false。
做完这些事后就可以在KeyDown事件里判断是否按下了Shift+M+B键了,代玛如下:
Dim mSta As Boolean
Dim bSta As Boolean

Private Sub Form_Load()
Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyM Then
mSta = True
End If
If KeyCode = vbKeyb Then
bSta = True
End If
If Shift = 1 And mSta = True And bSta = True Then
DoCmd.OpenForm "部门"
mSta = False
bSta = False
End If
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyM Then
mSta = False
End If
If KeyCode = vbKeyB Then
bSta = False
End If
End Sub

最新评论

相关分类

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-29 03:54 , Processed in 0.079131 second(s), 16 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回顶部