I want to perform the following action.
When I open the URL http://google.com, if I press the keyboard combination (or hotkey) Z + P , then google should input in the searchbox How to make userscripts and when I press the keyboard combination (or hotkey) Z + O then google should input in the searchbox How to run userscripts
Please guide me as to how can I achieve this function as I am a complete novice Thanks and regards, Vicky.
Edit: For the following code
document.onkeyup=functione{
var e = e || window.event; // for IE to cover IEs window event-object
if(e.altKey && e.which == 65) {
alert('Keyboard shortcut working!');
return false;
}
}
For the above code, when http://google.com is loaded and I press the Alt + A button, a pop up should be there with Keyboard shortcut working!. How to put this up in Tampermonkey? Please guide. @evolutionbox
As mentioned in the comments, you can use a browser plugin like Tampermonkey for Firefox. Userscripts allow you to set rules so that they automatically run once those conditions are met. I wrote up a basic userscript that should be able to do what you requested. I tested it using tampermonkey and Firefox.
Note: It is probably more ideal to use key modifiers (shift, alt, ctrl) to create hotkeys with JavaScript, as the logic can be somewhat simplified, but hopefully this will give you something to play with.
// ==UserScript==
// @name Google HotKeys
// @version 0.1
// @include https://www.google.com/
// @run-at document-end
// ==/UserScript==
//store the first condition as false to start
var firstCondition = false;
//change the input value
function performAction(s){
document.querySelector(`[aria-label="Search"]`).value = s;
}
//listen for keydown event
document.onkeydown = function(e){
if(firstCondition){
//if first condition is true, then look for second condition
if(e.key == "o"){
//if o is pressed after z, then do this...
performAction("How to run userscripts")
}else if(e.key == "p"){
//otherwise, if p is pressed after z, then do this...
performAction("How to make userscripts")
}
//either way, reset the first condition
firstCondition = false;
}else{
//if the first condition is false, then check if "z" was pressed
firstCondition = e.key == "z";
}
};