如何解决如何禁用访问表单中的所有键盘快捷键?
我在excel vba中找到了解决此问题的方法。 我想在Access vba中有一个类似的解决方案。
解决方法
这是我发现的excel vba代码。
Sub Disable_Keys()
Dim StartKeyCombination As Variant
Dim KeysArray As Variant
Dim Key As Variant
Dim I As Long
On Error Resume Next
'Shift key = "+" (plus sign)
'Ctrl key = "^" (caret)
'Alt key = "%" (percent sign
'We fill the array with this keys and the key combinations
'Shift-Ctrl,Shift- Alt,Ctrl-Alt,Shift-Ctrl-Alt
For Each StartKeyCombination In Array("+","^","%","+^","+%","^%","+^%")
KeysArray = Array("{BS}","{BREAK}","{CAPSLOCK}","{CLEAR}","{DEL}",_
"{DOWN}","{END}","{ENTER}","~","{ESC}","{HELP}","{HOME}",_
"{INSERT}","{LEFT}","{NUMLOCK}","{PGDN}","{PGUP}",_
"{RETURN}","{RIGHT}","{SCROLLLOCK}","{TAB}","{UP}")
'Disable the StartKeyCombination key(s) with every key in the KeysArray
For Each Key In KeysArray
Application.OnKey StartKeyCombination & Key,""
Next Key
'Disable the StartKeyCombination key(s) with every other key
For I = 0 To 255
Application.OnKey StartKeyCombination & Chr$(I),""
Next I
'Disable the F1 - F15 keys in combination with the Shift,Ctrl or Alt key
For I = 1 To 15
Application.OnKey StartKeyCombination & "{F" & I & "}",""
Next I
Next StartKeyCombination
'Disable the F1 - F15 keys
For I = 1 To 15
Application.OnKey "{F" & I & "}",""
Next I
'Disable the PGDN and PGUP keys
Application.OnKey "{PGDN}",""
Application.OnKey "{PGUP}",""
End Sub
Sub Enable_Keys()
Dim StartKeyCombination As Variant
Dim KeysArray As Variant
Dim Key As Variant
Dim I As Long
On Error Resume Next
'Shift key = "+" (plus sign)
'Ctrl key = "^" (caret)
'Alt key = "%" (percent sign
'We fill the array with this keys and the key combinations
'Shift-Ctrl,"{UP}")
'Enable the StartKeyCombination key(s) with every key in the KeysArray
For Each Key In KeysArray
Application.OnKey StartKeyCombination & Key
Next Key
'Enable the StartKeyCombination key(s) with every other key
For I = 0 To 255
Application.OnKey StartKeyCombination & Chr$(I)
Next I
'Enable the F1 - F15 keys in combination with the Shift,Ctrl or Alt key
For I = 1 To 15
Application.OnKey StartKeyCombination & "{F" & I & "}"
Next I
Next StartKeyCombination
'Enable the F1 - F15 keys
For I = 1 To 15
Application.OnKey "{F" & I & "}"
Next I
'Enable the PGDN and PGUP keys
Application.OnKey "{PGDN}"
Application.OnKey "{PGUP}"
End Sub
,
这是我在我同事的帮助下找到的解决方案:
Private Sub Form_Open(Cancel As Integer)
Me.KeyPreview = True 'turn keypreview on in order to receive all keyboard events
End Sub
Private Sub Form_KeyDown(keycode As Integer,shift As Integer)
sb_disablekeys keycode,shift
End Sub
Public Sub sb_disablekeys(keycode As Integer,shift As Integer)
'All keyboard events with CTRL don’t function anymore with the exception of CTRL+C and CTRL+V
‘All keyboard events with ALT don’t function anymorge
‘All function keys are disabled
Select Case shift
Case acCtrlMask ‘CTRL pressed
Select Case keycode
Case 0 To 16,18 To 66,68 To 85,87 To 255
'All keycodes with the exception of 17 (CTRL),67 (CTRL+C) and 86 (CTRL+V) are set to 0.
keycode = 0
End Select
Case acAltMask 'Alt pressed
keycode = 0
End Select
Select Case keycode
Case vbKeyF1 To vbKeyF16 ‘Function key pressed
keycode = 0
End Select
End Sub
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。