Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gmail seems to capture all keyboard events. Any way to go around that?

I'm writing a Chrome extension that launches a script with a keyboard shortcut. It works fine on most pages but I realized that on Gmail it doesn't: it seems that all keyboard events are captured by Gmail and are not bubbled up to my function.

I have a content script (in Chrome extension this is added to any page you want) that has (simplified of course):

document.body.addEventListener('keypress', myFunction, true);
function myFunction(event) {
    console.log("yay, Gmail didn't let me down!");
}

But actually, Gmail does let me down. I know that the script is loaded. I tried different variations of window.addEventListener and other event types to no avail.

Does anybody know of a way to bypass this? I tried to see if GreaseMonkey script could do it, that brought me here: http://code.google.com/p/gmail-greasemonkey/ but that didn't help me.

like image 688
Timothée Boucher Avatar asked Feb 24 '10 05:02

Timothée Boucher


2 Answers

I don't know the inner workings of GMail's keyboard event capturing, but I recently wrote a simple keyboard shortcut navigator (so I don't have to use the mouse to click links) for Chrome.

It's not an extension, but a user/Greasemonkey script, but it's triggered by typing comma (,) twice, and it works in GMail.

Maybe it'll help you to look at the source. You can download it here: http://userscripts.org/scripts/show/68609

like image 163
Martin R-L Avatar answered Oct 21 '22 01:10

Martin R-L


Okay I have a working solution, reverse engineered from the onePassword plugin. I can only guess as to why this works, I asume it's because of adding the event to the input elements. However Change anything and it stops working (the redir call on the bottom is on the bottom for a reason)

function redir(e) {
    e.focus();
    var h = document.createEvent("KeyboardEvent");
    h.initKeyboardEvent('keydown', true, true);
    e.dispatchEvent(h)
}
$("input").each(function(t,l) {redir(l)});

document.addEventListener('keydown', function(e) {
    if (e.ctrlKey && e.keyCode) {
      if (e.keyCode == 190) {
        chrome.extension.sendRequest({name: "spot-openPopUp"});
      }
    }
},false);

redir(document.body);

As you can see I used redirection. This example is really crude btw so don't just use it

like image 30
Thomas Avatar answered Oct 20 '22 23:10

Thomas