# How I can simulate keypress/keydown in my addon?

**URL:** <https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954>\
**Category:** Development\
**Created:** [May 6, 2019, 6:35pm UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954 "2019-05-06T18:35:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zpi96920](https://avatars.discourse-cdn.com/v4/letter/z/e5b9ba/32.png) [@zpi96920](https://discourse.mozilla.org/u/zpi96920)\
**Post date:** [May 6, 2019, 6:35pm UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/1 "2019-05-06T18:35:40Z")

</div>

Hi.

How I can simulate keypress/keydown in my addon? I tried with obj.trigger(e), KeyboardEvent and everything doesn’t work fine. Why?  
I need this addon only for me, so maybe can I switch off this security way in my browser?

Thanks.

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [May 6, 2019, 7:29pm UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/2 "2019-05-06T19:29:55Z")

</div>

Maybe something like:

```auto
buttonNode.dispatchEvent(new Event('click'));

```

> **[EventTarget: dispatchEvent() method - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent)**
>
> The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected
> event listeners in the appropriate order. The normal event processing
> rules (including the capturing and optional bubbling phase) also...

---

<div class="post-metadata">

**Author:** ![zpi96920](https://avatars.discourse-cdn.com/v4/letter/z/e5b9ba/32.png) [@zpi96920](https://discourse.mozilla.org/u/zpi96920)\
**Post date:** [May 6, 2019, 8:27pm UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/3 "2019-05-06T20:27:17Z")

</div>

@juraj.masiar I tried in that way:

var pressEvent = document.createEvent(“KeyboardEvent”);  
pressEvent.initKeyEvent (“keypress”, true, true, null, false, false, false, false, 20, 20);  
obj.dispatchEvent(pressEvent);

but it doesn’t work. It is iframe and maybe here is a problem?

---

<div class="post-metadata">

**Author:** ![jscher2000](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/jscher2000/32/28202_2.png) [@jscher2000](https://discourse.mozilla.org/u/jscher2000)\
**Post date:** [May 7, 2019, 1:33am UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/4 "2019-05-07T01:33:00Z")

</div>

Do you see any error messages in any console?

There is a concept of trusted (user-generated) and untrusted (script-generated) events when simulating key presses. Extensions are similarly constrained.

> **[1348213 - API for creating synthetic (keyboard) events that trigger default...](https://bugzilla.mozilla.org/show_bug.cgi?id=1348213)**
>
> NEW (nobody) in WebExtensions - General. Last updated 2018-12-26.

---

<div class="post-metadata">

**Author:** ![zpi96920](https://avatars.discourse-cdn.com/v4/letter/z/e5b9ba/32.png) [@zpi96920](https://discourse.mozilla.org/u/zpi96920)\
**Post date:** [May 7, 2019, 5:22am UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/5 "2019-05-07T05:22:12Z")

</div>

@jscher2000 I don’t see any error in console 😕

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [May 7, 2019, 8:21am UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/6 "2019-05-07T08:21:45Z")

</div>

try to look into Browser Console - Ctrl + Shift + J. Sometimes errors in content script won’t get printed into Web Console, only into Browser Console.

---

<div class="post-metadata">

**Author:** ![zpi96920](https://avatars.discourse-cdn.com/v4/letter/z/e5b9ba/32.png) [@zpi96920](https://discourse.mozilla.org/u/zpi96920)\
**Post date:** [May 7, 2019, 2:47pm UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/7 "2019-05-07T14:47:15Z")

</div>

OK, now I see an error. Look:

> TypeError: pressEvent.initKeyboardEvent is not a function

Where is a problem??

Here is my code:

```
var pressEvent = document.createEvent("KeyboardEvent");
pressEvent.initKeyboardEvent("keypress", true, true, null, false, false, false, false, 75, 0);

```

---

<div class="post-metadata">

**Author:** ![jscher2000](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/jscher2000/32/28202_2.png) [@jscher2000](https://discourse.mozilla.org/u/jscher2000)\
**Post date:** [May 7, 2019, 4:11pm UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/8 "2019-05-07T16:11:32Z")

</div>

`initKeyboardEvent` is deprecated, but might work in some circumstances, but try the KeyboardEvent() constructor instead:

> **[KeyboardEvent: KeyboardEvent() constructor - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent)**
>
> The KeyboardEvent() constructor creates a new
> KeyboardEvent object.

Otherwise, “is not a function” or “is not an object” can occur when permissions do not permit access to a thing.

---

<div class="post-metadata">

**Author:** ![zpi96920](https://avatars.discourse-cdn.com/v4/letter/z/e5b9ba/32.png) [@zpi96920](https://discourse.mozilla.org/u/zpi96920)\
**Post date:** [May 7, 2019, 5:16pm UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/9 "2019-05-07T17:16:36Z")

</div>

OK, now my code looks that:

```
var ev = new KeyboardEvent("keypress", {bubbles : true, cancelable : true, key : "Q", char : "Q", shiftKey : true});
obj.dispatchEvent(ev);

```

But I got:

> TypeError: obj.dispatchEvent is not a function

So I think, problem is here:

> Otherwise, “is not a function” or “is not an object” can occur when permissions do not permit access to a thing.

So the question is - how I can set this permissions to use this function? I need to use this script only into my browser, so If I can change it manually into my browser - it will be ok, but how I can do it?

---

<div class="post-metadata">

**Author:** ![zpi96920](https://avatars.discourse-cdn.com/v4/letter/z/e5b9ba/32.png) [@zpi96920](https://discourse.mozilla.org/u/zpi96920)\
**Post date:** [May 7, 2019, 5:28pm UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/10 "2019-05-07T17:28:45Z")

</div>

I found into net article about conflict jQuery with this method and I need to use:

```
jQuery.noConflict();

```

But when I use this code, I can’t use ajax into my code because this code:

```
$.ajax({
...
});

```

generate an error. So I tried do it in that way:

```
   var e = jQuery.Event("keypress");
e.which = 75; // # Some key code value
console.log(obj.trigger(e));

```

And I got into console:

```
 <unavailable>

```

So I need permissions for it but how I can change this permissions and where I can do it?

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [May 7, 2019, 7:10pm UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/11 "2019-05-07T19:10:35Z")

</div>

If you see `<unavailable>` in the console, you’re looking at the wrong console. See [https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Debugging](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Debugging) to determine an appropriate console. For anything happening in a website it’d typically be the web dev tools console.

Edit: also, if you want to dispatch the event to web content, you need to make sure the web content has access to the event. See [https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing\_objects\_with\_page\_scripts#Sharing\_content\_script\_objects\_with\_page\_scripts](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#Sharing_content_script_objects_with_page_scripts)

---

<div class="post-metadata">

**Author:** ![zpi96920](https://avatars.discourse-cdn.com/v4/letter/z/e5b9ba/32.png) [@zpi96920](https://discourse.mozilla.org/u/zpi96920)\
**Post date:** [May 7, 2019, 7:29pm UTC](https://discourse.mozilla.org/t/how-i-can-simulate-keypress-keydown-in-my-addon/39954/12 "2019-05-07T19:29:50Z")

</div>

> [@freaktechnik](#):
>
> If you see `<unavailable>` in the console, you’re looking at the wrong console. See [Debugging | Firefox Extension Workshop](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Debugging) to determine an appropriate console. For anything happening in a website it’d typically be the web dev tools console.

I did it and in new console I don’t see anything intresting. In this console is only this:

> Could not map contract ID ‘@mozilla.org/fxaccounts/push;1’ to CID {1b7db999-2ecd-4abf-bb95-a726896798ca} because no implementation of the CID is registered. components.manifest:175

> Use of nsIFile in content process is deprecated. NetUtil.jsm:307:12

So finally is it possible to use keypress into firefox addon? I’m beginning to doubt 😕
