Class: KeyboardLayout

Inherits:
Object show all
Defined in:
lib/keyboard/keyboard_layout.rb

Overview

This is a basic keyboard layout.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_matrix, start_coords, name: 'default', nav_to_key: nil, auto_case: false) ⇒ KeyboardLayout

Public: Create a new keyboard layout.

key_matrix - Multi-dimensional Array of Strings defining the keyboard layout. start_coords - Array of Integer start coordinates for this layout in [row, col]. name - String name of keyboard layout. nav_to_key - String key used to navigate to this layout (default: nil). auto_case - Boolean indicating whether the keyboard automatically capitalizes words (default: false).

Returns the new KeyboardLayout.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/keyboard/keyboard_layout.rb', line 14

def initialize(key_matrix, start_coords, name: 'default', nav_to_key: nil, auto_case: false)
  @key_matrix = key_matrix
  @start_coords = start_coords
  @name = name
  @nav_to_key = nav_to_key
  @auto_case = auto_case
  @shift_key = nil
  all_keys = key_matrix.flatten
  [:SHIFT_LOCK, :SHIFT].each do |shift_key|
    if all_keys.include?(shift_key)
      @shift_key = shift_key
      break
    end
  end
end

Instance Attribute Details

#auto_caseObject

Returns the value of attribute auto_case.



3
4
5
# File 'lib/keyboard/keyboard_layout.rb', line 3

def auto_case
  @auto_case
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/keyboard/keyboard_layout.rb', line 3

def name
  @name
end

Returns the value of attribute nav_to_key.



3
4
5
# File 'lib/keyboard/keyboard_layout.rb', line 3

def nav_to_key
  @nav_to_key
end

#shift_keyObject

Returns the value of attribute shift_key.



3
4
5
# File 'lib/keyboard/keyboard_layout.rb', line 3

def shift_key
  @shift_key
end

#start_coordsObject

Returns the value of attribute start_coords.



3
4
5
# File 'lib/keyboard/keyboard_layout.rb', line 3

def start_coords
  @start_coords
end

Instance Method Details

#key_matrix(shifted) ⇒ Object

Public: Gets key matrix of the current layout.

shifted - Boolean indicating whether the keyboard case is shifted.

Returns the key matrix.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/keyboard/keyboard_layout.rb', line 35

def key_matrix(shifted)
  if shifted
    @key_matrix.map do |row|
      row.map do |key|
        if key.is_a?(String)
          key =~ /^[[:upper:]]+$/ ? key.downcase : key.upcase
        else
          key
        end
      end
    end
  else
    @key_matrix
  end
end