ccampbell / mousetrap

Simple library for handling keyboard shortcuts in Javascript
https://craig.is/killing/mice
Apache License 2.0
11.69k stars 968 forks source link

node.js support? #244

Open jasonswearingen opened 9 years ago

jasonswearingen commented 9 years ago

any chance nodejs could be supported in the near future?

jasonswearingen commented 9 years ago

can use this for the low-level capture: http://nodejs.org/api/tty.html#tty_rs_setrawmode_mode

ccampbell commented 9 years ago

I'm not sure I understand how this would be of any use in node. It is a server side language so there is no concept of keyboard shortcuts. Could you clarify?

jasonswearingen commented 9 years ago

NW.js (node-webkit) allows using nodejs to make desktop applications,

but even without that, if you are making a server app that is interactive (example: utils)

lots of people (such as myself) use nodejs as a general-purpose application platform

ccampbell commented 9 years ago

Thanks @jasonswearingen. What exactly do I need to do to support this?

I added module.exports support here 7f453869d29871ede7c8aa6f345c361c53d317b7

I also added the ability to specify what element to have mousetrap bind to in version 1.5. I would imagine it should work in the latest version?

watdafox commented 9 years ago

Mousetrap works fine on Node Webkit (nwjs) version 9.2, 11.5 and 12 (I'm saying those because I haven't tried any other nwjs version). We use it for all keyboard interaction in Popcorn Time.

We import it through bower though, not npm.

Content of bower.json :

{
    "name": "popcorn-time",
    "dependencies": {
        "mousetrap": "~1.4.6"
    },
    "dependenciesIgnore": {
        "mousetrap": ["tests", ".gitignore", "./*!mousetrap.min.js"],
    }
}

Content of app.js:

Mousetrap.bind(['shift+f12', 'f12', 'command+0'], function (e) {
    win.showDevTools();
});
jasonswearingen commented 9 years ago

I use "bare" nodejs to write console apps and it would be very nice to use mousetrap.

But if it's difficult to figure out, I am OK with being required to take a dependency on a bigger framework like nw.js

watdafox commented 9 years ago

oh sorry I confused nodejs and nwjs x)

fatbattk commented 9 years ago

@jasonswearingen how did you manage for /plugins or did you not use any?

dafik commented 8 years ago

if you want load mousetrap from node web-kit context few changes are required:

 if(typeof document == "undefined"){
        document = window.document;
    }
    if(typeof navigator == "undefined"){
        navigator = window.navigator;
    }
}) (window, document); - > }) (window);
ccampbell commented 8 years ago

@dafik thanks for your comment here. I just pushed an update in 1.6.0 to make it “play nicer” with server side environments, but I don't think it will help anything with regards to this issue.

Is there a way to make both cases work that can be done directly in mousetrap? The isArray thing is simple enough and there is an existing PR open for that, but I'm not sure the best way to handle document. Should I assume that if window is defined and document/navigator are not, but they are defined on window that I should use the ones on window? That seems awfully fragile/hacky.