Why does Node hang when using deasync with x11 eve

2019-09-07 08:34发布

问题:

I'm trying to use the node modules deasync and x11 to perform actions when certain keys are pressed.

When I use deasync inside a callback that has been initiated by a keypress deasync seems to be stuck in an endless loop. It works fine if I create a generic event myself.

Run the following script using xtrace to see that X11 does respond: xtrace -D :10 ./the-script

#!/usr/bin/env node

var deasync = require('deasync');
var x11 = require('x11');

var display = (deasync(x11.createClient)());
var client = display.client;

var getInputFocus = deasync(client.GetInputFocus)
    .bind(client);

var focus1 = getInputFocus();
console.log("getting focus here works:", focus1);

// grab the "1"-key - keyCode = 10
client.GrabKey(display.screen[0].root, 0, null, 10, 0, 1);

client.on('event', processKeyPressEvent);

// client.emit("event"); // works

function processKeyPressEvent(event) {
    console.log("can see this");
    var focus2 = getInputFocus(); // problem
    console.log("never get here");
}

Thanx for your help.