tomzx / gkm

⚠ Unsupported/Unmaintained ⚠️️ Global Keyboard and Mouse listener for node.js.
MIT License
52 stars 16 forks source link

not working with Electron-packager #19

Closed aizaku closed 5 years ago

aizaku commented 5 years ago

Hi there. Was testing stuff out while making an app and noticed that events are not fired after the application was packaged. Tested this with simple

gkm.events.on('key.*', function(data) {
  console.log(data[0]);
});

and while unpackaged it works, after electron-packager it stops triggering all possible events.

tomzx commented 5 years ago

Hello,

This is unlikely to be related to gkm itself. I'm going to guess that when your electron application is packaged, the node modules are packaged within an asar file (https://electronjs.org/docs/tutorial/application-packaging) and as such, when

https://github.com/tomzx/gkm/blob/b2d7bc986b4dbc8e5d5a6027760e10e21512df32/gkm.js#L10

is called from gkm.js, java cannot find the jar file since it's not in the user file system but within that asar archive. I believe it should be possible for you to unpack the asar when installing the packaged application, which should solve this issue.

aizaku commented 5 years ago

Hi, Thank you for answering. Would you be so kind and guide me through that process? Should I unpack the whole thing or just the gkm.js? Should I somehow point to it after unpacking? etc.

And are there any downsides to doing so?

tomzx commented 5 years ago

It's been quite a while since I packaged any electron application, but back then I relied on https://github.com/electron-userland/electron-packager. Given their documentation (https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#asar), if you aren't packaging the source code into an asar archive, you should have nothing to do.

I'd suggest you first package your application, then install it, then go to the directory where it was installed and check whether you can find a node_modules directory inside. If you can, then check if you can find gkm directory within it. If you can, then things should work the same as if you were in your development environment. Also make sure that you can find the gkm.jar and JNativeHook.jar files.

On the other hand, if you can't find the node_modules nor the gkm directories, then it's likely that your source code is archived in an asar file. If you're using electron-packager, you can either disable using asar or only unpack a certain part of it (I never did myself). See the documentation I pointed at to know how to do that.

I'm not knowledgeable enough to know what are the downsides to unpacking, other than it exposes your source code on the file system.

aizaku commented 5 years ago

Yes. That helped a lot. I was able to unpack it successfully using asar.unpackDir and unpacking node_modules. Unpacking just the gkm resulted in error since it wasn't able to locate eventemitter2 module. I did not try to unpack just those two, will maybe experiment a bit more, but quite happy with the result already. Thank you.

tomzx commented 5 years ago

Happy to see it worked out for you!