Today we are going to learn how can we implement shortcut keys to navigate between different views with the help of shortcut keys. We are going to make custom key combination of two keys which will act as shortcut keys. Implementing it is super easy and interesting. See the code below and then read the explanation of the code to get full understanding of it.

document.onkeyup=function(e){

console.log(e)

var e = e || window.event

if (e.altKey && e.which == 72) {

$(“#myModal”).modal(‘show’);

}

}

 

Now if you read the above code it is telling you that if you press Alt+H keys together it will open a modal for you with the id ‘myModal’. e.Which, here you have to specify the the ascii code of the English character. Starting from A, it will be 65, for B it will be 66 and so on… . Let us take another example, suppose we want to the following URL by pressing Alt+I, here is how we are going to do it.

 

‘Home/Index’  //We are going to navigate to this url

<script>

 

document.onkeyup=function(e){

console.log(e)

var e = e || window.event

if (e.altKey && e.which == 73) {  //73 for i

window.location.href=”/Home/Index”;

}

}

 

</script>

 

That’s how it is. I hope it was easy and simple.