Skip to content

Commit bb22171

Browse files
committed
Merged branch dev into master
2 parents f3a299e + dda0662 commit bb22171

File tree

6 files changed

+260
-62
lines changed

6 files changed

+260
-62
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "lib/index.js",
66
"scripts": {
77
"start": "gulp && node ./lib/os-input-capture-cli.js",
8-
"test": "gulp && jasmine",
8+
"test": "gulp && echo 'oic' && ./node_modules/jasmine-es6/bin/jasmine.js ./spec/os-input-capture_spec.js && echo 'keyboard' && ./node_modules/jasmine-es6/bin/jasmine.js ./spec/keyboard-logger_spec.js && echo 'mouse' && ./node_modules/jasmine-es6/bin/jasmine.js ./spec/mouse-logger_spec.js && echo 'window' && ./node_modules/jasmine-es6/bin/jasmine.js ./spec/window-logger_spec.js",
99
"dev": "gulp dev && node ./lib/examples/basic-example.js"
1010
},
1111
"repository": {

spec/os-input-capture_spec.js

Lines changed: 137 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,147 @@
11
'use strict';
22

3-
import OsInputCapture from '../lib/os-input-capture.js';
3+
import oic from '../lib/os-input-capture.js';
4+
import _ from 'lodash';
45

56
let globals = {};
67

7-
describe('OsInputCapture', () => {
8+
describe('oic', () => {
89
beforeEach(() => {
9-
globals.osInputCapture = new OsInputCapture();
10+
_.assign(globals, { oic });
1011
});
11-
it('should be defined', () => {
12-
expect(globals.osInputCapture).toBeDefined();
12+
it('should expose the core classes', () => {
13+
expect(globals.oic.OsInputCapture).toBeDefined();
14+
expect(globals.oic.KeyboardLogger).toBeDefined();
15+
expect(globals.oic.MouseLogger).toBeDefined();
16+
expect(globals.oic.WindowLogger).toBeDefined();
1317
});
14-
it('should create instances of the logger classes', () => {
15-
expect(globals.osInputCapture.keyboardLogger).toBeDefined();
16-
expect(globals.osInputCapture.mouseLogger).toBeDefined();
17-
expect(globals.osInputCapture.windowLogger).toBeDefined();
18+
describe('OsInputCapture', () => {
19+
it('should not add any loggers by default', () => {
20+
let inputLogger = new globals.oic.OsInputCapture();
21+
expect(inputLogger.keyboardLogger).not.toBeDefined();
22+
expect(inputLogger.mouseLogger).not.toBeDefined();
23+
expect(inputLogger.windowLogger).not.toBeDefined();
24+
});
25+
it('should add keyboard logger if specified', () => {
26+
let inputLogger = new globals.oic.OsInputCapture(['keyboard']);
27+
expect(inputLogger.keyboardLogger).toBeDefined();
28+
});
29+
it('should add mouse logger if specified', () => {
30+
let inputLogger = new globals.oic.OsInputCapture(['mouse']);
31+
expect(inputLogger.mouseLogger).toBeDefined();
32+
});
33+
it('should add window logger if specified', () => {
34+
let inputLogger = new globals.oic.OsInputCapture(['window']);
35+
expect(inputLogger.windowLogger).toBeDefined();
36+
});
37+
describe('addKeyboardLogger', () => {
38+
it('should add keyboard logger to OsInputCapture', () => {
39+
let inputLogger = new globals.oic.OsInputCapture();
40+
inputLogger.addKeyboardLogger({});
41+
expect(inputLogger.keyboardLogger).toBeDefined();
42+
});
43+
it('should use supplied opts if present', () => {
44+
let fakeOpts = {
45+
keyboardOptions: {
46+
inputPath: 'fakeInputPath',
47+
outputDir: 'fakeOutputPath'
48+
}
49+
};
50+
let inputLogger = new globals.oic.OsInputCapture();
51+
inputLogger.addKeyboardLogger(fakeOpts);
52+
expect(inputLogger.keyboardLogger.opts).toEqual(fakeOpts.keyboardOptions);
53+
});
54+
});
55+
describe('addMouseLogger', () => {
56+
it('should add mouse logger to OsInputCapture', () => {
57+
let inputLogger = new globals.oic.OsInputCapture();
58+
inputLogger.addMouseLogger({});
59+
expect(inputLogger.mouseLogger).toBeDefined();
60+
});
61+
it('should use supplied opts if present', () => {
62+
let fakeOpts = {
63+
mouseOptions: {
64+
inputPath: 'fakeInputPath',
65+
outputDir: 'fakeOutputPath'
66+
}
67+
};
68+
let inputLogger = new globals.oic.OsInputCapture();
69+
inputLogger.addMouseLogger(fakeOpts);
70+
expect(inputLogger.mouseLogger.opts).toEqual(fakeOpts.mouseOptions);
71+
});
72+
});
73+
describe('addWindowLogger', () => {
74+
it('should add window logger to OsInputCapture', () => {
75+
let inputLogger = new globals.oic.OsInputCapture();
76+
inputLogger.addWindowLogger({});
77+
expect(inputLogger.windowLogger).toBeDefined();
78+
});
79+
it('should use supplied opts if present', () => {
80+
let fakeOpts = {
81+
windowOptions: {
82+
outputDir: 'fakeOutputPath'
83+
}
84+
};
85+
let inputLogger = new globals.oic.OsInputCapture();
86+
inputLogger.addWindowLogger(fakeOpts);
87+
expect(inputLogger.windowLogger.opts).toEqual(fakeOpts.windowOptions);
88+
});
89+
});
90+
describe('toggleActive', () => {
91+
it('should toggle its own active state', () => {
92+
let inputLogger = new globals.oic.OsInputCapture();
93+
let originalState = inputLogger.active;
94+
inputLogger.toggleActive();
95+
expect(inputLogger.active).toEqual(!originalState);
96+
});
97+
it('should toggle its keyboardLogger active state if its defined', () => {
98+
let fakeOpts = {
99+
keyboardOptions: {
100+
inputPath: 'fakeInputPath',
101+
outputDir: 'fakeOutputPath'
102+
}
103+
};
104+
let inputLogger = new globals.oic.OsInputCapture(['keyboard'], fakeOpts);
105+
let originalState = inputLogger.keyboardLogger.active;
106+
inputLogger.toggleActive();
107+
expect(inputLogger.keyboardLogger.active).toEqual(!originalState);
108+
});
109+
it('should toggle its mouseLogger active state if its defined', () => {
110+
let fakeOpts = {
111+
mouseOptions: {
112+
inputPath: 'fakeInputPath',
113+
outputDir: 'fakeOutputPath'
114+
}
115+
};
116+
let inputLogger = new globals.oic.OsInputCapture(['mouse'], fakeOpts);
117+
let originalState = inputLogger.mouseLogger.active;
118+
inputLogger.toggleActive();
119+
expect(inputLogger.mouseLogger.active).toEqual(!originalState);
120+
});
121+
it('should toggle its keyboardLogger active state if its defined', () => {
122+
let fakeOpts = {
123+
windowOptions: {
124+
outputDir: 'fakeOutputPath'
125+
}
126+
};
127+
let inputLogger = new globals.oic.OsInputCapture(['window'], fakeOpts);
128+
let originalState = inputLogger.windowLogger.active;
129+
inputLogger.toggleActive();
130+
expect(inputLogger.windowLogger.active).toEqual(!originalState);
131+
});
132+
});
133+
describe('getWindow', () => {
134+
it('should call windowLogger.get if this.windowLogger is defined', () => {
135+
let fakeOpts = {
136+
windowOptions: {
137+
outputDir: 'fakeOutputPath'
138+
}
139+
};
140+
let inputLogger = new globals.oic.OsInputCapture(['window'], fakeOpts);
141+
spyOn(inputLogger.windowLogger, 'get');
142+
inputLogger.getWindow();
143+
expect(inputLogger.windowLogger.get).toHaveBeenCalled();
144+
});
145+
});
18146
});
19147
});

src/keyboard-logger.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,21 @@ class KeyboardLogger {
3131
}
3232
handleKeyboardEvent(buffer) {
3333
let eventData = { timeStamp: Date.now(), code: buffer.readUInt16LE(20), value: buffer.readInt32LE(44) };
34-
if (eventData.code === 38 && eventData.value === 1 && !this.active) {
35-
this.parent.toggleActive();
36-
}
37-
if (eventData.code === 37 && eventData.value === 1 && this.active) {
38-
this.parent.toggleActive();
34+
if (((eventData.code === 38 && !this.active) || (eventData.code === 37 && this.active)) && eventData.value === 1 ) {
35+
if (!_.isUndefined(this.parent)) {
36+
this.parent.toggleActive();
37+
}
38+
if (_.isUndefined(this.parent)) {
39+
this.toggleActive();
40+
}
3941
}
4042
if (this.active) {
4143
this.writeStream.log('info', eventData);
42-
this.parent.getWindow();
44+
if (!_.isUndefined(this.parent)) {
45+
if (!_.isUndefined(this.parent.getWindow)) {
46+
this.parent.getWindow();
47+
}
48+
}
4349
}
4450
}
4551
}

src/mouse-logger.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ class MouseLogger {
6868
rightBtn: rightBtn
6969
};
7070
this.writeStream.log('info', eventData);
71-
this.parent.getWindow();
71+
if (!_.isUndefined(this.parent)) {
72+
if (!_.isUndefined(this.parent.getWindow)) {
73+
this.parent.getWindow();
74+
}
75+
}
7276
}
7377
}
7478
}

src/os-input-capture-cli.js

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
'use strict';
22

3-
import inquirer from 'inquirer';
4-
import OsInputCapture from './os-input-capture.js';
3+
// import inquirer from 'inquirer';
4+
import oic from './os-input-capture.js';
55
import del from 'del';
66

7-
inquirer.prompt([
8-
{
9-
type: 'rawlist',
10-
name: 'action',
11-
message: 'os-input-capture cli prompt: ',
12-
choices: [
13-
'test keyboardLogger',
14-
'test mouseLogger',
15-
'test windowLogger',
16-
'clean'
17-
]
18-
}
19-
]).then(answers => {
20-
if (answers.action === 'test keyboardLogger') {
21-
let keyboardLogger = new OsInputCapture().keyboardLogger;
22-
}
23-
if (answers.action === 'test mouseLogger') {
24-
let mouseLogger = new OsInputCapture().mouseLogger;
25-
}
26-
if (answers.action === 'test windowLogger') {
27-
let windowLogger = new OsInputCapture().windowLogger;
28-
windowLogger.get();
29-
}
30-
if (answers.action === 'clean') {
31-
del([
32-
'./lib/*'
33-
]);
34-
}
35-
});
7+
// let kbd = oic.KeyboardLogger();
8+
let mouse = oic.MouseLogger();
9+
mouse.active = true;
10+
11+
// inquirer.prompt([
12+
// {
13+
// type: 'rawlist',
14+
// name: 'action',
15+
// message: 'os-input-capture cli prompt: ',
16+
// choices: [
17+
// 'test keyboardLogger',
18+
// 'test mouseLogger',
19+
// 'test windowLogger',
20+
// 'clean'
21+
// ]
22+
// }
23+
// ]).then(answers => {
24+
// if (answers.action === 'test keyboardLogger') {
25+
// let keyboardLogger = new OsInputCapture().keyboardLogger;
26+
// }
27+
// if (answers.action === 'test mouseLogger') {
28+
// let mouseLogger = new OsInputCapture().mouseLogger;
29+
// }
30+
// if (answers.action === 'test windowLogger') {
31+
// let windowLogger = new OsInputCapture().windowLogger;
32+
// windowLogger.get();
33+
// }
34+
// if (answers.action === 'clean') {
35+
// del([
36+
// './lib/*'
37+
// ]);
38+
// }
39+
// });

src/os-input-capture.js

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,83 @@ import WindowLogger from './window-logger.js';
55
import _ from 'lodash';
66

77
class OsInputCapture {
8-
constructor(opts) {
8+
constructor(loggers = [], opts = {}) {
99
this.opts = opts;
1010
this.active = false;
11-
let { keyboardOptions, mouseOptions, windowOptions } = opts;
12-
this.keyboardLogger = new KeyboardLogger(keyboardOptions, this);
13-
this.mouseLogger = new MouseLogger(mouseOptions, this);
14-
this.windowLogger = new WindowLogger(windowOptions);
11+
if (_.findIndex(loggers, (item) => item === 'keyboard') !== -1) {
12+
this.addKeyboardLogger(opts);
13+
}
14+
if (_.findIndex(loggers, (item) => item === 'mouse') !== -1) {
15+
this.addMouseLogger(opts);
16+
}
17+
if (_.findIndex(loggers, (item) => item === 'window') !== -1) {
18+
this.addWindowLogger(opts);
19+
}
20+
}
21+
addKeyboardLogger(opts) {
22+
if (_.isUndefined(this.keyboardLogger)) {
23+
if (!_.isUndefined(opts)) {
24+
let { keyboardOptions } = opts;
25+
this.keyboardLogger = new KeyboardLogger(keyboardOptions, this);
26+
}
27+
if (_.isUndefined(opts)) {
28+
let { keyboardOptions } = this.opts;
29+
this.keyboardLogger = new KeyboardLogger(keyboardOptions, this);
30+
}
31+
} else {
32+
throw new Error('logger already present');
33+
}
34+
}
35+
addMouseLogger(opts) {
36+
if (_.isUndefined(this.mouseLogger)) {
37+
if (!_.isUndefined(opts)) {
38+
let { mouseOptions } = opts;
39+
this.mouseLogger = new MouseLogger(mouseOptions, this);
40+
}
41+
if (_.isUndefined(opts)) {
42+
let { mouseOptions } = this.opts;
43+
this.mouseLogger = new MouseLogger(mouseOptions, this);
44+
}
45+
} else {
46+
throw new Error('logger already present');
47+
}
48+
}
49+
addWindowLogger(opts) {
50+
if (_.isUndefined(this.windowLogger)) {
51+
if (!_.isUndefined(opts)) {
52+
let { windowOptions } = opts;
53+
this.windowLogger = new WindowLogger(windowOptions, this);
54+
}
55+
if (_.isUndefined(opts)) {
56+
let { windowOptions } = this.opts;
57+
this.windowLogger = new WindowLogger(windowOptions, this);
58+
}
59+
} else {
60+
throw new Error('logger already present');
61+
}
1562
}
1663
toggleActive() {
1764
this.active = !this.active;
18-
this.keyboardLogger.toggleActive();
19-
this.mouseLogger.toggleActive();
20-
this.windowLogger.toggleActive();
65+
if (!_.isUndefined(this.keyboardLogger)) {
66+
this.keyboardLogger.toggleActive();
67+
}
68+
if (!_.isUndefined(this.mouseLogger)) {
69+
this.mouseLogger.toggleActive();
70+
}
71+
if (!_.isUndefined(this.windowLogger)) {
72+
this.windowLogger.toggleActive();
73+
}
2174
}
2275
getWindow() {
23-
this.windowLogger.get();
76+
if (!_.isUndefined(this.windowLogger)) {
77+
this.windowLogger.get();
78+
}
2479
}
2580
}
2681

27-
export default (opts) => {
28-
if (!(this instanceof OsInputCapture)) {
29-
return new OsInputCapture(opts);
30-
}
31-
};
82+
let oic = exports;
83+
84+
oic.OsInputCapture = OsInputCapture;
85+
oic.KeyboardLogger = KeyboardLogger;
86+
oic.MouseLogger = MouseLogger;
87+
oic.WindowLogger = WindowLogger;

0 commit comments

Comments
 (0)