 retro = document.createElement 'canvas', 'x-game'
 document.body.appendChild retro
 
+onkey = (event) ->
+  if retro.player and settings.keys.hasOwnProperty event.which
+    pressed = event.type == 'keydown'
+    retro.player.inputs[0].buttons[settings.keys[event.which]] ?= {}
+    retro.player.inputs[0].buttons[settings.keys[event.which]].pressed = pressed
+    event.preventDefault()
+
+autosaver = 0
+
+stop = ->
+  retro.stop()
+  # retro.core.deinit()
+  window.removeEventListener 'keyup', onkey
+  window.removeEventListener 'keydown', onkey
+  window.clearInterval autosaver
+
 play = (rom, extension) ->
   retro.md5 = sparkmd5.ArrayBuffer.hash rom
   Promise.all([
     System.import settings.extensions[extension]
     localForage.getItem retro.md5
   ]).then ([core, save]) ->
+    stop() if retro.running
     retro.core = core
     retro.game = rom if rom
     retro.save = new Uint8Array save if save?

  reader.readAsArrayBuffer file

window.addEventListener 'drop', (event) ->
  event.preventDefault()
  draghint.classList.remove 'hover'
  if event.dataTransfer.files.length > 0

        break
  else if settings.extensions[extension]
    rom = buffer
  play rom, extension
  .catch (e) ->
    console.error e
    alert "that file couldn't be loaded"
    draghint.classList.remove 'hidden'

load = (file) ->
