Last active
March 8, 2016 00:50
-
-
Save MarshallOfSound/e9af90430832d2fbc370 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <node.h> | |
#include <nan.h> | |
#include <sstream> | |
#include <string> | |
#include <Windows.h> | |
#define _WIN32_WINNT 0x050 | |
using namespace v8; | |
Local<Function> cb; | |
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) | |
{ | |
BOOL fEatKeystroke = FALSE; | |
if (nCode == HC_ACTION) | |
{ | |
switch (wParam) | |
{ | |
case WM_KEYDOWN: | |
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam; | |
const unsigned argc = 1; | |
Isolate* isolate = Isolate::GetCurrent(); | |
std::ostringstream stream; | |
stream << p->vkCode; | |
std::string str = stream.str(); | |
Local<Value> argv[argc] = { String::NewFromUtf8(isolate, str.c_str()) }; | |
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); | |
break; | |
} | |
} | |
return CallNextHookEx(NULL, nCode, wParam, lParam); | |
} | |
void RunCallback(const FunctionCallbackInfo<Value>& args) { | |
Isolate* isolate = Isolate::GetCurrent(); | |
HandleScope scope(isolate); | |
cb = Local<Function>::Cast(args[0]); | |
HHOOK hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, 0, 0); | |
// Keep this app running until we're told to stop | |
MSG msg; | |
while (!GetMessage(&msg, NULL, NULL, NULL)) { //this while loop keeps the hook | |
TranslateMessage(&msg); | |
DispatchMessage(&msg); | |
} | |
UnhookWindowsHookEx(hhkLowLevelKybd); | |
} | |
void Init(Handle<Object> exports, Handle<Object> module) { | |
NODE_SET_METHOD(module, "exports", RunCallback); | |
} | |
NODE_MODULE(addon, Init) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment