Comments (10)
Yeah unfortunately this is a limitation right now. I designed the library to be really simple for the majority of use cases so it can only bind to the document. I'm not sure if there is a simple workaround right now. Might have to try another library 😒
If you don't mind editing mousetrap you can probably do something like adding a new public method around line 796
bindEventsTo: function(element) {
_addEvent(element, 'keypress', _handleKey);
_addEvent(element, 'keydown', _handleKey);
_addEvent(element, 'keyup', _handleKey);
}
Then you could do something like
$("iframe").each(function(i, iframe) {
Mousetrap.bindEventsTo(iframe.contentDocument);
});
This will get tricky though because the callbacks would get fired for each iframe so you would have to check in the callback that it is the one you are expecting.
from mousetrap.
OK, will try this soon thanks for the quick help. I don't understand your last comment
This will get tricky though because the callbacks would get fired for each iframe so you would have to check in the callback that it is the one you are expecting."
Do mean, that inside _handleKey I have to check and switch according to the "source iframe"? Do you assume I need to distinguish the source iframe or is there another reason.
My shortcut should be global, that means I don't care which iframe sent it originally.
from mousetrap.
You don't have to change _handleKey
. What I meant is that your callback potentially could be fired multiple times, but I think I might be wrong cause only one iframe can have focus at a time so it might be fine. Let me know
from mousetrap.
Works like a charm, thanks!
And i think there is no double triggering. My callback (a test alert()) is triggered only once, then the event is scheduled only once, right?
Is there any other way for my to "append" the bindEventsTo to the mousetrap object from the "outside" so that I could use mousetrap.min.js (sorry I am a JS noob). Thx again.
from mousetrap.
Unfortunately not right now. I would have to add public access to _addEvent
and _handleKey
which I'm not crazy about doing.
If you want to minimize the new version yourself you can do it here http://closure-compiler.appspot.com/home
from mousetrap.
I was rather looking for away to making "subscribing" to your future
changes as convenient as possible. But maybe I should look into git
"forking". Thanks for all the help.
from mousetrap.
Ah yeah, you could definitely fork it and then merge in updates :)
from mousetrap.
I just wanna chime in here (even though this is closed) and say thanks for this solution. I was fighting with MouseTrap to get it to bind keys on an iframe. I added your bindEventsTo method and it works wonderfully. Here's hoping you find a way to get something similar into a release so I'm not running a rogue version :P
from mousetrap.
from mousetrap.
My fork, https://www.npmjs.org/package/combokeys, has this feature:
var combokeys = new Combokeys(document.getElementById("someIframeOrSomething"));
And then your combokeys
instance is bound to that element.
from mousetrap.
Related Issues (20)
- Reactive form setValue not correctly working after event generated by Mousetrap
- Adding mousetrap to textarea class does not work, Mousetrap.prototype.stopCallback does work HOT 1
- bind('1',()=>{console.log('1')}) HOT 1
- typescript support and npm package? HOT 1
- Typing 25 acts like if you've typed 2 HOT 3
- Command+- not captured on Firefox (but does on all other browsers tested) HOT 1
- ctrl+, not work
- List of mapped keys
- shift-ctrl-alt-4 activates ctrl-alt-4
- escape key does not bind on Safari HOT 1
- Needs ESM export
- Needs ESM export
- ctrl+tab doesn't fire HOT 1
- Use of navigator.platform invokes Chrome issue
- ev.which is readonly and hence should not be assigned during normalization.
- Shortcuts that are a combination (f+i) are saved under the second key - if there is a collision in that second key, the callbacks can misfire HOT 1
- Are there any alternatives to mousetrap? HOT 7
- Unbind or reset fails to remove hot key HOT 2
- Allow mousetrap to work in inputs
- keypress event is deprecated
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mousetrap.