Linux get notification on focused gui window change

前端 未结 2 2037
情话喂你
情话喂你 2021-02-09 09:52

In linux, is it possible to get notifications when the currently focused GUI app changes? I\'m writing an app that tracks how long a user stays on each GUI app(per process, not

2条回答
  •  难免孤独
    2021-02-09 10:26

    Example in JavaScript using node-x11:

    var x11 = require('x11');
    x11.createClient(function(err, display) {
      var X = display.client;
      X.ChangeWindowAttributes(display.screen[0].root, { eventMask: x11.eventMask.PropertyChange });
      X.on('event', function(ev) {
        if(ev.name == 'PropertyNotify') {
          X.GetAtomName(ev.atom, function(err, name) {
            if (name == '_NET_ACTIVE_WINDOW') {
              X.GetProperty(0, ev.window, ev.atom, X.atoms.WINDOW, 0, 4, function(err, prop) {
                console.log('New active window:' + prop.data.readUInt32LE(0));
              });
            }
          });
        }
      });
    });
    

提交回复
热议问题