【问题标题】:How do you disable all keyboard shortcuts in an access form?如何禁用访问表单中的所有键盘快捷键?
【发布时间】:2020-08-13 11:33:05
【问题描述】:

我在 excel vba 中找到了解决此问题的方法。 我想在访问 vba 中有类似的解决方案。

【问题讨论】:

    标签: vba forms ms-access shortcut disable


    【解决方案1】:

    这是我找到的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, 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}")
        
                '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
    

    【讨论】:

      【解决方案2】:

      这是我在一位同事的帮助下找到的解决方案:

      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
      

      【讨论】:

        猜你喜欢
        • 2021-11-19
        • 2019-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-05
        • 1970-01-01
        • 2012-11-06
        • 2023-04-02
        相关资源
        最近更新 更多