Advertisement
Guest User

Untitled

a guest
Apr 11th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const KEYS = [
  2. [
  3.     "`1234567890-=",
  4.     "qwertyuiop[]\\",
  5.     "asdfghjkl;'",
  6.     "zxcvbnm,./",
  7.     "Shift#1,Alt#1,Space#5,Bksp#1,Enter#1"
  8. ],
  9. [
  10.     "~!@#$%^&*()_+",
  11.     "QWERTYUIOP{}|",
  12.     "ASDFGHJKL:'",
  13.     "ZXCVBNM<>?"
  14. ],
  15. [
  16.     "`1234567890-=",
  17.     "qwęrtyuióp[]\\",
  18.     "ąśdfghjkl;'",
  19.     "żźćvbńm,./"
  20. ],
  21. [
  22.     "~!@#$%^&*()_+",
  23.     "QWĘRTYUIÓP{}|",
  24.     "ĄŚDFGHJKL:'",
  25.     "ŻŹĆVBŃM<>?"
  26. ]
  27. ];
  28.  
  29. class Keyboard {
  30.  
  31.     constructor() {
  32.         this.scr = null;
  33.         this.mode = 0;
  34.         this.shiftMode = 0;
  35.         this.altMode = 0;
  36.         this.keys = new Array(5).fill(0).map(() => document.createElement('div'));
  37.         this.keys.forEach((div, row) => {
  38.            if(row != 4) for(let i = 0; i < KEYS[0][row].length; ++i) {
  39.                 const btn = document.createElement('div');
  40.                 btn.className = 'button';
  41.                 btn.textContent = KEYS[0][row][i];
  42.                 btn.addEventListener('click', e => {
  43.                     this.scr.textContent += e.target.textContent;
  44.                     if(this.shiftMode === 1) {
  45.                         this.shiftMode = 0;
  46.                         this.mode = 0;
  47.                         this.changeLayout();
  48.                     }
  49.                      if(this.altMode === 1) {
  50.                         this.altMode = 0;
  51.                         this.mode = 0;
  52.                         this.changeLayout2();
  53.                     }
  54.                     if(this.altMode === 2 && this.shiftMode === 2) {
  55.                              this.mode = 0;
  56.                             this.changeLayout3();
  57.                         }
  58.                         if(this.altMode === 1 && this.shiftMode === 1) {
  59.                             this.mode = 0;
  60.                             this.changeLayout3();
  61.                         }
  62.                         if(this.altMode === 2 && this.shiftMode === 1) {
  63.                             this.mode = 2;
  64.                             this.changeLayout3();
  65.                         }
  66.                         if(this.altMode === 1 && this.shiftMode === 2) {
  67.                             this.altMode = 1;
  68.                             this.shiftMode = 2;
  69.                             this.changeLayout3();
  70.                         }
  71.                         if(this.altMode === 0 && this.shiftMode === 2) {
  72.                             this.mode = 1;
  73.                             this.changeLayout2();
  74.                         }
  75.                         if(this.altMode === 2 && this.shiftMode === 0) {
  76.                             this.mode = 2;
  77.                             this.changeLayout2();
  78.                         }
  79.                  });
  80.                 div.appendChild(btn);
  81.             }
  82.             else {
  83.                 const keys = KEYS[0][row].split(',').map(k => k.split('#'));
  84.                 keys.forEach(key => {
  85.                     const btn = document.createElement('div');
  86.                     btn.className = 'button';
  87.                     btn.textContent = key[0];
  88.                     btn.style.flexGrow = key[1];
  89.                     btn.addEventListener('click', e => {
  90.                         const type = e.target.textContent.toLowerCase();
  91.  
  92.                         switch(type) {
  93.                             case 'space':
  94.                             this.scr.textContent += ' ';
  95.                             break;
  96.  
  97.                             case 'enter':
  98.                             this.scr.textContent += '\n';
  99.                             break;
  100.  
  101.                             case 'bksp':
  102.                             document.getElementById("gora").textContent = document.getElementById("gora").textContent.slice(0, -1);
  103.                             break;
  104.  
  105.                             case 'shift':
  106.                             this.shiftMode = ++this.shiftMode % 3;
  107.                             this.mode = this.shiftMode > 0 ? 1 : 0;
  108.                             this.keys[4].childNodes[0].className = this.shiftMode === 2 ? "button lock" : "button";
  109.                             this.changeLayout();
  110.                             break;
  111.  
  112.                             case 'alt':
  113.                             this.altMode = ++this.altMode % 3;
  114.                             this.mode = this.altMode > 0 ? 2 : 0;
  115.                             this.keys[4].childNodes[1].className = this.altMode === 2 ? "button lock" : "button";
  116.                             this.changeLayout2();
  117.                             break;
  118.  
  119.                         }
  120.                         if(this.shiftMode === 1 && type !== 'shift') {
  121.                             this.mode = 0;
  122.                             this.changeLayout();
  123.                         }
  124.                         if(this.altMode === 1 && type !== 'alt') {
  125.                             this.mode = 0;
  126.                             this.changeLayout2();
  127.                         }
  128.                         if(this.altMode === 2 && this.shiftMode === 2) {
  129.                              this.mode = 0;
  130.                             this.changeLayout3();
  131.                         }
  132.                         if(this.altMode === 1 && this.shiftMode === 1) {
  133.                             this.mode = 0;
  134.                             this.changeLayout3();
  135.                         }
  136.                         if(this.altMode === 2 && this.shiftMode === 1) {
  137.                             this.mode = 2;
  138.                             this.changeLayout3();
  139.                         }
  140.                         if(this.altMode === 1 && this.shiftMode === 2) {
  141.                             this.altMode = 1;
  142.                             this.shiftMode = 2;
  143.                             this.changeLayout3();
  144.                         }
  145.                         if(this.altMode === 0 && this.shiftMode === 2) {
  146.                             this.mode = 1;
  147.                             this.changeLayout2();
  148.                         }
  149.                         if(this.altMode === 2 && this.shiftMode === 0) {
  150.                             this.mode = 2;
  151.                             this.changeLayout2();
  152.                         }
  153.                      });
  154.                     div.appendChild(btn);
  155.                 });
  156.             }
  157.         });
  158.     }
  159.  
  160.     changeLayout() {
  161.         const keys = KEYS[this.mode];
  162.             for(let r=0; r < 4; ++r) {
  163.                 this.keys[r].childNodes
  164.                 .forEach((key, i) => { key.textContent = keys[r][i];});
  165.             }
  166.     }
  167.     changeLayout2() {
  168.         const keys = KEYS[this.mode];
  169.             for(let r=0; r < 4; ++r) {
  170.                 this.keys[r].childNodes
  171.                 .forEach((key, i) => { key.textContent = keys[r][i]});
  172.             }
  173.     }
  174.     changeLayout3() {
  175.         const keys = KEYS[3];
  176.             for(let r=0; r < 4; ++r) {
  177.                 this.keys[r].childNodes
  178.                 .forEach((key, i) => { key.textContent = keys[r][i]});
  179.             }
  180.     }
  181.    
  182.     init() {
  183.         this.scr = document.getElementById('gora');
  184.         const cont = document.getElementById('klawiatura');
  185.         this.keys.forEach(div => cont.appendChild(div));
  186.     }
  187.  
  188. }
  189.  
  190. const klawiatura = new Keyboard();
  191.  
  192. window.addEventListener('DOMContentLoaded', () => klawiatura.init());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement