Skip to content

Commit 49b73e4

Browse files
committed
In js from the docs, change keyboard eventlistener to be compatible with non-english keyboard layouts. Fixes #26016 Fixes #16572
1 parent 27975c4 commit 49b73e4

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/librustdoc/html/static/main.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,46 @@
7676
highlightSourceLines(null);
7777
$(window).on('hashchange', highlightSourceLines);
7878

79-
$(document).on('keyup', function handleKeyboardShortcut(e) {
79+
// Helper function for Keyboard events,
80+
// Get's the char from the keypress event
81+
//
82+
// This method is used because e.wich === x is not
83+
// compatible with non-english keyboard layouts
84+
//
85+
// Note: event.type must be keypress !
86+
function getChar(event) {
87+
if (event.which == null) {
88+
return String.fromCharCode(event.keyCode) // IE
89+
} else if (event.which!=0 && event.charCode!=0) {
90+
return String.fromCharCode(event.which) // the rest
91+
} else {
92+
return null // special key
93+
}
94+
}
95+
96+
$(document).on('keypress', function handleKeyboardShortcut(e) {
8097
if (document.activeElement.tagName === 'INPUT') {
8198
return;
8299
}
83100

84-
if (e.which === 191) { // question mark
101+
if (getChar(e) === '?') {
85102
if (e.shiftKey && $('#help').hasClass('hidden')) {
86103
e.preventDefault();
87104
$('#help').removeClass('hidden');
88105
}
89-
} else if (e.which === 27) { // esc
106+
} else if (getChar(e) === 's' || getChar(e) === 'S') {
107+
e.preventDefault();
108+
$('.search-input').focus();
109+
}
110+
}).on('keydown', function(e) {
111+
// The escape key event has to be captured with the keydown event.
112+
// Because keypressed has no keycode for the escape key
113+
// (and other special keys in general)...
114+
if (document.activeElement.tagName === 'INPUT') {
115+
return;
116+
}
117+
118+
if (e.keyCode === 27) { // escape key
90119
if (!$('#help').hasClass('hidden')) {
91120
e.preventDefault();
92121
$('#help').addClass('hidden');
@@ -95,16 +124,14 @@
95124
$('#search').addClass('hidden');
96125
$('#main').removeClass('hidden');
97126
}
98-
} else if (e.which === 83) { // S
99-
e.preventDefault();
100-
$('.search-input').focus();
101127
}
102128
}).on('click', function(e) {
103129
if (!$(e.target).closest('#help').length) {
104130
$('#help').addClass('hidden');
105131
}
106132
});
107133

134+
108135
$('.version-selector').on('change', function() {
109136
var i, match,
110137
url = document.location.href,

0 commit comments

Comments
 (0)