Class: Dommy::KeyboardEvent

Inherits:
UIEvent show all
Defined in:
lib/dommy/event.rb

Constant Summary collapse

LEGACY_KEY_CODES =

Legacy keyCode values for non-printable keys (UI Events "keyCode" informative mapping) — jQuery-era code still branches on these.

{
  "Backspace" => 8, "Tab" => 9, "Enter" => 13, "Shift" => 16,
  "Control" => 17, "Alt" => 18, "CapsLock" => 20, "Escape" => 27,
  " " => 32, "PageUp" => 33, "PageDown" => 34, "End" => 35, "Home" => 36,
  "ArrowLeft" => 37, "ArrowUp" => 38, "ArrowRight" => 39, "ArrowDown" => 40,
  "Delete" => 46, "Meta" => 91,
}.freeze

Constants inherited from Event

Event::AT_TARGET, Event::BUBBLING_PHASE, Event::CAPTURING_PHASE, Event::NONE

Instance Attribute Summary

Attributes inherited from Event

#type

Instance Method Summary collapse

Methods inherited from Event

#__internal_clear_propagation_flags__, #__internal_mark_trusted__, #__internal_prepare_for_dispatch__, #__internal_record_path__, #__internal_run_passive__, #__internal_set_current_target__, #__internal_set_dispatch_flag__, #__internal_set_event_phase__, #__js_set__, #bubbles?, #default_prevented?, #immediate_propagation_stopped?, #init_event, #propagation_stopped?

Methods included from Bridge::Methods

included

Constructor Details

#initialize(type, init = nil) ⇒ KeyboardEvent

Returns a new instance of KeyboardEvent.



924
925
926
927
928
929
930
931
932
933
934
935
936
937
# File 'lib/dommy/event.rb', line 924

def initialize(type, init = nil)
  super
  @key = read_init(init, "key").to_s
  @code = read_init(init, "code").to_s
  @ctrl_key = !!read_init(init, "ctrlKey")
  @shift_key = !!read_init(init, "shiftKey")
  @alt_key = !!read_init(init, "altKey")
  @meta_key = !!read_init(init, "metaKey")
  @repeat = !!read_init(init, "repeat")
  @location = (read_init(init, "location") || 0).to_i
  @is_composing = !!read_init(init, "isComposing")
  @key_code = read_init(init, "keyCode")
  @char_code = read_init(init, "charCode")
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/dommy/event.rb', line 990

def __js_call__(method, args)
  case method
  when "getModifierState"
    case (args || []).first.to_s
    when "Shift" then @shift_key
    when "Control" then @ctrl_key
    when "Alt" then @alt_key
    when "Meta" then @meta_key
    else false
    end
  when "initKeyboardEvent"
    # Legacy initKeyboardEvent(type, bubbles, cancelable, view, key, location,
    # modifiersList, repeat, locale). A no-op while dispatching.
    raise Bridge::TypeError, "initKeyboardEvent requires a type argument" if args.empty?

    unless @dispatch_flag
      init_event(args[0], args[1], args[2])
      @view = args[3]
      @key = args[4].to_s
      @location = (args[5] || 0).to_i
      @repeat = !!args[7]
    end
    nil
  else
    super
  end
end

#__js_get__(key) ⇒ Object



959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'lib/dommy/event.rb', line 959

def __js_get__(key)
  case key
  when "key"
    @key
  when "code"
    @code
  when "ctrlKey"
    @ctrl_key
  when "shiftKey"
    @shift_key
  when "altKey"
    @alt_key
  when "metaKey"
    @meta_key
  when "repeat"
    @repeat
  when "location"
    @location
  when "isComposing"
    @is_composing
  when "keyCode"
    key_code
  when "charCode"
    char_code
  when "which"
    key_code.zero? ? char_code : key_code
  else
    super
  end
end

#char_codeObject

Legacy charCode: nonzero only on a printable keypress.



953
954
955
956
957
# File 'lib/dommy/event.rb', line 953

def char_code
  return @char_code.to_i if @char_code

  type == "keypress" && printable_key? ? @key.ord : 0
end

#key_codeObject

Legacy keyCode: explicit init wins; a printable keypress reports the character code (Chrome parity), a printable keydown/keyup the upcased character; named keys use the legacy table.



942
943
944
945
946
947
948
949
950
# File 'lib/dommy/event.rb', line 942

def key_code
  return @key_code.to_i if @key_code

  if printable_key?
    type == "keypress" ? @key.ord : @key.upcase(:ascii).ord
  else
    LEGACY_KEY_CODES[@key] || 0
  end
end