index.html 7.7 KB

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