0

I was working on a project and I need a code to stimulate keypress Ctrl+G in Javascript.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Stanimal
  • 15
  • 5

3 Answers3

1

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.

Community
  • 1
  • 1
sg.cc
  • 1,726
  • 19
  • 41
0

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.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
RunningFromShia
  • 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
0

Press ctrl+g to check the resultThis 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.