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).

1  
// TODO: better logic that doesn't poll all the time
2  
sclass JSSendMouseEventsToWebSocket {
3  
  int interval = 100; // 0 for continuous
4  
  bool verbose;
5  
  
6  
  S html() {
7  
    ret hjs(js());
8  
  }
9  
  
10  
  S js() {
11  
    O[] params = litparams(+interval, +verbose);
12  
    
13  
    if (interval == 0) ret replaceDollarVars([[
14  
      console.log("Note: Streaming mouse events to web socket.");
15  
      
16  
      var mouseVerbose = $verbose;
17  
      var mouseX = -1, mouseY = -1;
18  
      
19  
      function wsSendMouse() {
20  
        if (wsReady) {
21  
          var msg = JSON.stringify({"mouse": [mouseX, mouseY]});
22  
          if (mouseVerbose) console.log(msg);
23  
          ws.send(msg);
24  
        }
25  
      }
26  
      
27  
      $("body").mousemove(function(e) {
28  
        mouseX = e.pageX;
29  
        mouseY = e.pageY;
30  
        wsSendMouse();
31  
      });
32  
    ]], params);
33  
    
34  
    ret replaceDollarVars([[
35  
      var mouseInterval = $interval;
36  
      var mouseX = -1, mouseY = -1;
37  
      
38  
      $("body").mousemove(function(e) {
39  
        mouseX = e.pageX;
40  
        mouseY = e.pageY;
41  
      });
42  
      
43  
      function wsSendMouse() {
44  
        if (wsReady)
45  
          ws.send(JSON.stringify({"mouse": [mouseX, mouseY]}));
46  
      }
47  
      
48  
      setTimeout(function sendMouse, mouseInterval);
49  
    ]], params);
50  
  }
51  
}

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: 125 / 288
Version history: 8 change(s)
Referenced in: [show references]