   onKeypress: (event) ->
     keyChar = KeyboardUtils.getKeyCharString event
     @unlessKeyCharIsPassKey keyChar, =>
-      if keyChar and @keyCharIsMapped keyChar
+      if @keyCharIsMapped keyChar
         @handleKeyChar event, keyChar
       else if @isCountKey keyChar
         digit = parseInt keyChar


  onKeydown: (event) ->
    keyChar = KeyboardUtils.getKeyCharString event
    if KeyboardUtils.isEscape event
      if @countPrefix == 0 and @keyState.length == 1
        @continueBubbling
      else
        @keydownEvents[event.keyCode] = true
        @reset()
        false # Suppress event.
    else if keyChar and @keyCharIsMapped keyChar
      @unlessKeyCharIsPassKey keyChar, =>
        @keydownEvents[event.keyCode] = true
        @handleKeyChar event, keyChar
    else if keyChar
      @continueBubbling
    else if (keyChar = KeyboardUtils.getKeyChar event) and (@keyCharIsMapped(keyChar) or @isCountKey keyChar)
      # It looks like we will be handling a subsequent keypress event, so suppress propagation of this event
      # to prevent triggering page event listeners (e.g. Google instant Search).
      @unlessKeyCharIsPassKey keyChar, =>
        @keydownEvents[event.keyCode] = true
        DomUtils.suppressPropagation event

# Example key mapping (@keyMapping):
#   i:
#     command: "enterInsertMode", ... # This is a registryEntry object (as too are the other commands).
#   g:
#     g:
#       command: "scrollToTop", ...
#     t:
#       command: "nextTab", ...
#
# This key-mapping structure is generated by Commands.generateKeyStateMapping() and may be arbitrarily deep.
# Observe that @keyMapping["g"] is itself also a valid key mapping.  At any point, the key state (@keyState)
# consists of a (non-empty) list of such mappings.

class KeyHandlerMode extends Mode
  keydownEvents: {}
  setKeyMapping: (@keyMapping) -> @reset()
  setPassKeys: (@passKeys) -> @reset()

  # Reset the key state, optionally retaining the count provided.
  reset: (@countPrefix = 0) ->
    bgLog "Clearing key state, set count=#{@countPrefix}."
    @keyState = [@keyMapping]

  constructor: (options) ->
    @commandHandler = options.commandHandler ? (->)
