I was working on a project and I need a code to stimulate keypress Ctrl+G in Javascript.
-
1You can't do that. What are you trying to accomplish? – SLaks Feb 12 '16 at 15:01
-
What result of a `Ctrl+g` are you wanting to simulate? – Jonathan M Feb 12 '16 at 15:01
-
you mean an onkeydown that pretends it was ctrl-g that was pressed? – Marc B Feb 12 '16 at 15:02
-
Ctrl+g enable/disables Google Transliteration in a webpage. I've tried other ways but none work. This is my last resort. – Stanimal Feb 12 '16 at 15:04
-
1http://stackoverflow.com/questions/4158847/is-there-a-way-to-simulate-key-presses-or-a-click-with-javascript – Justin Pavatte Feb 12 '16 at 15:07
-
Still not working.... – Stanimal Feb 12 '16 at 15:16
-
You probably need to buy something like https://en.wikipedia.org/wiki/HP_QuickTest_Professional – Ed Heal Feb 12 '16 at 15:58
3 Answers
There was a similar question recently:
Simulate Shift+Esc combination using JavaScript
Long story short, you can simulate clicks and keypresses which will work on the webpage and trigger that webpage's functions. What you're trying to do is use the browser's functions; simulated events don't travel all the way to the browser, they stay on the webpage because they originated on the webpage. If the browser doesn't have an API for this particular action, you cannot trigger it on the page.
EDIT
Looks like Google Translate has its own Javascript package, which you can utilize like any other library on your webpage. Here's a tutorial.
Try this:
<script>
//global vars
var ctrlpressed = false;
var gpressed = false;
document.onkeydown = function(event){ //Event listener: once any key is pressed, run this function
if(event.keyCode == 17){ //if CTRL is pressed, change the boolean of ctrlpressed to true.
ctrlpressed = true;
}
if(event.keyCode == 71){ //if G is pressed, change the boolean of ctrlpressed to true.
gpressed = true;
}
if (gpressed === true && ctrlpressed === true) //if both of them are pressed together, generate a message in the client's console
{
console.log("Both pressed together");
}
}
document.onkeyup = function(event){ //Event listener: once any key is released, reset the booleans above
ctrlpressed = false;
gpressed = false;
}
</script>
Worked fine for me, everytime I click Ctrl+G it console.log it.

- 7,603
- 6
- 44
- 104

- 590
- 6
- 20
-
I had a button which when clicked on, it stimulates the keypress of Control+g. Could you please explain your above code. Sorry, I am a beginner so Im having problems. Thanks :) – Stanimal Feb 12 '16 at 15:39
-
If you read OP's comments, he wants Ctrl+G doing a specific thing (enable/disable google translate). Your code will trigger Ctrl+G just fine, but it won't touch google translate api. See my answer below – sg.cc Feb 12 '16 at 15:40
-
Ctrl + g is just another method of word searching in chrome, I don't see how it's got anything to do with google translate. You can't change google chrome's shortcuts. OP, I've added more explanation, check the comments (//) – RunningFromShia Feb 12 '16 at 15:52
Press ctrl+g to check the result
This code working fine.
Run snippet code
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Testing</title>
</head>
<script>
$(document).bind('keydown', function (event) {
if (event.ctrlKey || event.metaKey) {
switch (String.fromCharCode(event.which).toLowerCase()) {
case 's':
event.preventDefault();
alert('ctrl-s');
break;
case 'f':
event.preventDefault();
alert('ctrl-f');
break;
case 'g':
event.preventDefault();
alert('ctrl-g');
break;
}
}
});
</script>
<body>
<h2>Javascript testing</h2>
</body>
</html>
and Press ctrl+s , ctrl+f and ctrl+g for testing.

- 1
- 3