index.html 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. <script>
  22. let socket;
  23. let protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
  24. let port = window.location.port ? window.location.port : (protocol === 'wss' ? 443 : 80);
  25. let socketURL = `${protocol}://${window.location.hostname}:${port}/hid`;
  26. document.getElementById('startButton').addEventListener('click', function() {
  27. const socketUrl = socketURL;
  28. socket = new WebSocket(socketUrl);
  29. socket.addEventListener('open', function(event) {
  30. console.log('WebSocket is connected.');
  31. });
  32. socket.addEventListener('message', function(event) {
  33. console.log('Message from server ', event.data);
  34. });
  35. document.addEventListener('keydown', handleKeyDown);
  36. document.addEventListener('keyup', handleKeyUp);
  37. });
  38. document.getElementById('stopButton').addEventListener('click', function() {
  39. if (socket) {
  40. socket.close();
  41. console.log('WebSocket is disconnected.');
  42. }
  43. document.removeEventListener('keydown', handleKeyDown);
  44. document.removeEventListener('keyup', handleKeyUp);
  45. });
  46. function isNumpadEvent(event) {
  47. return event.location === 3;
  48. }
  49. function handleKeyDown(event) {
  50. event.preventDefault();
  51. const key = event.key;
  52. if (socket){
  53. if (key == "Shift" || key == "Control" || key == "Alt"){
  54. if (event.location == 1){
  55. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: "LEFT_" + key }));
  56. } else {
  57. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: "RIGHT_" + key }));
  58. }
  59. }else if (isNumpadEvent(event)){
  60. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: "NUMPAD_" + key }));
  61. }else{
  62. socket.send(JSON.stringify({ t: 'kw', s: 'kd', d: key }));
  63. }
  64. }
  65. }
  66. function handleKeyUp(event) {
  67. event.preventDefault();
  68. const key = event.key;
  69. if (socket) {
  70. if (key == "Shift" || key == "Control" || key == "Alt") {
  71. if (event.location == 1) {
  72. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: "LEFT_" + key }));
  73. } else {
  74. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: "RIGHT_" + key }));
  75. }
  76. }else if (isNumpadEvent(event)){
  77. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: "NUMPAD_" + key }));
  78. }else if (key == "NumLock"){
  79. //Do nothing, release is hardware offloaded
  80. } else {
  81. socket.send(JSON.stringify({ t: 'kw', s: 'ku', d: key }));
  82. }
  83. }
  84. }
  85. </script>
  86. </body>
  87. </html>