index.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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>Welcome to my website!</p>
  19. <button id="startButton">Start</button>
  20. <button id="stopButton">Stop</button>
  21. <button id="capture">Capture</button>
  22. <script>
  23. let socket;
  24. let protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
  25. let port = window.location.port ? window.location.port : (protocol === 'wss' ? 443 : 80);
  26. let socketURL = `${protocol}://${window.location.hostname}:${port}/hid`;
  27. /* Mouse */
  28. document.getElementById('capture').addEventListener('click', function() {
  29. document.body.requestPointerLock();
  30. document.addEventListener('mousemove', handleMouseMove);
  31. });
  32. document.addEventListener('pointerlockchange', function() {
  33. if (document.pointerLockElement === document.body) {
  34. console.log('Pointer locked');
  35. } else {
  36. console.log('Pointer unlocked');
  37. document.removeEventListener('mousemove', handleMouseMove);
  38. }
  39. });
  40. function handleMouseMove(event) {
  41. console.log(`Mouse moved: X=${event.movementX}, Y=${event.movementY}`);
  42. if (socket) {
  43. socket.send(JSON.stringify({ t: 'mm', x: event.movementX, y: event.movementY }));
  44. }
  45. }
  46. /* Keyboard */
  47. document.getElementById('startButton').addEventListener('click', function() {
  48. const socketUrl = socketURL;
  49. socket = new WebSocket(socketUrl);
  50. socket.addEventListener('open', function(event) {
  51. console.log('WebSocket is connected.');
  52. });
  53. socket.addEventListener('message', function(event) {
  54. console.log('Message from server ', event.data);
  55. });
  56. document.addEventListener('keydown', handleKeyDown);
  57. document.addEventListener('keyup', handleKeyUp);
  58. });
  59. document.getElementById('stopButton').addEventListener('click', function() {
  60. if (socket) {
  61. socket.close();
  62. console.log('WebSocket is disconnected.');
  63. }
  64. document.removeEventListener('keydown', handleKeyDown);
  65. document.removeEventListener('keyup', handleKeyUp);
  66. });
  67. function isNumpadEvent(event) {
  68. return event.location === 3;
  69. }
  70. function handleKeyDown(event) {
  71. event.preventDefault();
  72. const key = event.key;
  73. if (socket){
  74. if (key == "Shift" || key == "Control" || key == "Alt"){
  75. if (event.location == 1){
  76. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: "LEFT_" + key }));
  77. } else {
  78. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: "RIGHT_" + key }));
  79. }
  80. }else if (isNumpadEvent(event)){
  81. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: "NUMPAD_" + key }));
  82. }else if (key == "PrintScreen"){
  83. //Do nothing, press is hardware offloaded
  84. }else{
  85. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: key }));
  86. }
  87. }
  88. }
  89. function handleKeyUp(event) {
  90. event.preventDefault();
  91. const key = event.key;
  92. if (socket) {
  93. if (key == "Shift" || key == "Control" || key == "Alt") {
  94. if (event.location == 1) {
  95. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: "LEFT_" + key }));
  96. } else {
  97. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: "RIGHT_" + key }));
  98. }
  99. }else if (isNumpadEvent(event)){
  100. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: "NUMPAD_" + key }));
  101. }else if (key == "NumLock" || key == "Pause"){
  102. //Do nothing, release is hardware offloaded
  103. } else {
  104. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: key }));
  105. }
  106. }
  107. }
  108. </script>
  109. </body>
  110. </html>