Created
January 19, 2018 07:27
-
-
Save mooyoul/d74ab735e6fd4afeffe2163d32cc6a3c to your computer and use it in GitHub Desktop.
node-ffi
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
/** | |
* Created by Prescott on 2015-05-08. | |
*/ | |
const ffi = require("ffi"); | |
// const ref = require('ref'); | |
/* | |
const intPtr = ref.refType(ref.types.int32); | |
const Ucs2String = { | |
indirection: 1, | |
size: ref.sizeof.pointer, | |
get: function (buffer, offset) { | |
var _buf = buffer.readPointer(offset); | |
// TODO: a *real* way to detect the end of the ucs2 string. this is a band-aid... | |
return _buf.reinterpret(10000).toString('ucs2'); | |
}, | |
set: function (buffer, offset, string) { | |
var _buf = new Buffer(Buffer.byteLength(string, 'ucs2') + 1); | |
_buf.write(string, 'ucs2'); | |
_buf[_buf.length - 1] = 0; | |
return buffer.writePointer(_buf, offset); | |
} | |
}; | |
*/ | |
const user32 = ffi.Library('user32', { | |
/** | |
* Signature | |
* MethodName: [ReturnType, [Args[0]Type, Args[1]Type, ...]] | |
*/ | |
'SetCursorPos': [ 'bool', ['int', 'int'] ], | |
'mouse_event': ['void', ['int', 'int', 'int', 'int', 'int']] | |
/* | |
'FindWindowA': ['pointer', ['string', 'string']], | |
'FindWindowExA': ['pointer', ['pointer', 'pointer', 'string', 'string']] | |
'SendMessage ': ['int', ['pointer', 'uint', 'pointer', 'pointer']] | |
'FindWindowW': ['pointer', [ Ucs2String, Ucs2String ]] | |
*/ | |
// put other functions that you want to use from the library here, e.g., "GetCursorPos" | |
}); | |
const MOUSEEVENTF_LEFTDOWN = 0x02; | |
const MOUSEEVENTF_LEFTUP = 0x04; | |
module.exports = exports = function emulateClick(x, y) { | |
user32.SetCursorPos(x, y); | |
user32.mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0); | |
user32.mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment