Skip to content

Commit d31a5af

Browse files
committed
intuitive implementation of stop_hook
1 parent 545bb5f commit d31a5af

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class IOCallback{
1919
return function(msg){
2020

2121
if(that.active==false) return;
22-
22+
if(msg==undefined) {console.log(msg); return;}
2323
if(msg.type==3)
2424
{
2525
if(that._cbObject["keypress"])
@@ -131,6 +131,10 @@ class IOHook {
131131
this.callback.active = true;
132132
}
133133

134+
stop(){
135+
NodeHookAddon.stop_hook();
136+
}
137+
134138
}
135139

136140
module.exports = IOHook;

main.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ function createWindow () {
2929
mainWindow = new BrowserWindow({width: 800, height: 600});
3030

3131
// and load the index.html of the app.
32-
mainWindow.loadURL('file://' + __dirname + '/index.html');
33-
34-
32+
mainWindow.loadURL("http://www.google.com");
3533

3634
//install event listener
37-
ioHook.on("keypress",function(msg){console.log(msg)})
35+
ioHook.on("keyup",function(msg){console.log(msg); if(msg.keyboard.keycode==57){
36+
ioHook.stop()
37+
}
38+
})
3839
ioHook.on("mousemove",function(msg){console.log(msg)})
3940

40-
41-
42-
4341
mainWindow.on("focus",function()
4442
{
4543
ioHook.resume();

src/node-iohook.cc

+17-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ void HookProcessWorker::Execute(const Nan::AsyncProgressWorkerBase<uiohook_event
3535
hook_set_logger_proc(&logger_proc);
3636
hook_set_dispatch_proc(&dispatch_proc);
3737
fHookExecution = &progress;
38-
std::cout << hook_run();
39-
38+
hook_run();
4039
}
4140

41+
void HookProcessWorker::Stop()
42+
{
43+
hook_stop();
44+
sIsRuning = false;
45+
}
4246
void HookProcessWorker::HandleProgressCallback(const uiohook_event *data, size_t size)
4347
{
4448
HandleScope scope(Isolate::GetCurrent());
@@ -106,13 +110,24 @@ NAN_METHOD(StartHook) {
106110
}
107111
}
108112

113+
NAN_METHOD(StopHook) {
114+
115+
//allow one single execution
116+
if ((sIsRuning == true) && (sIOHook !=nullptr))
117+
{
118+
sIOHook->Stop();
119+
}
120+
}
109121

110122
NAN_MODULE_INIT(Init) {
111123

112124

113125

114126
Nan::Set(target, Nan::New<String>("start_hook").ToLocalChecked(),
115127
Nan::GetFunction(Nan::New<FunctionTemplate>(StartHook)).ToLocalChecked());
128+
129+
Nan::Set(target, Nan::New<String>("stop_hook").ToLocalChecked(),
130+
Nan::GetFunction(Nan::New<FunctionTemplate>(StopHook)).ToLocalChecked());
116131
}
117132

118133
NODE_MODULE(nodeHook, Init)

src/node-iohook.h

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class HookProcessWorker : public Nan::AsyncProgressWorkerBase<uiohook_event>
1818

1919
void HandleProgressCallback(const uiohook_event *data, size_t size);
2020

21+
void Stop();
22+
2123
const HookExecution* fHookExecution;
2224

2325
};

0 commit comments

Comments
 (0)