Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

51
LINES

< > BotCompany Repo | #1031706 // JSSendMouseEventsToWebSocket (only motion right now)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3343L/20K).

// TODO: better logic that doesn't poll all the time
sclass JSSendMouseEventsToWebSocket {
  int interval = 100; // 0 for continuous
  bool verbose;
  
  S html() {
    ret hjs(js());
  }
  
  S js() {
    O[] params = litparams(+interval, +verbose);
    
    if (interval == 0) ret replaceDollarVars([[
      console.log("Note: Streaming mouse events to web socket.");
      
      var mouseVerbose = $verbose;
      var mouseX = -1, mouseY = -1;
      
      function wsSendMouse() {
        if (wsReady) {
          var msg = JSON.stringify({"mouse": [mouseX, mouseY]});
          if (mouseVerbose) console.log(msg);
          ws.send(msg);
        }
      }
      
      $("body").mousemove(function(e) {
        mouseX = e.pageX;
        mouseY = e.pageY;
        wsSendMouse();
      });
    ]], params);
    
    ret replaceDollarVars([[
      var mouseInterval = $interval;
      var mouseX = -1, mouseY = -1;
      
      $("body").mousemove(function(e) {
        mouseX = e.pageX;
        mouseY = e.pageY;
      });
      
      function wsSendMouse() {
        if (wsReady)
          ws.send(JSON.stringify({"mouse": [mouseX, mouseY]}));
      }
      
      setTimeout(function sendMouse, mouseInterval);
    ]], params);
  }
}

Author comment

Began life as a copy of #1031659

download  show line numbers  debug dex  old transpilations   

Travelled to 5 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031706
Snippet name: JSSendMouseEventsToWebSocket (only motion right now)
Eternal ID of this version: #1031706/9
Text MD5: 56e5e7ec1b392aa1276e547a8a078d6b
Transpilation MD5: 6b49ff47e471bd9bfa756f06a3c448ee
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-28 15:14:37
Source code size: 1290 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 181 / 371
Version history: 8 change(s)
Referenced in: [show references]