I'm trying to create a chrome extension that modifies the controls of the popular browser game, Agar.io. Quick summary, in the game SPACE performs the split action. I want to perform the same action but by pressing D instead of SPACE. The extension works fine. It detects that D has been pressed and the my split() function is called; I know this because the alert pops up but the actual game action doesn't perform. I've looked around and tried different ways but nothing is working. Below is my JS file.
window.addEventListener('keydown', keydown);
function keydown(event) {
if (event.keyCode == 68) { //key D
split();
}
}
function split() {
$("body").trigger($.Event("keydown", { keyCode: 32}));
$("body").trigger($.Event("keyup", { keyCode: 32}));
alert("Did it work?");
}
Also tried this method with no success:
var space = jQuery.Event("keydown");
space.which = 32;
$("body").trigger(space);
As shown in this question [Definitive way to trigger keypress events with jQuery
]1