如何禁用访问表单中的所有键盘快捷键?

问题描述

我在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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...