How to disable shortcut key in the website using jQuery

We can use jQuery to disable shortcut keys on our entire website by binding a function to the jquery "keydown" event and we can prevent the default action of the key press.

In the previous tutoria, we discuss. how to  disable right click to the whole website you must read 

In this tutorial, we will see separate examples of basic shortcut keys combination and also all keyboard key numbers which will help to prevent.

Here's an example of how you can disable the "Ctrl+S" shortcut key on your entire website:

        
        $(document).on("keydown", function(e) {
  if(e.ctrlKey && e.which == 83) {
    e.preventDefault();
  }
});
        
    

This code binds a function to the "keydown" event on the entire document, and when the event is triggered, it checks if the "Ctrl" key and the "S" key are being pressed. If they are, it calls the "preventDefault" method to prevent the default action of the "Ctrl+S" shortcut key. You can also use this method to disable other shortcut keys as well, such as "Ctrl+C" (copy) or "Ctrl+V" (paste).

We can also use this method to disable other shortcut keys as well, such as "Ctrl+C" (copy) or "Ctrl+V" (paste).

        
        $(document).on("keydown", function(e) {
  if(e.ctrlKey && e.which == 67) { //Ctrl+C
    e.preventDefault();
    alert("Shortcut key Ctrl+C is disabled on this website");
  }
  if(e.ctrlKey && e.which == 86) { //Ctrl+V
    e.preventDefault();
    alert("Shortcut key Ctrl+V is disabled on this website");
  }
});
        
    

Related Post

We can also disable other keys like "F5" (refresh) or "F12" (dev tools)

        
        $(document).on("keydown", function(e) {
  if (e.which === 116) { //F5
    e.preventDefault();
    alert("F5 key is disabled on this website");
  }
  if (e.which === 123) { //F12
    e.preventDefault();
    alert("F12 key is disabled on this website");
  }
});
        
    

Here is the list of all keyboard Key Code Values

Key

Key value

Key

Key value

Alt

18

F5

116

Arrow Down

40

F6

117

Arrow Left

37

F7

118

Arrow Right

39

F8

119

Arrow Up

38

F9

120

Backspace

8

F10

121

Caps Lock

20

F1

122

Ctrl

17

F12

123

Delete

46

Home

36

End

35

Insert

45

Enter

13

Num Lock

144

Esc

27

(NumPad) -

109

F1

112

(NumPad) *

106

F2

113

(NumPad) .

110

F3

114

(NumPad) /

111

F4

115

(NumPad) +

107

(NumPad) 0

96

P

80

NumPad) 1

97

Q

81

(NumPad) 2

98

R

82

NumPad) 3

99

S

83

(NumPad) 4

100

T

84

(NumPad) 5

101

U

85

(NumPad) 6

102

V

86

(NumPad) 7

103

W

87

(NumPad) 8

104

X

88

(NumPad) 9

105

Y

89

Page Down

34

Z

90

Page Up

33

1

49

Pause

19

2

50

Print Scrn

44

3

51

Scroll Lock

145

4

52

Shift

16

5

53

Spacebar

32

6

54

Tab

9

7

55

A

65

8

56

B

66

9

57

C

67

0

48

D

68

'

222

E

69

-

189

F

70

,

188

G

71

.

190

H

72

/

191

I

73

;

186

J

74

[

219

K

75

\

220

L

76

]

221

M

77

'

192

N

78

=

187

O

79

   

Leave a comment