npm install hotkeys-js
1. Add the ‘hotkeys.js’ script to the webpage.
<script src="hotkeys.js"></script>
2. Basic usage. Supported modifiers: ⇧, shift, option, ⌥, alt, ctrl, control, command, and ⌘.
hotkeys('shift+a,alt+d, w', function(e){
console.log('Do something',e);
if(hotkeys.shift) console.log('You just clicked shift .');
if(hotkeys.ctrl) console.log('You just clicked ctrl .');
if(hotkeys.alt) console.log('You just clicked alt .');
}); 3. Advanced usages.
hotkeys('a', function(event,handler){
//event.srcElement: input
//event.target: input
if(event.target === "input"){
alert('You just pressed a!')
}
alert('You just pressed a!')
});
hotkeys('ctrl+a,ctrl+b,r,f', function(event,handler){
switch(handler.key){
case "ctrl+a":alert('You just pressed ctrl+a!');break;
case "ctrl+b":alert('You just pressed ctrl+b!');break;
case "r":alert('You just pressed r!');break;
case "f":alert(' You just pressedf!');break;
}
});
hotkeys('ctrl+r', function(){ alert('Alert!'); return false });
hotkeys('⌘+r, ctrl+r', function(){ });
hotkeys('ctrl+a+s', function(event,handler) {
if(handler.key === 'ctrl+a+s') {
alert('you pressed ctrl+a+s!');
}
}); 4. Trigger.
hotkeys.trigger('ctrl+o');
hotkeys.trigger('ctrl+o', 'scope2'); 5. Available options which can be passed as the second parameter to the hotkeys method.
hotkeys('ctrl+a+s', {
scope: 'all',
splitKey: '+',
capture: true, // pass the useCapture option to addEventListener
single: false,
element: document.getElementById('warpper'),
keyup: true, // triggers event on keyup
keydown: true // triggers event on down
}, function(event,handler) {
if(handler.key === 'ctrl+a+s') {
alert('you pressed ctrl+a+s!');
}
}); 6. More API.
// check if a modifier is pressed
hotkeys.shift
hotkeys.ctrl
hotkeys.alt
hotkeys.option
hotkeys.control
hotkeys.cmd
hotkeys.command
// check if is a key is pressed
hotkeys.isPressed('b');
// set scope
// Define shortcuts with a scope
hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function(){
console.log('do something');
});
hotkeys.setScope('issues');
// get scope
hotkeys.getScope();
// delete scope
hotkeys.deleteScope('issues');
hotkeys.deleteScope('issues', 'newScopeName');
// unbind 'a' handler
hotkeys.unbind('a');
// Unbind a hotkeys only for a single scope
// If no scope is specified it defaults to the current scope (hotkeys.getScope())
hotkeys.unbind('o, enter', 'issues');
hotkeys.unbind('o, enter', 'files');
// unbind all
hotkeys.unbind();
// returns an array of key codes currently pressed
hotkeys.getPressedKeyCodes();
// returns an array of key codes currently pressed
hotkeys.getPressedKeyString();
// return to the true shortcut keys set to play a role, false shortcut keys set up failure.
hotkeys.filter = function(event){
return true;
}
// relinquish HotKeys’s control of the hotkeys variable
hotkeys.noConflict(); v3.13.10 (05/08/2025)
v3.13.9 (12/12/2024)
v3.13.8 (12/11/2024)
v3.13.7 (02/07/2024)
v3.13.6 (02/02/2024)
v3.13.5 (01/14/2024)
v3.13.4 (01/13/2024)
v3.13.3 (12/19/2023)
v3.13.1 (12/10/2023)
v3.13.0 (12/08/2023)
v3.12.2 (11/26/2023)
v3.12.1 (11/25/2023)
v3.12.0 (08/02/2023)
v3.11.2 (07/11/2023)
v3.11.1 (07/09/2023)
v3.11.0 (07/09/2023)
v3.10.4 (07/02/2023)
v3.10.3 (06/25/2022)
v3.10.2 (04/05/2022)
v3.10.1 (11/23/2022)
v3.10.0 (09/07/2022)
v3.9.5 (08/20/2022)
v3.9.4 (05/21/2022)
v3.9.3 (05/03/2022)
v3.9.2 (05/01/2022)
v3.9.1 (04/30/2022)
v3.9.0 (04/22/2022)
v3.8.9 (04/11/2022)
v3.8.8 (04/08/2022)
v3.8.7 (06/13/2021)
v3.8.6 (06/11/2021)
v3.8.5 (05/11/2021)
v3.8.4 (05/11/2021)
v3.8.3 (03/11/2021)
v3.8.2 (01/20/2021)
v3.8.1 (05/18/2020)
v3.8.0 (05/16/2020)
v3.7.6 (03/28/2020)
v3.7.5 (03/27/2020)
v3.7.4 (03/25/2020)
v3.7.3 (11/23/2019)
v3.7.2 (09/17/2019)
v3.7.1 (08/24/2019)
v3.7.0 (08/24/2019)
v3.6.14 (08/09/2019)
v3.6.13 (07/30/2019)
v3.6.12 (07/14/2019)
v3.6.11 (05/27/2019)
v3.6.10 (05/20/2019)
v3.6.7/8 (05/09/2019)
v3.6.6 (05/08/2019)
v3.6.4 (05/04/2019)
v3.6.3 (04/28/2019)
v3.6.2 (03/30/2019)
v3.6.0 (03/27/2019)
v3.5.1 (03/25/2019)
v3.5.0 (03/21/2019)
v3.4.4 (02/12/2019)
v3.4.3 (01/16/2019)
v3.4.1 (11/22/2018)
v3.4.0 (11/21/2018)
v3.3.9 (09/07/2018)
v3.3.7 (08/28/2018)
v3.3.6 (08/25/2018)
v3.3.5 (06/08/2018)
v3.3.3 (06/03/2018)
v3.3.2 (05/29/2018)
The post Capture Keyboard Input In Pure JavaScript – hotkeys appeared first on CSS Script.
Threat actors are deploying a sophisticated multi-stage malware campaign to distribute the PureLog Stealer. Disguised…
Threat actors are deploying a sophisticated multi-stage malware campaign to distribute the PureLog Stealer. Disguised…
Google has released a substantial security update for its Chrome web browser, addressing 26 distinct…
Oracle has issued an out-of-band Security Alert addressing a critical remote code execution (RCE) vulnerability,…
NEW HAVEN, Ind. (WOWO) — The city of New Haven, Indiana has been named a…
A flyer fastened with tape to the bare carrot display at Hannaford in Concord caught…
This website uses cookies.