Coder Social home page Coder Social logo

Comments (10)

ccampbell avatar ccampbell commented on August 23, 2024

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.

janmechtel avatar janmechtel commented on August 23, 2024

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.

ccampbell avatar ccampbell commented on August 23, 2024

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.

janmechtel avatar janmechtel commented on August 23, 2024

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.

ccampbell avatar ccampbell commented on August 23, 2024

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.

janmechtel avatar janmechtel commented on August 23, 2024

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.

ccampbell avatar ccampbell commented on August 23, 2024

Ah yeah, you could definitely fork it and then merge in updates :)

from mousetrap.

jaylach avatar jaylach commented on August 23, 2024

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.

ts avatar ts commented on August 23, 2024

👍 @ccampbell

from mousetrap.

mightyiam avatar mightyiam commented on August 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.