问题描述:

I wrote following code to set keyboard cursor position. But get garbage. Any clue, what am I missing?

var ffi = require('ffi');

var ref = require('ref');

var Struct = require('ref-struct');

var point = Struct({

'x': 'long',

'y': 'long'

});

var user32 = ffi.Library('user32.dll', {

GetCaretPos:['bool',[locPtr]]

});

var pbuf = new point();

caretpos = user32.GetCaretPos(pbuf);

console.log(":",pbuf.x );

Next, I tried the following, but that doesn't work as well.

var ffi = require('ffi');

var ref = require('ref');

var Struct = require('ref-struct');

var voidPtr = ref.refType(ref.types.void);

var user32 = ffi.Library('user32.dll', {

GetCaretPos:['bool',[voidPtr]]

});

var pbuf = new Buffer(2);

caretpos = user32.GetCaretPos(pbuf);

var cpos =(new Uint8Array(pbuf));

console.log(">",cpos ); //Doesn't work **> Uint8Array [ 0, 0, 0, 0 ]**

网友答案:

As the article heading states "GetCursorPos in Node FFI" and return pointer x,y

The code below works for me, its a hack to get the x,y pointer.

var ffi = require('ffi');
var repbuffer = new Buffer(16); // holder for windows structures
var user32 = ffi.Library('user32.dll', {
    'GetCursorPos':['bool',['pointer']]
});
//Show mouse cords at console every sec
setInterval( function() { getmousepos();}, 1000);
function getmousepos(){
    var p = user32.GetCursorPos(repbuffer);
    var x= repbuffer[0]+ (repbuffer[1]*256); 
    var y= repbuffer[4]+(repbuffer[5]*256);  
    console.log(x + " " + y);
}
相关阅读:
Top