keyboard.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <div id="myWindow" class="window">
  2. <div class="window-top">
  3. <button class="round green"></button><button class="round yellow"></button><button class="round red"></button>
  4. </div>
  5. <div class="window-content">
  6. <div class="simple-keyboard"></div>
  7. </div>
  8. </div>
  9. <script src="https://cdn.jsdelivr.net/npm/simple-keyboard@latest/build/index.js"></script>
  10. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/simple-keyboard@latest/build/css/index.css" />
  11. <style>
  12. body {
  13. background: #333;
  14. }
  15. .window {
  16. position: fixed;
  17. top: 0;
  18. width: 400px;
  19. height: 250px;
  20. border-radius: 10px;
  21. border: none;
  22. box-shadow: 1px 1px 4px rgba(0, 0, 0, 0, 0.9), -1px 1px 4px rgba(0, 0, 0, 0, 0.9);
  23. background: #fff;
  24. }
  25. .window-content {
  26. height: 100%;
  27. }
  28. .window-top,
  29. .window-top-no-bind {
  30. cursor: move;
  31. text-align: right;
  32. height: 20px;
  33. border-top-right-radius: 5px;
  34. border-top-left-radius: 5px;
  35. padding: 5px;
  36. background-color: #ddd;
  37. }
  38. .window-top-no-bind {
  39. cursor: inherit;
  40. }
  41. .round {
  42. height: 16px;
  43. width: 16px;
  44. border-radius: 50%;
  45. border: none;
  46. margin-right: 6px;
  47. box-shadow: 1px 1px 2px #000;
  48. }
  49. .green {
  50. background-color: limegreen;
  51. }
  52. .yellow {
  53. background-color: yellow;
  54. }
  55. .red {
  56. cursor: pointer;
  57. background-color: red;
  58. }
  59. #myWindow {
  60. z-index: 999;
  61. }
  62. #myWindow2 {
  63. top: 0;
  64. left: 500px;
  65. }
  66. </style>
  67. <script>
  68. // You can choose to have an element with the class "window-top" inside of your draggable window that will act as the "handle" for the window or it will attach to the element itself
  69. function makeDraggable(elmnt) {
  70. // Make an element draggable (or if it has a .window-top class, drag based on the .window-top element)
  71. let currentPosX = 0, currentPosY = 0, previousPosX = 0, previousPosY = 0;
  72. // If there is a window-top classed element, attach to that element instead of full window
  73. if (elmnt.querySelector('.window-top')) {
  74. // If present, the window-top element is where you move the parent element from
  75. elmnt.querySelector('.window-top').onmousedown = dragMouseDown;
  76. elmnt.querySelector('.window-top').ontouchstart = dragMouseDown;
  77. }
  78. else {
  79. // Otherwise, move the element itself
  80. elmnt.onmousedown = dragMouseDown;
  81. }
  82. function dragMouseDown(e) {
  83. // Prevent any default action on this element (you can remove if you need this element to perform its default action)
  84. e.preventDefault();
  85. // Get the mouse cursor position and set the initial previous positions to begin
  86. previousPosX = e.clientX;
  87. previousPosY = e.clientY;
  88. // When the mouse is let go, call the closing event
  89. document.onmouseup = closeDragElement;
  90. // call a function whenever the cursor moves
  91. document.onmousemove = elementDrag;
  92. }
  93. function elementDrag(e) {
  94. // Prevent any default action on this element (you can remove if you need this element to perform its default action)
  95. e.preventDefault();
  96. // Calculate the new cursor position by using the previous x and y positions of the mouse
  97. currentPosX = previousPosX - e.clientX;
  98. currentPosY = previousPosY - e.clientY;
  99. // Replace the previous positions with the new x and y positions of the mouse
  100. previousPosX = e.clientX;
  101. previousPosY = e.clientY;
  102. // Set the element's new position
  103. elmnt.style.top = (elmnt.offsetTop - currentPosY) + 'px';
  104. elmnt.style.left = (elmnt.offsetLeft - currentPosX) + 'px';
  105. }
  106. function closeDragElement() {
  107. // Stop moving when mouse button is released and release events
  108. document.onmouseup = null;
  109. document.onmousemove = null;
  110. }
  111. }
  112. // Make myWindow and myWindow2 draggable in different ways...
  113. // myWindow will only be able to be moved via the top bar (.window-top element). The main element does nothing on mouse down.
  114. makeDraggable(document.querySelector('#myWindow'));
  115. //And this is just for fun
  116. for (const closeElement of document.querySelectorAll('.round.red')) {
  117. closeElement.addEventListener('click', function () {
  118. });
  119. }
  120. const Keyboard = window.SimpleKeyboard.default;
  121. const keyboard = new Keyboard({
  122. onChange: input => onChange(input),
  123. onKeyPress: button => onKeyPress(button)
  124. });
  125. function onChange(input) {
  126. // document.querySelector(".input").value = input;
  127. console.log("Input changed", input);
  128. }
  129. function onKeyPress(button) {
  130. console.log("Button pressed", button);
  131. }
  132. </script>