Node-webkit WinAPI

12
我正在为Windows(Vista、7等)开发简单的node-webkit应用程序,需要使用一些WinAPI函数,特别是RegisterHotKeySendInput,来绑定系统范围的热键并基于此进行按键操作。因为node-webkit没有提供这样的API,所以我打算使用node-ffi来调用这些函数。
我刚开始学习WinAPI开发,所以阅读了一些MSDN手册,但大多数示例都创建窗口、消息循环、消息处理过程等等。因此,我不太明白如何从node-webkit中正确实现对WinAPI的调用,而不创建单独的窗口? Node-ffi教程没有涵盖这种情况,因此我找到了node Windows库,但似乎它只是通过node实现Windows应用程序。
有没有一种方法可以在不创建Windows应用程序的情况下实现本地调用?正确的做法是什么?

1
你看过这里吗:https://dev59.com/Pmkw5IYBdhLWcg3w1d4m。josh3736的回答对你很有帮助。 - user568109
1
我认为修改node-webkit以添加您的API的方式更加简单和优秀。如果您愿意贡献,我可以考虑合并您的代码。 - Roger Wang
2个回答

18
我编写了一个使用 ffirefref-struct 模块在 Windows 上捕获热键的 Node 脚本。但是,由于 ffiref 是本地插件,所以在打包成 .exe 文件时遇到了一些问题。有关更多信息,请参见我之前开启的这个 GitHub 问题

无论如何,以下是代码:

var FFI = require('ffi'),
    ref = require('ref'),
    Struct = require('ref-struct');

/* First, create the necessary data structures that'll be used
   by our windows api calls. */

var pointStruct = Struct({
  'x': 'long',
  'y': 'long'
});

var msgStruct = Struct({
  'hwnd': 'int32',
  'message': 'int32', 
  'wParam': 'int32', 
  'lParam': 'int32', 
  'time': 'int32', 
  'pt': pointStruct
});

var msgStructPtr = ref.refType(msgStruct);

/* Second, register the functions we'd like to use by providing
   their method signatures. */

var user32 = new FFI.Library('user32', {

  'RegisterHotKey': [ 
    'bool', ['int32', 'int', 'int32', 'int32'] 
  ],

  'GetMessageA': [ 
    'bool', [msgStructPtr, 'int32', 'int32', 'int32'] 
  ]

  /* You may prefer to use PeekMessageA which has the same
     signature as GetMessageA, but is non-blocking. I haven't
     tested it, though.

});

/* Third, register your hotkeys. I wanted to control a media player,
   so these keys reflect that. */

var ALT = 0x0001,
    CTRL = 0x0002,
    SHIFT = 0x0004;

var MEDIA_NEXT = 0xB0,
    MEDIA_PREV = 0xB1,
    MEDIA_STOP = 0xB2,
    MEDIA_PLAY_PAUSE = 0xB3,
    MEDIA_LAUNCH = 0xB5;

var PERIOD = 0xBE,
    COMMA = 0xBC,
    EQUAL = 0xBB,
    DIVIDE = 0xBF,
    SQUOTE = 0xDE,
    PAGEUP = 0x21,
    PAGEDOWN = 0x22;

registrations = [];
registrations.push(user32.RegisterHotKey(0, 1, 0, MEDIA_NEXT));
registrations.push(user32.RegisterHotKey(0, 1, 0, MEDIA_PREV));
registrations.push(user32.RegisterHotKey(0, 1, 0, MEDIA_STOP));
registrations.push(user32.RegisterHotKey(0, 1, 0, MEDIA_PLAY_PAUSE));
registrations.push(user32.RegisterHotKey(0, 1, 0, MEDIA_LAUNCH));
registrations.push(user32.RegisterHotKey(0, 1, CTRL, PERIOD));
registrations.push(user32.RegisterHotKey(0, 1, CTRL, COMMA));
registrations.push(user32.RegisterHotKey(0, 1, CTRL, EQUAL));
registrations.push(user32.RegisterHotKey(0, 1, CTRL, DIVIDE));
registrations.push(user32.RegisterHotKey(0, 1, CTRL | ALT, PAGEUP));
registrations.push(user32.RegisterHotKey(0, 1, CTRL | ALT, PAGEDOWN));

// an array of booleans telling us which registrations failed/succeeded
console.log(registrations);

/* Fourth, wait for new hotkey events from the message queue. */

var myMsg = new msgStruct;
while (user32.GetMessageA(myMsg.ref(), 0, 0, 0)) {
    var key = myMsg.lParam >> 16;
    switch (key) {
        case MEDIA_NEXT: console.log('media next'); break;
        case MEDIA_PREV: console.log('media prev'); break;
        case MEDIA_STOP: console.log('media stop'); break;
        case MEDIA_PLAY_PAUSE: console.log('media play/pause'); break;
        case MEDIA_LAUNCH: console.log('media launch'); break;
        case PERIOD: console.log('next'); break;
        case COMMA: console.log('previous'); break;
        case EQUAL: console.log('play/pause'); break;
        case DIVIDE: console.log('info'); break;
        case PAGEUP: console.log('volume up'); break;
        case PAGEDOWN: console.log('volume down'); break;
        default: console.log('undefined hotkey', key, key.toString(16));
    }
}

如果您希望在node-webkit上使用此功能,请确保使用 nw-gyp 构建所有本机插件,并将 --target 设置为您所使用的node-webkit版本(在我的情况下为0.5.1):
# Make sure you run this command in the following directories (where the binding.gyp files are):
#  node_modules/ffi/
#  node_modules/ffi/node_modules/ref/
#  node_modules/ref/
$ nw-gyp clean configure --target=v0.5.1 build

请查阅MSDN文档以了解所使用的方法签名和结构体。希望这能帮到你!


2
一个替代node-ffi的选择是使用iohook npm模块:https://github.com/wilix-team/iohook Node.js全局键盘和鼠标监听器。此模块可以通过JavaScript/TypeScript应用程序内部和外部的本地钩子处理键盘和鼠标事件。
其他一些替代方案可以在这里找到:here。(然而,据我所知,其他的不如这个好;例如,大多数都不再维护。)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接