Skip to content

[Fix for #26016] In the docs js: support keyboard events for non-english keyboard layouts #26675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,46 @@
highlightSourceLines(null);
$(window).on('hashchange', highlightSourceLines);

$(document).on('keyup', function handleKeyboardShortcut(e) {
// Helper function for Keyboard events,
// Get's the char from the keypress event
//
// This method is used because e.wich === x is not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which

// compatible with non-english keyboard layouts
//
// Note: 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
}
}

$(document).on('keypress', function handleKeyboardShortcut(e) {
if (document.activeElement.tagName === 'INPUT') {
return;
}

if (e.which === 191) { // question mark
if (getChar(e) === '?') {
if (e.shiftKey && $('#help').hasClass('hidden')) {
e.preventDefault();
$('#help').removeClass('hidden');
}
} else if (e.which === 27) { // esc
} else if (getChar(e) === 's' || getChar(e) === 'S') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A deep part of my soul wants these to be cached in a variable, but then I remember this is javascript and it doesn't matter at all.

e.preventDefault();
$('.search-input').focus();
}
}).on('keydown', function(e) {
// The escape key event has to be captured with the keydown event.
// Because keypressed has no keycode for the escape key
// (and other special keys in general)...
if (document.activeElement.tagName === 'INPUT') {
return;
}

if (e.keyCode === 27) { // escape key
if (!$('#help').hasClass('hidden')) {
e.preventDefault();
$('#help').addClass('hidden');
Expand All @@ -95,16 +124,14 @@
$('#search').addClass('hidden');
$('#main').removeClass('hidden');
}
} else if (e.which === 83) { // S
e.preventDefault();
$('.search-input').focus();
}
}).on('click', function(e) {
if (!$(e.target).closest('#help').length) {
$('#help').addClass('hidden');
}
});


$('.version-selector').on('change', function() {
var i, match,
url = document.location.href,
Expand Down