My respectful greetings to all of you. I want to disable all except a few shortcut keys in the form, for this I have seen the codes on Google and they are working properly but I do not have complete knowledge about such codes.
I want to enable alt + F4 in the form, for this I have modified the code. Please tell me whether I have made the correct modification. If the modification is correct then why I have to write 'End Select' three times in it.
Thank you
Code:
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 'FOR STOP PGUP & PAGE DOWN
Case 33 To 34
keycode = 0
End Select
Select Case keycode
Case vbKeyF1 To vbKeyF16 ' ‘Function key pressed
keycode = 0
End Select
End Sub
Private Sub Form_KeyDown(keycode As Integer, Shift As Integer)
sb_disablekeys keycode, Shift
End Sub
Private Sub Form_Open(CANCEL As Integer)
Me.KeyPreview = True
End Sub
Revise Code
Code:
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
Select Case Shift
Case acAltMask 'Alt pressed
Select Case keycode
Case 0 To 17, 19 To 255
'Case 0 To 8, 10 To 17, 19 To 255
keycode = 0
End Select '1 time
End Select '2 time
End Select '3 time
Select Case keycode 'FOR STOP PGUP & PAGE DOWN
Case 33 To 34
keycode = 0
End Select
Select Case keycode
'Case vbKeyF1 To vbKeyF16 ' ‘Function key pressed
Case vbKeyF1, vbKeyF3, vbKeyF5 To vbKeyF16
keycode = 0
End Select
End Sub