Here i explain how to add 'Ctrl+S' and 'Alt+S' keyboard shortcut to your webpage using JQuery and JavaScript.
Please follow the below steps
1) Catch the 'keydown' event
2) Check whether 'Ctrl' key is pressed or not
3) Check whether the 'S' key is pressed or not
4) Prevent the default behavior of 'Ctrl+S'.
//In JQuery
2) Check whether 'Alt' key is pressed or not
3) Check whether the 'S' key is pressed or not
4) Prevent the default behavior of 'Alt+S'.
//In JavaScript
Eg: Ctrl+N, Ctrl+W, Ctrl+T etc...
Note: In the demo page, after executing the below html code please click on the output pane and press 'Ctrl+S' & 'Alt+S'
Final code and demo is here
Demo
Please follow the below steps
1) Catch the 'keydown' event
2) Check whether 'Ctrl' key is pressed or not
3) Check whether the 'S' key is pressed or not
4) Prevent the default behavior of 'Ctrl+S'.
//In JQuery
<script>1) Catch the 'keydown' event
$(document).bind('keydown', function (e)
{
if (e.ctrlKey && (e.which === 83)) //Ctrl+S
{
e.preventDefault();
alert('Ctrl+S Pressed');
return false;
}
});
</script>
2) Check whether 'Alt' key is pressed or not
3) Check whether the 'S' key is pressed or not
4) Prevent the default behavior of 'Alt+S'.
//In JavaScript
<script>Some control key combinations have been reserved for browser usage only and can no longer be intercepted by the client side JavaScript in the web page.
document.onkeydown=function(e)
{
var e = e || window.event; // for IE to cover IEs window object
if(e.altKey && e.which === 83)
{
alert('Alt+S Pressed');
return false;
}
};
</script>
Eg: Ctrl+N, Ctrl+W, Ctrl+T etc...
Note: In the demo page, after executing the below html code please click on the output pane and press 'Ctrl+S' & 'Alt+S'
Final code and demo is here
Demo
No comments:
Post a Comment