'use strict'

const ui = require('../lib/ui')
const ipc = require('electron').ipcRenderer;

function getFilePath() {
  if (ui.getCurrent() != undefined) {
    return ui.getCurrent().getAttribute('href')
  }

  return undefined;
}

function isFocusInputField() {
  return document.activeElement.tagName.toLowerCase() == "input"
}


function execCommands(data) {
  switch (data.code) {
    // Focus Search
    case "KeyF":
      if (isFocusInputField()) {
        ui.searchInputForm.blur();
      } else {
        ui.searchInputForm.focus();
      }
      break;
    // Change layout to grid
    case "KeyG":
      ipc.send('changeLayout', data);
      break;
    default:
  }
}

var previous = undefined
document.addEventListener("keydown", (event) => {
  var data = {
    code: event.code,
    path: ui.directoryPath.getAttribute('href'),
    filePath: getFilePath()
  };

  if (["MetaLeft", "MetaRight"].includes(previous)) {
    execCommands(data);
  }

  if (isFocusInputField()) {
    previous = event.code
    ipc.send('keydown', data);
    return;
  }

  switch (event.code) {
    case "Backspace":
      ipc.send('moveToTrash', data);
      break;
    default:
  }
  previous = event.code
  ipc.send('keydown', data);
})

@@ -323,17 +323,16 @@
 put%22%0A%7D%0A%0A
-%0A
 function
@@ -678,28 +678,272 @@
 var 
-previous = undefined
+isControlCommandKey = false;%0Afunction handleCommand(code, key) %7B%0A  var is = %5B%22MetaLeft%22, %22MetaRight%22%5D.includes(code);%0A  if (!is) return;%0A%0A  isControlCommandKey = key != %22up%22;%0A%7D%0A%0Adocument.addEventListener(%22keyup%22, (event) =%3E %7B%0A  handleCommand(event.code, %22up%22);%0A%7D)%0A
 %0Adoc
@@ -989,16 +989,53 @@
 t) =%3E %7B%0A
+  handleCommand(event.code, %22down%22);%0A
   var da
@@ -1156,52 +1156,27 @@
 if (
-%5B%22MetaLeft%22, %22MetaRight%22%5D.includes(previous)
+isControlCommandKey
 ) %7B%0A
@@ -1206,24 +1206,25 @@
 %0A  %7D%0A%0A  if (
+!
 isFocusInput
@@ -1240,80 +1240,8 @@
 %7B%0A  
-  previous = event.code%0A    ipc.send('keydown', data);%0A    return;%0A  %7D%0A%0A
   sw
@@ -1260,24 +1260,26 @@
 code) %7B%0A    
+  
 case %22Backsp
@@ -1284,16 +1284,18 @@
 space%22:%0A
+  
       ip
@@ -1321,39 +1321,43 @@
 ', data);%0A      
+  
 break;%0A
+  
     default:%0A  %7D
@@ -1359,33 +1359,16 @@
 :%0A  
+ 
+ 
 %7D%0A  
-previous = event.code
+%7D%0A
 %0A  i

Handle key up/down to control commands