Skip to content

Commit 9d67b9f

Browse files
committed
Auto merge of #26675 - azerupi:doc-js-keyevent, r=alexcrichton
Like explained in #26016, typing `?` had no effect with non-english keyboard layouts in the docs. This patch seems to resolve this issue, **tested with AZERTY keyboard in Google Chrome and Firefox**. I haven't tested it with more exotic keyboard layouts or with other browsers though. This code is based on the information found on: http://javascript.info/tutorial/keyboard-events **More specifically:** > The only event which reliably provides the character is keypress. **And** >``` // event.type must be keypress function getChar(event) { if (event.which == null) { return String.fromCharCode(event.keyCode) // IE } else if (event.which!=0 && event.charCode!=0) { return String.fromCharCode(event.which) // the rest } else { return null // special key } } ``` `?` and `S` work, `escape` however does not (on an Azerty keyboard). It would be good if some people could test it with other browsers and keyboard layouts: http://www.mathieudavid.org/test/rustdoc/std/index.html **Edit:** - swedish layout works on Firefox and Chromium - french (azerty) mac layout works on Safari
2 parents d4fe2a0 + 49b73e4 commit 9d67b9f

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)