thiesdiggity
thiesdiggity

Reputation: 1967

Send Combination-keystrokes using sendKeyEvent in a Mozilla/Firefox Extension

I am building a Mozilla Extension and I need to send the window keystrokes using the sendKeyEvent function. I have it working when I send a single keystroke to the window but it is not working when I try to send a multi-keystroke pattern (ie. SHIFT+3 to send the # character). Does anyone have any idea how to send multiple keys to the window? I have tried the following code but it doesn't seem to be working:

var utils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindowUtils);
utils.sendKeyEvent("keydown", 51, 51, 4);
utils.sendKeyEvent("keypress", 51, 51, 4);
utils.sendKeyEvent("keyup", 51, 51, 4);

I researched the web but I cannot find any examples of multi-key combinations.

UPDATE:: OK so the reason why I asked this question is because I am building a custom FF extension that will interact with a website. On the website they have some shortcut keys, and in the extension I would like to trigger a couple short-cut key events. One such shortcut is the SHIFT+3 (# pound sign). So on the site I believe they have something like the following:

function key_event(evt)
{
     if(evt.keyCode == 16) {
         if(evt.keyCode == 51){ 
           FIRE EVENT
         }
     }
}

Now does anyone know how I can trigger this event in javascript since the sendKeyEvent doesn't seem to trigger it? My initial thought is to create a while loop which presses the SHIFT key and setup an interval to press the 3 key. At some point they would have to be triggered at the same time? Not sure if this would be the best way. Any guidance/ideas would be greatly appreciated!

Thanks for your time.

Upvotes: 1

Views: 3225

Answers (1)

Brock Adams
Brock Adams

Reputation: 93493

For the Octothorpe ("# character"), try sending its ASCII code. That is, use 35 instead of 51.

Something like:

utils.sendKeyEvent("keypress", 35, 35, 4);

Upvotes: 1

Related Questions