     @dom_handler.has_focus(true)
 
   @key_down: (e) ->
-    if e.shiftKey
-      turns = 3
+    [key, shift, ctrl] = [e.keyCode, e.shiftKey, e.ctrlKey]
+
+    if key == key_tab
+      new_focus = if shift then @active_display.previous_display() else @active_display.next_display()
+      this.set_active_display(new_focus)
+
+    else if key == key_up_arrow
+      @active_display.to_end()
+
+    else if key in button_keys
+      this._fake_click_down(this._button_for(key, shift))
+
+    else if key in rotate_keys
+      axis = switch key
+        when key_C, key_Z then 'up'
+        when key_A, key_D then 'dr'
+        when key_S, key_X then 'dl'
+      turns = switch key
+        when key_C, key_A, key_S then 1
+        when key_Z, key_D, key_X then -1
+      this._rotate(axis, turns)
+
+    else if key in turn_keys
+      turns = if shift then 3 else if ctrl then 2 else 1
+      this._move("#{turn_for[key]}#{turns}")
+
     else
-      turns = if e.ctrlKey then 2 else 1
+      return false
 
-    switch e.keyCode
-      when key_tab
-        new_focus = if e.shiftKey then @active_display.previous_display() else @active_display.next_display()
-        this.set_active_display(new_focus)
-      when key_home, key_down_arrow then this._fake_click_down(@dom_handler.reset)
-      when key_left_arrow  then b = (if e.shiftKey then @dom_handler.reset else @dom_handler.prev); this._fake_click_down(b)
-      when key_right_arrow then b = (if e.shiftKey then @dom_handler.active_play_or_pause else @dom_handler.next); this._fake_click_down(b)
-      when key_space       then this._fake_click_down(@dom_handler.active_play_or_pause)
-      when key_C then this._rotate('up', 1)
-      when key_Z then this._rotate('up',-1)
-      when key_A then this._rotate('dr', 1)
-      when key_D then this._rotate('dr',-1)
-      when key_S then this._rotate('dl', 1)
-      when key_X then this._rotate('dl',-1)
-      when key_J then this._move("U#{turns}")
-      when key_K then this._move("F#{turns}")
-      when key_L then this._move("R#{turns}")
-      else
-        handled = false
-
-    !handled
+  @_button_for: (key, shift) ->
+    switch key
+      when key_home, key_down_arrow
+        @dom_handler.reset
+      when key_left_arrow
+        if shift then @dom_handler.reset else @dom_handler.prev
+      when key_right_arrow
+        if shift then @dom_handler.active_play_or_pause else @dom_handler.next
+      when key_space
+        @dom_handler.active_play_or_pause
 
   @key_up: (e) ->
-    switch e.keyCode
-      when key_home, key_left_arrow, key_right_arrow, key_space
-        if @down_button
-          this._fake_click_up(@down_button)
-          @down_button = null
-      else
-        handled = false
-
-    !handled
+    button_key = e.keyCode in button_keys
+    if button_key
+      if @down_button
+        this._fake_click_up(@down_button)
+        @down_button = null
+    return button_key
 
   @_fake_click_down: (button) ->
     unless button.attr("disabled")

  key_S = 83
  key_X = 88
  key_Z = 90

#= require roofpig/utils

#This is all global data and functions. Think of it as a "singleton" class.
class @InputHandler


  @set_active_display: (new_active) ->
    @dom_handler.has_focus(false) if @active_display
