index.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 mouseIsOutside = false;
  34. let mouseButtonState = [0, 0, 0]; // Array to track mouse button states, left, middle, right
  35. /* Mouse events */
  36. function handleMouseMove(event) {
  37. const mouseButtonBits = mouseButtonState.reduce((acc, state, index) => {
  38. return acc | (state << index);
  39. }, 0);
  40. const hidCommand = {
  41. event: 2,
  42. mouse_x: event.clientX,
  43. mouse_y: event.clientY,
  44. mouse_move_button_state: mouseButtonBits // Combine mouse button states into a single bitmask
  45. };
  46. const rect = event.target.getBoundingClientRect();
  47. const relativeX = event.clientX - rect.left;
  48. const relativeY = event.clientY - rect.top;
  49. if (relativeX < 0 || relativeY < 0 || relativeX > rect.width || relativeY > rect.height) {
  50. mouseIsOutside = true;
  51. return; // Mouse is outside the client rect
  52. }
  53. mouseIsOutside = false;
  54. const percentageX = (relativeX / rect.width) * 4096;
  55. const percentageY = (relativeY / rect.height) * 4096;
  56. hidCommand.mouse_x = Math.round(percentageX);
  57. hidCommand.mouse_y = Math.round(percentageY);
  58. console.log(`Mouse move: (${event.clientX}, ${event.clientY})`);
  59. console.log(`Mouse move relative: (${relativeX}, ${relativeY})`);
  60. console.log(`Mouse move percentage: (${hidCommand.mouse_x}, ${hidCommand.mouse_y})`);
  61. if (socket && socket.readyState === WebSocket.OPEN) {
  62. socket.send(JSON.stringify(hidCommand));
  63. } else {
  64. console.error("WebSocket is not open.");
  65. }
  66. }
  67. function handleMousePress(event) {
  68. event.preventDefault();
  69. event.stopImmediatePropagation();
  70. if (mouseIsOutside) {
  71. console.warn("Mouse is outside the capture area, ignoring mouse press.");
  72. return;
  73. }
  74. const buttonMap = {
  75. 0: 1,
  76. 1: 3,
  77. 2: 2
  78. }; //Map javascript mouse buttons to HID buttons
  79. const hidCommand = {
  80. event: 3,
  81. mouse_button: buttonMap[event.button] || 0
  82. };
  83. // Update mouse button state
  84. if (event.button >= 0 && event.button < mouseButtonState.length) {
  85. mouseButtonState[event.button] = 1; // Set button state to pressed
  86. }
  87. // Log the mouse button state
  88. console.log(`Mouse down: ${hidCommand.mouse_button}`);
  89. if (socket && socket.readyState === WebSocket.OPEN) {
  90. socket.send(JSON.stringify(hidCommand));
  91. } else {
  92. console.error("WebSocket is not open.");
  93. }
  94. }
  95. function handleMouseRelease(event) {
  96. event.preventDefault();
  97. event.stopImmediatePropagation();
  98. if (mouseIsOutside) {
  99. console.warn("Mouse is outside the capture area, ignoring mouse press.");
  100. return;
  101. }
  102. const buttonMap = {
  103. 0: 1,
  104. 1: 3,
  105. 2: 2
  106. }; //Map javascript mouse buttons to HID buttons
  107. const hidCommand = {
  108. event: 4,
  109. mouse_button: buttonMap[event.button] || 0
  110. };
  111. // Update mouse button state
  112. if (event.button >= 0 && event.button < mouseButtonState.length) {
  113. mouseButtonState[event.button] = 0; // Set button state to released
  114. }
  115. console.log(`Mouse release: ${hidCommand.mouse_button}`);
  116. if (socket && socket.readyState === WebSocket.OPEN) {
  117. socket.send(JSON.stringify(hidCommand));
  118. } else {
  119. console.error("WebSocket is not open.");
  120. }
  121. }
  122. function handleMouseScroll(event) {
  123. const hidCommand = {
  124. event: 5,
  125. mouse_scroll: event.deltaY
  126. };
  127. if (mouseIsOutside) {
  128. console.warn("Mouse is outside the capture area, ignoring mouse press.");
  129. return;
  130. }
  131. console.log(`Mouse scroll: mouse_scroll=${event.deltaY}`);
  132. if (socket && socket.readyState === WebSocket.OPEN) {
  133. socket.send(JSON.stringify(hidCommand));
  134. } else {
  135. console.error("WebSocket is not open.");
  136. }
  137. }
  138. // Attach mouse event listeners
  139. document.addEventListener('mousemove', handleMouseMove);
  140. document.addEventListener('mousedown', handleMousePress);
  141. document.addEventListener('mouseup', handleMouseRelease);
  142. document.addEventListener('wheel', handleMouseScroll);
  143. /* Keyboard */
  144. function isNumpadEvent(event) {
  145. return event.location === 3;
  146. }
  147. function handleKeyDown(event) {
  148. event.preventDefault();
  149. event.stopImmediatePropagation();
  150. const key = event.key;
  151. let hidCommand = {
  152. event: 0,
  153. keycode: event.keyCode
  154. };
  155. console.log(`Key down: ${key} (code: ${event.keyCode})`);
  156. // Check if the key is a modkey on the right side of the keyboard
  157. const rightModKeys = ['Control', 'Alt', 'Shift', 'Meta'];
  158. if (rightModKeys.includes(key) && event.location === 2) {
  159. hidCommand.is_right_modifier_key = true;
  160. }else if (key === 'Enter' && isNumpadEvent(event)) {
  161. //Special case for Numpad Enter
  162. hidCommand.is_right_modifier_key = true;
  163. }else{
  164. hidCommand.is_right_modifier_key = false;
  165. }
  166. if (socket && socket.readyState === WebSocket.OPEN) {
  167. socket.send(JSON.stringify(hidCommand));
  168. } else {
  169. console.error("WebSocket is not open.");
  170. }
  171. }
  172. function handleKeyUp(event) {
  173. event.preventDefault();
  174. event.stopImmediatePropagation();
  175. const key = event.key;
  176. let hidCommand = {
  177. event: 1,
  178. keycode: event.keyCode
  179. };
  180. console.log(`Key up: ${key} (code: ${event.keyCode})`);
  181. // Check if the key is a modkey on the right side of the keyboard
  182. const rightModKeys = ['Control', 'Alt', 'Shift', 'Meta'];
  183. if (rightModKeys.includes(key) && event.location === 2) {
  184. hidCommand.is_right_modifier_key = true;
  185. } else if (key === 'Enter' && isNumpadEvent(event)) {
  186. //Special case for Numpad Enter
  187. hidCommand.is_right_modifier_key = true;
  188. }else{
  189. hidCommand.is_right_modifier_key = false;
  190. }
  191. if (socket && socket.readyState === WebSocket.OPEN) {
  192. socket.send(JSON.stringify(hidCommand));
  193. } else {
  194. console.error("WebSocket is not open.");
  195. }
  196. }
  197. /* Start and Stop events */
  198. function startWebSocket(){
  199. if (socket){
  200. //Already started
  201. alert("Websocket already started");
  202. return;
  203. }
  204. const socketUrl = socketURL;
  205. socket = new WebSocket(socketUrl);
  206. socket.addEventListener('open', function(event) {
  207. console.log('WebSocket is connected.');
  208. });
  209. socket.addEventListener('message', function(event) {
  210. console.log('Message from server ', event.data);
  211. });
  212. document.addEventListener('keydown', handleKeyDown);
  213. document.addEventListener('keyup', handleKeyUp);
  214. }
  215. function stopWebSocket(){
  216. if (!socket){
  217. alert("No ws connection to stop");
  218. return;
  219. }
  220. socket.close();
  221. console.log('WebSocket is disconnected.');
  222. document.removeEventListener('keydown', handleKeyDown);
  223. document.removeEventListener('keyup', handleKeyUp);
  224. }
  225. $(document).ready(function(){
  226. startWebSocket();
  227. });
  228. </script>
  229. </body>
  230. </html>