Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const keyboardLayout = {
  2.   en: {
  3.     backquote: ['`', '~'],
  4.     digit1: ['1', '!'],
  5.     digit2: ['2', '@'],
  6.     digit3: ['3', '#'],
  7.     digit4: ['4', '$'],
  8.     digit5: ['5', '%'],
  9.     digit6: ['6', '^'],
  10.     digit7: ['7', '&'],
  11.     digit8: ['8', '*'],
  12.     digit9: ['9', '('],
  13.     digit0: ['0', ')'],
  14.     dash: ['-', '_'],
  15.     equal: ['=', '+'],
  16.     backspace: ['Backspace'],
  17.     tab: ['Tab'],
  18.     keyQ: ['q', 'Q'],
  19.     keyW: ['w', 'W'],
  20.     keyE: ['e', 'E'],
  21.     keyR: ['r', 'R'],
  22.     keyT: ['t', 'T'],
  23.     keyY: ['y', 'Y'],
  24.     keyU: ['u', 'U'],
  25.     keyI: ['i', 'I'],
  26.     keyO: ['o', 'O'],
  27.     keyP: ['p', 'P'],
  28.     leftBracket: ['[', '{'],
  29.     rightBracket: [']', '}'],
  30.     backslash: ['\\', '|'],
  31.     del: ['DEL'],
  32.     capslock: ['CapsLock'],
  33.     keyA: ['a', 'A'],
  34.     keyS: ['s', 'S'],
  35.     keyD: ['d', 'D'],
  36.     keyF: ['f', 'F'],
  37.     keyG: ['g', 'G'],
  38.     keyH: ['h', 'H'],
  39.     keyJ: ['j', 'J'],
  40.     keyK: ['k', 'K'],
  41.     keyL: ['l', 'L'],
  42.     semicolon: [';', ':'],
  43.     quote: ['\'', '"'],
  44.     enter: ['ENTER'],
  45.     shiftLeft: ['Shift'],
  46.     keyZ: ['z', 'Z'],
  47.     keyX: ['x', 'X'],
  48.     keyC: ['c', 'C'],
  49.     keyV: ['v', 'V'],
  50.     keyB: ['b', 'B'],
  51.     keyN: ['n', 'N'],
  52.     keyM: ['m', 'M'],
  53.     comma: [',', '<'],
  54.     dot: ['.', '>'],
  55.     slash: ['/', '?'],
  56.     arrowUp: ['↑'],
  57.     shiftRight: ['Shift'],
  58.     controlLeft: ['Ctrl'],
  59.     altLeft: ['Alt'],
  60.     space: [' ', ' '],
  61.     altRight: ['Alt'],
  62.     arrowLeft: ['←'],
  63.     arrowDown: ['↓'],
  64.     arrowRight: ['→'],
  65.     controlRight: ['Ctrl'],
  66.   },
  67.   ru: {
  68.     backquote: ['ё', 'Ё'],
  69.     digit1: ['1', '!'],
  70.     digit2: ['2', '"'],
  71.     digit3: ['3', '№'],
  72.     digit4: ['4', ';'],
  73.     digit5: ['5', '%'],
  74.     digit6: ['6', ':'],
  75.     digit7: ['7', '?'],
  76.     digit8: ['8', '*'],
  77.     digit9: ['9', '('],
  78.     digit0: ['0', ')'],
  79.     dash: ['-', '_'],
  80.     equal: ['=', '+'],
  81.     backspace: ['Backspace'],
  82.     tab: ['Tab'],
  83.     keyQ: ['й', 'Й'],
  84.     keyW: ['ц', 'Ц'],
  85.     keyE: ['у', 'У'],
  86.     keyR: ['к', 'К'],
  87.     keyT: ['е', 'Е'],
  88.     keyY: ['н', 'Н'],
  89.     keyU: ['г', 'Г'],
  90.     keyI: ['ш', 'Ш'],
  91.     keyO: ['щ', 'Щ'],
  92.     keyP: ['з', 'З'],
  93.     leftBracket: ['х', 'Х'],
  94.     rightBracket: ['ъ', 'Ъ'],
  95.     backslash: ['\\', '/'],
  96.     del: ['DEL'],
  97.     capslock: ['CapsLock'],
  98.     keyA: ['ф', 'Ф'],
  99.     keyS: ['ы', 'Ы'],
  100.     keyD: ['в', 'В'],
  101.     keyF: ['а', 'А'],
  102.     keyG: ['п', 'П'],
  103.     keyH: ['р', 'Р'],
  104.     keyJ: ['о', 'О'],
  105.     keyK: ['л', 'Л'],
  106.     keyL: ['д', 'Д'],
  107.     semicolon: ['ж', 'Ж'],
  108.     quote: ['э', 'Э'],
  109.     enter: ['ENTER'],
  110.     shiftLeft: ['Shift'],
  111.     keyZ: ['я', 'Я'],
  112.     keyX: ['ч', 'Ч'],
  113.     keyC: ['с', 'С'],
  114.     keyV: ['м', 'М'],
  115.     keyB: ['и', 'И'],
  116.     keyN: ['т', 'Т'],
  117.     keyM: ['ь', 'Ь'],
  118.     comma: ['б', 'Б'],
  119.     dot: ['ю', 'Ю'],
  120.     slash: ['.', ','],
  121.     arrowUp: ['↑'],
  122.     shiftRight: ['Shift'],
  123.     controlLeft: ['Ctrl'],
  124.     altLeft: ['Alt'],
  125.     space: [' ', ' '],
  126.     altRight: ['Alt'],
  127.     arrowLeft: ['←'],
  128.     arrowDown: ['↓'],
  129.     arrowRight: ['→'],
  130.     controlRight: ['Ctrl'],
  131.   },
  132. };
  133.  
  134. const css = {
  135.   wrapper: 'wrapper',
  136.   textarea: 'textarea',
  137.   keyboard: 'keyboard',
  138.   row: 'row',
  139.   key: 'key',
  140.   backspace: 'backspace',
  141.   tab: 'tab',
  142.   del: 'del',
  143.   capslock: 'capslock',
  144.   enter: 'enter',
  145.   shift: 'shift',
  146.   ctrl: 'ctrl',
  147.   alt: 'alt',
  148.   space: 'space',
  149.   keydown: 'keydown',
  150. };
  151.  
  152. function handleKeydown(event) {
  153.   event.preventDefault();
  154.   const keyElem = document.querySelector(`${event.code}`);
  155.   keyElem.classList.add(css.keydown);
  156.   console.log(event);
  157.   return true;
  158. }
  159.  
  160. function handleKeyup(event) {
  161.   event.preventDefault();
  162.   const keyElem = document.querySelector(`${event.code}`);
  163.   keyElem.classList.remove(css.keydown);
  164.   console.log(event);
  165.   return true;
  166. }
  167.  
  168. function createKey() {
  169.   const key = document.createElement('div');
  170.   key.classList.add(css.key);
  171.   return key;
  172. }
  173.  
  174. function getValue() {
  175.   const engArr = Object.entries(keyboardLayout.en);
  176.   const value = engArr.map((arr) => {
  177.     const [keys, values] = arr;
  178.     const [val1, val2] = values;
  179.     const val = document.createElement('span');
  180.  
  181.     val.setAttribute('class', 'val');
  182.     val.textContent = val1;
  183.  
  184.     const key = createKey();
  185.     key.append(val);
  186.  
  187.     key.classList.add(css.backspace);
  188.     key.classList.add(css.tab);
  189.     key.classList.add(css.del);
  190.     key.classList.add(css.capslock);
  191.     key.classList.add(css.enter);
  192.     key.classList.add(css.shift);
  193.     key.classList.add(css.ctrl);
  194.     key.classList.add(css.alt);
  195.     key.classList.add(css.space);
  196.  
  197.     function specialKeys(keyWord, cssClass) {
  198.       if (val1 !== keyWord) {
  199.         key.classList.remove(cssClass);
  200.       }
  201.       return key;
  202.     }
  203.  
  204.     specialKeys('Backspace', 'backspace');
  205.     specialKeys('Tab', 'tab');
  206.     specialKeys('DEL', 'del');
  207.     specialKeys('CapsLock', 'capslock');
  208.     specialKeys('ENTER', 'enter');
  209.     specialKeys('Shift', 'shift');
  210.     specialKeys('Ctrl', 'ctrl');
  211.     specialKeys('Alt', 'alt');
  212.     specialKeys(' ', 'space');
  213.  
  214.     return key;
  215.   });
  216.  
  217.   return value;
  218. }
  219.  
  220. function createElements() {
  221.   const wrapper = document.createElement('div');
  222.   wrapper.classList.add(css.wrapper);
  223.  
  224.   const textarea = document.createElement('textarea');
  225.   const t = document.createTextNode('');
  226.   textarea.appendChild(t);
  227.   textarea.style = 'resize: none;';
  228.   textarea.setAttribute('cols', 80);
  229.   textarea.setAttribute('rows', 10);
  230.   document.body.appendChild(textarea);
  231.   textarea.classList.add(css.textarea);
  232.  
  233.   const keyboard = document.createElement('div');
  234.   keyboard.classList.add(css.keyboard);
  235.   wrapper.append(textarea, keyboard);
  236.  
  237.   const allKeys = getValue();
  238.   let indexAllKeys = 0;
  239.  
  240.   function createRowsWithKeys(row, keysAmount) {
  241.     row.classList.add(css.row);
  242.     for (let i = 0; i < keysAmount; i += 1, indexAllKeys += 1) {
  243.       row.append(allKeys[indexAllKeys]);
  244.       keyboard.append(row);
  245.     }
  246.     return keyboard;
  247.   }
  248.  
  249.   const row1 = document.createElement('div');
  250.   const row2 = document.createElement('div');
  251.   const row3 = document.createElement('div');
  252.   const row4 = document.createElement('div');
  253.   const row5 = document.createElement('div');
  254.  
  255.   createRowsWithKeys(row1, 14);
  256.   createRowsWithKeys(row2, 15);
  257.   createRowsWithKeys(row3, 13);
  258.   createRowsWithKeys(row4, 13);
  259.   createRowsWithKeys(row5, 8);
  260.  
  261.   return wrapper;
  262. }
  263.  
  264. /*
  265. document.onkeypress = function(event) {
  266.   function addDataToKey() {
  267.     key.setAttribute('data', event.charCode);
  268.     return key;
  269.   }
  270.   document.querySelector('.key').classList.add('active');
  271. } */
  272.  
  273. window.addEventListener('load', () => {
  274.   const pageElements = createElements();
  275.   document.querySelector('body').appendChild(pageElements);
  276.   window.addEventListener('keydown', handleKeydown);
  277.   window.addEventListener('keyup', handleKeyup);
  278. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement