├── .gitignore ├── test.js ├── binding.gyp ├── src ├── iohook.h └── iohook.cc ├── package.json ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | libuiohook 2 | build 3 | node_modules 4 | package-lock.json -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const io=require('.'); io.on('event', e => { 2 | let ev=e.event; if(process.argv[2] || (ev!='keypress' && ev!='mousemove' 3 | && ev!='mousedrag' && ev!='mousewheel')) console.log(e); 4 | }); io.start(); -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | {"targets": [{ 2 | "target_name": "iohook", 3 | "sources": ["src/iohook.cc"], 4 | "include_dirs": ["../nan", "node_modules/nan", "libuiohook/dist/include"], 5 | "conditions": [ 6 | ['OS=="win"', { #Windows 7 | "libraries": ["../libuiohook/dist/lib/uiohook"] 8 | }, { #Not Windows 9 | "libraries": ["-luiohook", "-L../libuiohook/dist/lib", "-lxkbfile", "-lxkbcommon-x11", "-lxkbcommon", "-lX11-xcb", "-lxcb", "-lXinerama", "-lXt", "-lXtst", "-lX11"] 10 | }] 11 | ] 12 | }]} -------------------------------------------------------------------------------- /src/iohook.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "uiohook.h" 5 | 6 | class HookProcessWorker: public Nan::AsyncProgressWorkerBase { 7 | public: 8 | typedef Nan::AsyncProgressWorkerBase::ExecutionProgress HookExecution; 9 | HookProcessWorker(Nan::Callback* cb); 10 | void Execute(const ExecutionProgress& progress); 11 | void HandleProgressCallback(const uiohook_event *data, size_t size); 12 | void Stop(); const HookExecution* fHook; 13 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "iohook2", 3 | "version": "1.0.6", 4 | "description": "Nodejs bindings for libuiohook", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test.js", 8 | "preinstall": "node build.js", 9 | "install": "node-gyp rebuild" 10 | }, 11 | "keywords": [ 12 | "uiohook", 13 | "iohook", 14 | "hook", 15 | "global", 16 | "event", 17 | "keyboard", 18 | "capture", 19 | "hook", 20 | "mouse" 21 | ], 22 | "author": "Pecacheu", 23 | "license": "GPL-3.0", 24 | "dependencies": { 25 | "bindings": "^1.2.1", 26 | "nan": "^2.4.0" 27 | }, 28 | "gypfile": true, 29 | "bugs": {"url": "https://github.com/Pecacheu/node-iohook/issues"}, 30 | "homepage": "https://github.com/Pecacheu/node-iohook#readme" 31 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; const IOHook=require('bindings')('iohook'); 2 | 3 | const kCodes = {65288:"backspace", 65289:"tab", 65293:"enter", 65505:"leftShift", 65506:"rightShift", 65507:"leftCtrl", 65508:"rightCtrl", 65509:"capsLock", 65513:"leftAlt", 65514:"rightAlt", 65515:"leftMeta", 65516:"rightMeta", 65300:"mediaPlay", 65301:"mediaStop", 65302:"mediaBack", 65303:"mediaNext", 65307:"esc", 65282:"brightUp", 65283:"brightDown", 65299:"pauseBreak", 65361:"left", 65362:"up", 65363:"right", 65364:"down", 65365:"pageUp", 65366:"pageDown", 65379:"insert", 65535:"delete", 65360:"home", 65367:"end", 65377:"printScr", 65407:"numLock", 65470:"f1", 65471:"f2", 65472:"f3", 65473:"f4", 65474:"f5", 65475:"f6", 65476:"f7", 65477:"f8", 65478:"f9", 65479:"f10", 65480:"f11", 65481:"f12"}; 4 | //Todo Test: Media buttons, numLock 5 | //Todo: Number Pad, ??:"scrollLock", ??:"menu" 6 | //Todo: Write documentation on the key code names 7 | 8 | class IOCallback { 9 | constructor() { this._cb={}; } 10 | set(event,cb) { this._cb[event]=cb; } 11 | get() { 12 | return d => { 13 | if(!this.active) return; if(d==null) return console.log(d); 14 | let m=d.mask,e=d.mouse||d.wheel||{},n; 15 | switch(d.type) { 16 | case 0: n="key0"; break; case 1: n="key1"; break; case 2: n="key2"; break; case 4: n="key4"; break; 17 | case 3: n="keydown"; break; 18 | case 5: n="keyup"; break; case 6: n="mouseclick"; break; 19 | case 7: n="mousedown"; break; case 8: n="mouseup"; break; 20 | case 9: n="mousemove"; break; case 10: n="mousedrag"; break; 21 | case 11: n="mousewheel"; 22 | } 23 | e.event=n; if(d.kb) { 24 | e.code=d.kb.rawcode; 25 | e.key=e.code<200?String.fromCharCode(e.code).toLowerCase():kCodes[e.code]; 26 | e.leftShift=!!(m&1), e.rightShift=!!(m&16), e.shift=((m&1)||(m&16)); 27 | e.leftCtrl=!!(m&2), e.rightCtrl=!!(m&32), e.ctrl=!!((m&2)||(m&32)); 28 | e.leftMeta=!!(m&4), e.rightMeta=!!(m&64), e.meta=!!((m&4)||(m&64)); 29 | e.leftAlt=!!(m&8), e.rightAlt=!!(m&128), e.alt=!!((m&8)||(m&128)); 30 | e.capsLock=!!(m&16384); 31 | } 32 | try { if(this._cb[n]) this._cb[n](e); if(this._cb.event) this._cb.event(e); } 33 | catch(e) {console.log(e)} 34 | } 35 | } 36 | } 37 | 38 | class Hook { 39 | constructor() { 40 | this.cb=new IOCallback(); 41 | process.on('exit', this.stop); 42 | } 43 | on(e,cb) { this.cb.set(e,cb); } 44 | start() { 45 | if(!this.status) { 46 | IOHook.start_hook(this.cb.get()); 47 | this.status=1, this.cb.active=1; 48 | } 49 | } 50 | pause() { this.cb.active=0; } 51 | resume() { this.cb.active=1; } 52 | stop() { IOHook.stop_hook(); this.status=0; } 53 | } 54 | module.exports=new Hook(); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # iohook2 2 | Nodejs bindings for libuiohook, and a replacement/renewal to the abandoned iohook library (and at least 5 other alternatives that have also been abandoned!) 3 | 4 | Install with `npm install iohook2` 5 | 6 | # About 7 | This package allows your nodejs program to read keyboard and mouse events on a global scope, even if the user isn't focused in the console window! 8 | 9 | ### You can check for the following events: 10 | - keypress *[TODO: Needs fixing. Use keydown/keyup]* 11 | - keydown 12 | - keyup 13 | - mouseclick 14 | - mousedown 15 | - mouseup 16 | - mousemove 17 | - mousedrag 18 | - mousewheel 19 | 20 | ## Example 21 | ``` 22 | const io = require('iohook2'); 23 | 24 | io.on('keydown', e => { 25 | console.log(e.key+" pressed!"); 26 | }); 27 | io.on('keyup', e => { 28 | console.log(e.key+" released!"); 29 | }); 30 | 31 | io.start(); 32 | ``` 33 | 34 | ## Build Requirements 35 | - Windows: VS2015 36 | - MAC: Clang 37 | - Linux: GCC 38 | 39 | ### Linux 40 | **The following packages are required:** git cmake pkg-config libx11-dev libxtst-dev libxt-dev libxinerama-dev libx11-xcb-dev libxkbcommon-dev libxkbcommon-x11-dev libxkbfile-dev 41 | 42 | ### Windows 43 | Please install [cmake](https://cmake.org/download). Any required DLLs should be pre-installed. 44 | 45 | *Note: Windows support is WIP. Code compiles with no errors but then has a strange linker issue.* 46 | 47 | ### Mac 48 | *Macs cost money and (for personal reasons I won't get into, not to mention the right-to-repair stuff) Apple isn't getting any more of mine. But if you have one please do help out getting it to work! Shouldn't be too different from Linux, just different dependencies.* 49 | 50 | ## Detailed Usage 51 | All key events provide a `key` property with the key name as a String, as well as a `code` property with the raw keycode. The recognized special keys are as follows: 52 | - backspace 53 | - esc 54 | - tab 55 | - enter 56 | - capsLock 57 | - leftShift / rightShift 58 | - leftCtrl / rightCtrl 59 | - leftAlt / rightAlt 60 | - leftMeta / rightMeta 61 | - mediaPlay / mediaStop / mediaBack / mediaNext 62 | - up / down / right / left 63 | - pageUp / pageDown 64 | - insert / delete 65 | - home / end 66 | - brightUp / brightDown 67 | - printScr 68 | - pauseBreak 69 | - numLock 70 | - f1 -> f12 71 | 72 | Additionally, the following boolean properties are available in any key event to check if a *modifier key* is being held: 73 | - capsLock 74 | - leftShift / rightShift / shift 75 | - leftCtrl / rightCtrl / ctrl 76 | - leftAlt / rightAlt / alt 77 | - leftMeta / rightMeta / meta 78 | 79 | ### TODO: 80 | - scrollLock (SCROLL LOCK BUTTON IS ANGY, CAN'T GET CODE) 81 | - Test mediaStop (I don't have a stop button. It's kinda redundant isn't it?) 82 | - volumeUp/Down/Mute (Yeah I don't have an excuse for this one besides lazy) 83 | - Number Pad Buttons (Could be of use to many programs, but I don't have a numberpad on hand, will have to dig one out somewhere. Or hacksaw one off the neighbor's keyboard they won't notice) 84 | - Documentation for mouse/wheel events 85 | - Document on('event'), start(), pause(), resume(), stop() -------------------------------------------------------------------------------- /src/iohook.cc: -------------------------------------------------------------------------------- 1 | #include "iohook.h" 2 | #include 3 | 4 | using namespace v8; 5 | using Callback = Nan::Callback; 6 | static bool sIsRuning=false; 7 | 8 | static HookProcessWorker* sIOHook = nullptr; 9 | 10 | static void dispatch_proc(uiohook_event * const event) { 11 | if(sIOHook != nullptr && sIOHook->fHook != nullptr) { 12 | sIOHook->fHook->Send(event, sizeof(uiohook_event)); 13 | } 14 | } 15 | 16 | static bool logger_proc(unsigned int level, const char *format, ...) { 17 | return true; 18 | } 19 | 20 | HookProcessWorker::HookProcessWorker(Nan::Callback* cb): 21 | Nan::AsyncProgressWorkerBase(cb), fHook(nullptr) {} 22 | 23 | void HookProcessWorker::Execute(const Nan::AsyncProgressWorkerBase::ExecutionProgress& progress) { 24 | hook_set_logger_proc(&logger_proc); hook_set_dispatch_proc(&dispatch_proc); 25 | fHook=&progress; hook_run(); 26 | } 27 | 28 | void HookProcessWorker::Stop() { 29 | hook_stop(); sIsRuning=false; 30 | } 31 | 32 | void HookProcessWorker::HandleProgressCallback(const uiohook_event *data, size_t size) { 33 | HandleScope scope(Isolate::GetCurrent()); 34 | Local obj = Nan::New(); 35 | 36 | Nan::Set(obj, Nan::New("type").ToLocalChecked(), Nan::New((uint16_t)data->type)); 37 | Nan::Set(obj, Nan::New("mask").ToLocalChecked(), Nan::New((uint16_t)data->mask)); 38 | if((data->type >= EVENT_MOUSE_CLICKED) && (data->type < EVENT_MOUSE_WHEEL)) { 39 | Local mouse = Nan::New(); 40 | Nan::Set(mouse, Nan::New("button").ToLocalChecked(), Nan::New((int)data->data.mouse.button)); 41 | Nan::Set(mouse, Nan::New("clicks").ToLocalChecked(), Nan::New((int)data->data.mouse.clicks)); 42 | Nan::Set(mouse, Nan::New("x").ToLocalChecked(), Nan::New((int)data->data.mouse.x)); 43 | Nan::Set(mouse, Nan::New("y").ToLocalChecked(), Nan::New((int)data->data.mouse.y)); 44 | Nan::Set(obj, Nan::New("mouse").ToLocalChecked(), mouse); 45 | Local argv[] = {obj}; callback->Call(1, argv); 46 | } else if((data->type >= EVENT_KEY_TYPED) && (data->type <= EVENT_KEY_RELEASED)) { 47 | Local kb = Nan::New(); 48 | Nan::Set(kb, Nan::New("keychar").ToLocalChecked(), Nan::New((int)data->data.keyboard.keychar)); 49 | Nan::Set(kb, Nan::New("keycode").ToLocalChecked(), Nan::New((int)data->data.keyboard.keycode)); 50 | Nan::Set(kb, Nan::New("rawcode").ToLocalChecked(), Nan::New((int)data->data.keyboard.rawcode)); 51 | Nan::Set(obj, Nan::New("kb").ToLocalChecked(), kb); 52 | Local argv[] = {obj}; callback->Call(1, argv); 53 | } else if(data->type == EVENT_MOUSE_WHEEL) { 54 | Local wheel = Nan::New(); 55 | Nan::Set(wheel, Nan::New("amount").ToLocalChecked(), Nan::New((int)data->data.wheel.amount)); 56 | Nan::Set(wheel, Nan::New("clicks").ToLocalChecked(), Nan::New((int)data->data.wheel.clicks)); 57 | Nan::Set(wheel, Nan::New("direction").ToLocalChecked(), Nan::New((int)data->data.wheel.direction)); 58 | Nan::Set(wheel, Nan::New("rotation").ToLocalChecked(), Nan::New((int)data->data.wheel.rotation)); 59 | Nan::Set(wheel, Nan::New("type").ToLocalChecked(), Nan::New((int)data->data.wheel.type)); 60 | Nan::Set(wheel, Nan::New("x").ToLocalChecked(), Nan::New((int)data->data.wheel.x)); 61 | Nan::Set(wheel, Nan::New("y").ToLocalChecked(), Nan::New((int)data->data.wheel.y)); 62 | Nan::Set(obj, Nan::New("wheel").ToLocalChecked(), wheel); 63 | Local argv[] = {obj}; callback->Call(1, argv); 64 | } 65 | } 66 | 67 | NAN_METHOD(StartHook) { //Allow one single execution 68 | if(sIsRuning==false) { 69 | if(info.Length() > 0) { 70 | if(info[0]->IsFunction()) { 71 | Callback* cb=new Callback(info[0].As()); 72 | sIOHook=new HookProcessWorker(cb); 73 | Nan::AsyncQueueWorker(sIOHook); sIsRuning=true; 74 | } 75 | } 76 | } 77 | } 78 | 79 | NAN_METHOD(StopHook) { //Allow one single execution 80 | if((sIsRuning == true) && (sIOHook !=nullptr)) sIOHook->Stop(); 81 | } 82 | 83 | NAN_MODULE_INIT(Init) { 84 | Nan::Set(target, Nan::New("start_hook").ToLocalChecked(), 85 | Nan::GetFunction(Nan::New(StartHook)).ToLocalChecked()); 86 | Nan::Set(target, Nan::New("stop_hook").ToLocalChecked(), 87 | Nan::GetFunction(Nan::New(StopHook)).ToLocalChecked()); 88 | } 89 | 90 | NODE_MODULE(nodeHook, Init) --------------------------------------------------------------------------------