I'm trying to build keyboard shortcuts for accessibility in my website. For example, when user presses '1' I want to move him to the home. When user presses '2', to a different page.
I'm trying to use keyup for this but it's not working.
$(function() {
$(document).keyup(function(e) {
if (e.which == 1) {
console.log('1');
window.location.href = '/';
} else if (e.which == 2) {
window.location.href = '/escolas';
} else if (e.which == 3) {
window.location.href = '/noticias';
} else if (e.which == 4) {
window.location.href = '/eventos';
} else if (e.which == 5) {
window.location.href = '/contato';
}
});
});
Can someone help me? It's not logging 1 when I press '1'.
It is because the key code for '1' is not 1 but 49 (or 97 for the one on the numpad).
Check this to see your key code:
$(function() { $(document).keyup(function(e) { $('body').text(e.type + ' ' + e.which); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
You are using wrong keycodes. Refer this
$(function() { $(document).keyup(function(e) { if (e.which == 49) { console.log('1'); window.location.href = '/'; } else if (e.which == 50) { window.location.href = '/escolas'; } else if (e.which == 51) { window.location.href = '/noticias'; } else if (e.which == 52) { window.location.href = '/eventos'; } else if (e.which == 53) { window.location.href = '/contato'; } }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
You can get the key character by String.fromCharCode(event.which);
, and then directly compare with actual character:
$(function() {
$(document).keyup(function(e) {
if (e.key) {
var key = e.key;
} else {
var key = String.fromCharCode(e.which || e.keyCode);
}
if (key == 1) {
console.log('1');
window.location.href = '/';
} else if (key == 2) {
window.location.href = '/escolas';
} else if (key == 3) {
window.location.href = '/noticias';
} else if (key == 4) {
window.location.href = '/eventos';
} else if (key == 5) {
window.location.href = '/contato';
}
});
});
e.which gives you ASCII value. To convert ASCII into character use String.fromCharCode(e.which) .
$(function () {
$(document).keyup(function (e) {
if (String.fromCharCode(e.which) == 1) {
window.location.href = '/';
} else if (String.fromCharCode(e.which) == 2) {
window.location.href = '/escolas';
} else if (String.fromCharCode(e.which) == 3) {
window.location.href = '/noticias';
} else if (String.fromCharCode(e.which) == 4) {
window.location.href = '/eventos';
} else if (String.fromCharCode(e.which) == 5) {
window.location.href = '/contato';
}
});
});
If you wanna go fancy:
$(document).ready(function() { 'use strict'; const addresses = [ { codes: [49, 97], path: '/' }, { codes: [50, 98], path: '/escolas' }, { codes: [51, 99], path: '/noticias' }, { codes: [52, 100], path: '/eventos' }, { codes: [52, 101], path: '/contato' } ] $(document).on("keyup", evt => { const key = evt.which; addresses.map(address => { if(address.codes.filter(code => code === key).length) window.location.href = address.path; }) }); })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.