index.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="description" content="A basic Hello World HTML page with metadata and OpenGraph headers">
  7. <meta name="author" content="Your Name">
  8. <title>Hello World</title>
  9. <!-- OpenGraph Metadata -->
  10. <meta property="og:title" content="Hello World">
  11. <meta property="og:description" content="A basic Hello World HTML page with metadata and OpenGraph headers">
  12. <meta property="og:type" content="website">
  13. <meta property="og:url" content="http://example.com">
  14. <meta property="og:image" content="http://example.com/image.jpg">
  15. <style>
  16. body{
  17. margin: 0;
  18. }
  19. #remoteCapture{
  20. width: 100%;
  21. pointer-events: none;
  22. user-select: none;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <img id="remoteCapture" src="/stream"></img>
  28. <p>Click start to start connection to backend and start Capture to start KVM-ing</p>
  29. <button id="startButton">Start</button>
  30. <button id="stopButton">Stop</button>
  31. <button id="capture">Capture</button>
  32. <button id="screenshot">Win + Shift + S</button>
  33. <script>
  34. let socket;
  35. let protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
  36. let port = window.location.port ? window.location.port : (protocol === 'wss' ? 443 : 80);
  37. let socketURL = `${protocol}://${window.location.hostname}:${port}/hid`;
  38. /* Mouse */
  39. document.getElementById('capture').addEventListener('click', function(event) {
  40. event.preventDefault();
  41. event.stopPropagation();
  42. // Remove old listeners if they exist
  43. document.removeEventListener('mousemove', handleMouseMove);
  44. document.removeEventListener('mousedown', handleMouseDown);
  45. document.removeEventListener('mouseup', handleMouseUp);
  46. document.removeEventListener('wheel', handleScroll);
  47. // Add new listeners
  48. document.body.requestPointerLock();
  49. document.addEventListener('mousemove', handleMouseMove);
  50. document.addEventListener('mousedown', handleMouseDown);
  51. document.addEventListener('mouseup', handleMouseUp);
  52. document.addEventListener('wheel', handleScroll);
  53. if (socket) {
  54. // Reset USBKVM state to avoid stuck keys
  55. socket.send(JSON.stringify({ t: 'reset'}));
  56. }
  57. });
  58. function handleMouseDown(event) {
  59. console.log(`Mouse button pressed: Button=${event.button}`);
  60. if (socket) {
  61. socket.send(JSON.stringify({ t: 'mw', s: 'md', d: event.button+"" }));
  62. }
  63. }
  64. function handleMouseUp(event) {
  65. console.log(`Mouse button released: Button=${event.button}`);
  66. if (socket) {
  67. socket.send(JSON.stringify({ t: 'mw', s: 'mu', d: event.button+"" }));
  68. }
  69. }
  70. function handleScroll(event) {
  71. console.log(`Mouse scrolled: DeltaX=${event.deltaX}, DeltaY=${event.deltaY}`);
  72. if (socket) {
  73. socket.send(JSON.stringify({ t: 'ms', y: event.deltaY }));
  74. }
  75. }
  76. document.addEventListener('pointerlockchange', function() {
  77. if (document.pointerLockElement === document.body) {
  78. console.log('Pointer locked');
  79. } else {
  80. console.log('Pointer unlocked');
  81. document.removeEventListener('mousemove', handleMouseMove);
  82. }
  83. });
  84. function handleMouseMove(event) {
  85. console.log(`Mouse moved: X=${event.movementX}, Y=${event.movementY}`);
  86. if (socket) {
  87. socket.send(JSON.stringify({ t: 'mm', x: event.movementX, y: event.movementY }));
  88. }
  89. }
  90. /* Keyboard */
  91. document.getElementById('startButton').addEventListener('click', function() {
  92. const socketUrl = socketURL;
  93. socket = new WebSocket(socketUrl);
  94. socket.addEventListener('open', function(event) {
  95. console.log('WebSocket is connected.');
  96. });
  97. socket.addEventListener('message', function(event) {
  98. console.log('Message from server ', event.data);
  99. });
  100. document.addEventListener('keydown', handleKeyDown);
  101. document.addEventListener('keyup', handleKeyUp);
  102. });
  103. document.getElementById('stopButton').addEventListener('click', function() {
  104. if (socket) {
  105. socket.close();
  106. console.log('WebSocket is disconnected.');
  107. }
  108. document.removeEventListener('keydown', handleKeyDown);
  109. document.removeEventListener('keyup', handleKeyUp);
  110. });
  111. document.getElementById('screenshot').addEventListener('click', function() {
  112. if (socket) {
  113. // Send keydown for Shift
  114. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: 'LEFT_Shift' }));
  115. // Send keydown for Windows key
  116. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: 'Meta' }));
  117. // Send keydown for S
  118. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: 's' }));
  119. setTimeout(function() {
  120. // Send keyup for S
  121. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: 's' }));
  122. // Send keyup for Windows key
  123. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: 'Meta' }));
  124. // Send keyup for Shift
  125. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: 'LEFT_Shift' }));
  126. }, 1000);
  127. }
  128. });
  129. function isNumpadEvent(event) {
  130. return event.location === 3;
  131. }
  132. function handleKeyDown(event) {
  133. event.preventDefault();
  134. event.stopImmediatePropagation();
  135. const key = event.key;
  136. if (socket){
  137. if (key == "Shift" || key == "Control" || key == "Alt"){
  138. if (event.location == 1){
  139. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: "LEFT_" + key }));
  140. } else {
  141. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: "RIGHT_" + key }));
  142. }
  143. }else if (isNumpadEvent(event)){
  144. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: "NUMPAD_" + key }));
  145. }else if (key == "PrintScreen"){
  146. //Do nothing, press is hardware offloaded
  147. }else{
  148. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: key }));
  149. }
  150. }
  151. }
  152. function handleKeyUp(event) {
  153. event.preventDefault();
  154. event.stopImmediatePropagation();
  155. const key = event.key;
  156. if (socket) {
  157. if (key == "Shift" || key == "Control" || key == "Alt") {
  158. if (event.location == 1) {
  159. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: "LEFT_" + key }));
  160. } else {
  161. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: "RIGHT_" + key }));
  162. }
  163. }else if (isNumpadEvent(event)){
  164. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: "NUMPAD_" + key }));
  165. }else if (key == "NumLock" || key == "Pause"){
  166. //Do nothing, release is hardware offloaded
  167. } else {
  168. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: key }));
  169. }
  170. }
  171. }
  172. </script>
  173. </body>
  174. </html>