Class: Hemi::Event::Pattern::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/hemi/event/pattern/key.rb

Constant Summary collapse

MATCHED_EVENT =
"SDL2::Event::KeyDown".freeze
ALLOWED_TYPES =
%i[up down].freeze
MOD_KEYS =
%i[lshift rshift lctrl rctrl lalt ralt lgui rgui].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, type: :down, mods: nil) ⇒ Key

Returns a new instance of Key.



9
10
11
12
13
14
15
# File 'lib/hemi/event/pattern/key.rb', line 9

def initialize(key, type: :down, mods: nil)
  @type = type.to_s
  @key  = key.to_s
  @mods = mods
  key_to_scancode
  mods_to_modcode
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



17
18
19
# File 'lib/hemi/event/pattern/key.rb', line 17

def key
  @key
end

#modcodeObject (readonly)

Returns the value of attribute modcode.



17
18
19
# File 'lib/hemi/event/pattern/key.rb', line 17

def modcode
  @modcode
end

#modsObject (readonly)

Returns the value of attribute mods.



17
18
19
# File 'lib/hemi/event/pattern/key.rb', line 17

def mods
  @mods
end

#scancodeObject (readonly)

Returns the value of attribute scancode.



17
18
19
# File 'lib/hemi/event/pattern/key.rb', line 17

def scancode
  @scancode
end

#typeObject (readonly)

Returns the value of attribute type.



17
18
19
# File 'lib/hemi/event/pattern/key.rb', line 17

def type
  @type
end

Instance Method Details

#hashObject



19
20
21
# File 'lib/hemi/event/pattern/key.rb', line 19

def hash
  KeyPattern.new(scancode, modcode).hash
end

#inspectObject



23
24
25
# File 'lib/hemi/event/pattern/key.rb', line 23

def inspect
  "#<#{self.class}:#{hash} key=#{key} mods=#{mods.join(', ')}>"
end

#key_to_scancodeObject



27
28
29
# File 'lib/hemi/event/pattern/key.rb', line 27

def key_to_scancode
  @scancode = SDL2::Key::Scan.const_get(key.upcase)
end

#mods_to_modcodeObject



31
32
33
34
35
# File 'lib/hemi/event/pattern/key.rb', line 31

def mods_to_modcode
  @modcode = mods&.sum do |modkey|
    SDL2::Key::Mod.const_get(modkey.upcase)
  end || 0
end