index.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
  16. <style>
  17. body{
  18. margin: 0;
  19. }
  20. #remoteCapture{
  21. width: 100%;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <img id="remoteCapture" src="/stream" oncontextmenu="return false;"></img>
  27. <script>
  28. let socket;
  29. let protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
  30. let port = window.location.port ? window.location.port : (protocol === 'wss' ? 443 : 80);
  31. let socketURL = `${protocol}://${window.location.hostname}:${port}/hid`;
  32. let mouseMoveAbsolte = true; // Set to true for absolute mouse coordinates, false for relativeZ
  33. let mouseScrollSensitivity = 0.01; // Adjust this value to change scroll sensitivity
  34. let mouseIsOutside = false;
  35. /* Mouse events */
  36. function handleMouseMove(event) {
  37. const hidCommand = {
  38. event: 2,
  39. mouse_x: event.clientX,
  40. mouse_y: event.clientY
  41. };
  42. const rect = event.target.getBoundingClientRect();
  43. const relativeX = event.clientX - rect.left;
  44. const relativeY = event.clientY - rect.top;
  45. if (relativeX < 0 || relativeY < 0 || relativeX > rect.width || relativeY > rect.height) {
  46. mouseIsOutside = true;
  47. return; // Mouse is outside the client rect
  48. }
  49. mouseIsOutside = false;
  50. const percentageX = (relativeX / rect.width) * 4096;
  51. const percentageY = (relativeY / rect.height) * 4096;
  52. hidCommand.mouse_x = Math.round(percentageX);
  53. hidCommand.mouse_y = Math.round(percentageY);
  54. console.log(`Mouse move: (${event.clientX}, ${event.clientY})`);
  55. console.log(`Mouse move relative: (${relativeX}, ${relativeY})`);
  56. console.log(`Mouse move percentage: (${hidCommand.mouse_x}, ${hidCommand.mouse_y})`);
  57. if (socket && socket.readyState === WebSocket.OPEN) {
  58. socket.send(JSON.stringify(hidCommand));
  59. } else {
  60. console.error("WebSocket is not open.");
  61. }
  62. }
  63. function handleMousePress(event) {
  64. event.preventDefault();
  65. event.stopImmediatePropagation();
  66. if (mouseIsOutside) {
  67. console.warn("Mouse is outside the capture area, ignoring mouse press.");
  68. return;
  69. }
  70. const buttonMap = {
  71. 0: 1,
  72. 1: 3,
  73. 2: 2
  74. }; //Map javascript mouse buttons to HID buttons
  75. const hidCommand = {
  76. event: 3,
  77. mouse_button: buttonMap[event.button] || 0
  78. };
  79. console.log(`Mouse down: ${hidCommand.mouse_button}`);
  80. if (socket && socket.readyState === WebSocket.OPEN) {
  81. socket.send(JSON.stringify(hidCommand));
  82. } else {
  83. console.error("WebSocket is not open.");
  84. }
  85. }
  86. function handleMouseRelease(event) {
  87. event.preventDefault();
  88. event.stopImmediatePropagation();
  89. if (mouseIsOutside) {
  90. console.warn("Mouse is outside the capture area, ignoring mouse press.");
  91. return;
  92. }
  93. const buttonMap = {
  94. 0: 1,
  95. 1: 3,
  96. 2: 2
  97. }; //Map javascript mouse buttons to HID buttons
  98. const hidCommand = {
  99. event: 4,
  100. mouse_button: buttonMap[event.button] || 0
  101. };
  102. console.log(`Mouse release: ${hidCommand.mouse_button}`);
  103. if (socket && socket.readyState === WebSocket.OPEN) {
  104. socket.send(JSON.stringify(hidCommand));
  105. } else {
  106. console.error("WebSocket is not open.");
  107. }
  108. }
  109. function handleMouseScroll(event) {
  110. const hidCommand = {
  111. event: 5,
  112. mouse_scroll: parseInt(event.deltaY * mouseScrollSensitivity)
  113. };
  114. if (mouseIsOutside) {
  115. console.warn("Mouse is outside the capture area, ignoring mouse press.");
  116. return;
  117. }
  118. console.log(`Mouse scroll: mouse_scroll=${event.deltaY} (scaled: ${hidCommand.mouse_scroll})`);
  119. if (socket && socket.readyState === WebSocket.OPEN) {
  120. socket.send(JSON.stringify(hidCommand));
  121. } else {
  122. console.error("WebSocket is not open.");
  123. }
  124. }
  125. // Attach mouse event listeners
  126. document.addEventListener('mousemove', handleMouseMove);
  127. document.addEventListener('mousedown', handleMousePress);
  128. document.addEventListener('mouseup', handleMouseRelease);
  129. document.addEventListener('wheel', handleMouseScroll);
  130. /* Keyboard */
  131. function isNumpadEvent(event) {
  132. return event.location === 3;
  133. }
  134. function handleKeyDown(event) {
  135. event.preventDefault();
  136. event.stopImmediatePropagation();
  137. const key = event.key;
  138. let hidCommand = {
  139. event: 0,
  140. keycode: event.keyCode
  141. };
  142. console.log(`Key down: ${key} (code: ${event.keyCode})`);
  143. // Check if the key is a modkey on the right side of the keyboard
  144. const rightModKeys = ['Control', 'Alt', 'Shift', 'Meta'];
  145. if (rightModKeys.includes(key) && event.location === 2) {
  146. hidCommand.is_right_modifier_key = true;
  147. }else if (key === 'Enter' && isNumpadEvent(event)) {
  148. //Special case for Numpad Enter
  149. hidCommand.is_right_modifier_key = true;
  150. }else{
  151. hidCommand.is_right_modifier_key = false;
  152. }
  153. if (socket && socket.readyState === WebSocket.OPEN) {
  154. socket.send(JSON.stringify(hidCommand));
  155. } else {
  156. console.error("WebSocket is not open.");
  157. }
  158. }
  159. function handleKeyUp(event) {
  160. event.preventDefault();
  161. event.stopImmediatePropagation();
  162. const key = event.key;
  163. let hidCommand = {
  164. event: 1,
  165. keycode: event.keyCode
  166. };
  167. console.log(`Key up: ${key} (code: ${event.keyCode})`);
  168. // Check if the key is a modkey on the right side of the keyboard
  169. const rightModKeys = ['Control', 'Alt', 'Shift', 'Meta'];
  170. if (rightModKeys.includes(key) && event.location === 2) {
  171. hidCommand.is_right_modifier_key = true;
  172. } else if (key === 'Enter' && isNumpadEvent(event)) {
  173. //Special case for Numpad Enter
  174. hidCommand.is_right_modifier_key = true;
  175. }else{
  176. hidCommand.is_right_modifier_key = false;
  177. }
  178. if (socket && socket.readyState === WebSocket.OPEN) {
  179. socket.send(JSON.stringify(hidCommand));
  180. } else {
  181. console.error("WebSocket is not open.");
  182. }
  183. }
  184. /* Start and Stop events */
  185. function startWebSocket(){
  186. if (socket){
  187. //Already started
  188. alert("Websocket already started");
  189. return;
  190. }
  191. const socketUrl = socketURL;
  192. socket = new WebSocket(socketUrl);
  193. socket.addEventListener('open', function(event) {
  194. console.log('WebSocket is connected.');
  195. });
  196. socket.addEventListener('message', function(event) {
  197. console.log('Message from server ', event.data);
  198. });
  199. document.addEventListener('keydown', handleKeyDown);
  200. document.addEventListener('keyup', handleKeyUp);
  201. }
  202. function stopWebSocket(){
  203. if (!socket){
  204. alert("No ws connection to stop");
  205. return;
  206. }
  207. socket.close();
  208. console.log('WebSocket is disconnected.');
  209. document.removeEventListener('keydown', handleKeyDown);
  210. document.removeEventListener('keyup', handleKeyUp);
  211. }
  212. $(document).ready(function(){
  213. startWebSocket();
  214. });
  215. </script>
  216. </body>
  217. </html>