class TextEditor::Keyboard
Public Instance Methods
aliases()
click to toggle source
# File lib/text_editor/keyboard.rb 6 def aliases 7 @aliases ||= { 8 "\e" => :escape, 9 ctrl_m: :enter, 10 dc: :delete, 11 ic: :insert, 12 npage: :page_down, 13 ppage: :page_up, 14 } 15 end
codes()
click to toggle source
# File lib/text_editor/keyboard.rb 17 def codes 18 @codes ||= { 19 0 => :ctrl_space, 20 9 => :tab, 21 27 => :escape, 22 127 => :backspace, 23 263 => :backspace, 24 353 => [:shift, :tab], 25 527 => [:ctrl, :shift, :end], 26 532 => [:ctrl, :shift, :home], 27 } 28 end
keys()
click to toggle source
# File lib/text_editor/keyboard.rb 30 def keys 31 @keys ||= terminal.keys.transform_keys do |key| 32 key.to_s.downcase.sub("key_", "").to_sym 33 end 34 end
listen()
click to toggle source
# File lib/text_editor/keyboard.rb 36 def listen 37 translate(terminal.input) 38 end
translate(key)
click to toggle source
# File lib/text_editor/keyboard.rb 40 def translate(key) 41 return unless key 42 43 key = codes[key] if codes.key?(key) 44 key = keys.key(key) if keys.value?(key) 45 key = aliases[key] if aliases.key?(key) 46 47 case key 48 when 0..255 then key.chr.to_sym 49 when Array then key 50 else key.to_sym 51 end 52 end