I wanted to use the SendInput function from the windows Api in nodejs, using the FFI package.
My knowledge of C is limited so I can't really figure out what problem I have, I'm basically trying to Virtually press a key on the keyboard.
That's the code I have:
var ffi = require('ffi');
var ref = require ('ref');
var struct = require ('ref-struct');
var keyboardInput = struct({
'type': 'int',
'wVK': 'int',
'wScan': 'int',
'dwFlags': 'int',
'time': 'int',
'dwExtraInfo': 'int64'
});
var keyboardInputPtr = ref.refType(keyboardInput);
var keyboard = new keyboardInput();
keyboard.type = 1;
keyboard.wVK = 0x41;
keyboard.wScan = 0;
keyboard.dwFlags = 2;
keyboard.time = 0;
keyboard.dwExtraInfo = 0;
var user32 = ffi.Library('user32', {
'SendInput': [ 'int', [ 'uint', keyboardInputPtr, 'int' ] ]
});
setInterval(function(){
var r = user32.SendInput(1, keyboard.ref(), 40);
console.log(r);
}, 500);
It logs me a "1" in the console, shouldn't that mean it works? Because I don't get a key pressed when I open notepad.
The "1" tells you that 1 event was inserted, not what the event actually is. I don't know about FFI but it seems to me that keyboardInput has some invalid type definitions. wVK and wScan must be 16-bit integers (hence the 'w' for WORD). Since they are typed the same as dwFlags (an 'int') that's cause invalid input values.