Sha256: 7cb6c2bcc21a137859c93c5ae4d0aed5aa76257fc62e932d82a942cd90f491b6

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module Browser; module DOM; class Event

class Keyboard < UI
  def self.supported?
    not $$[:KeyboardEvent].nil?
  end

  class Definition < UI::Definition
    def alt!
      `#@native.altKey = true`
    end

    def ctrl!
      `#@native.ctrlKey = true`
    end

    def meta!
      `#@native.metaKey = true`
    end

    def shift!
      `#@native.shiftKey = true`
    end

    def code=(code)
      `#@native.keyCode = #@native.which = #{code}`
    end

    def key=(key)
      `#@native.key = #{key}`
    end

    def char=(char)
      `#@native.char = #@native.charCode = #{char}`
    end

    def repeat!
      `#@native.repeat = true`
    end
  end

  def self.create(name, &block)
    new(`new KeyboardEvent(#{name}, #{Definition.new(&block)})`)
  end

  def alt?
    `#@native.altKey`
  end

  def ctrl?
    `#@native.ctrlKey`
  end

  def meta?
    `#@native.metaKey`
  end

  def shift?
    `#@native.shiftKey`
  end

  def repeat?
    `#@native.repeat`
  end

  def key
    `#@native.key || #@native.keyIdentifier || nil`
  end

  def code
    `#@native.keyCode || #@native.which || nil`
  end

  def char
    `#@native.char || #@native.charCode || #{code ? code.chr : nil}`
  end

  alias to_i key

  def down?
    name.downcase == 'keydown'
  end

  def press?
    name.downcase == 'keypress'
  end

  def up?
    name.downcase == 'keyup'
  end
end

end; end; end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
opal-browser-0.1.0.beta1 opal/browser/dom/event/keyboard.rb