简体   繁体   中英

Dispatch KeyHandler event to new DOM target with Google Closure (or plain Javascript)

I have a web-application that needs to capture any keyboard event on the page, and target them to the appropriate editable-div .

  • If the editable-div has focus, then the event flows to my event handler and to the DOM to push the character corresponding to the key into the DIV .
  • However, if the editable-div is not the current focus target, I am able to capture the event with my event handler, but the character corresponding to the key pressed is not pushed into the DIV .

My previous implementation had a dependency on jQuery, and $.trigger() was doing the right thing: moving the keyboard event from a non-matching target to the default editable-div I choose.

I am trying to achieve the same without jQuery, and with Google Closure. I tried various incantations of dispatchEvent without success in triggering the new virtual keypress.

In ClojureScript, trying to do something naive such as

(.dispatchEvent new-target (.getBrowserEvent event))

will cause the browser to complain that The event is already being dispatched.

Is there any simple solution to this problem?

You can use goog.testing.events/fireKeySequence to create events similar like jQuery's trigger .

More discussions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM