Method: Keyboard#type?
- Defined in:
- lib/keyboard/keyboard.rb
#type?(text, args = {}) ⇒ Boolean
Public: This function is used to write the given text on the virtual keyboard.
text - String or Array of String keys to type. start_key - String non-default start key (default: nil). verify_roi - Roi object to verify the typed text (default: nil). delay_between_keys - Delay used between key presses in miliseconds min value of 3000 (default: 3000). nav_only - Boolean whether to navigate without selecting (default: false).
Used to navigate to a single key without selecting it.
Returns Boolean true if successfully able to write the given text. Else, returns false.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/keyboard/keyboard.rb', line 37 def type?(text, args={}) @key_delay = args[:delay_between_keys].to_i if args[:delay_between_keys].to_i > 3000 nav_only = args.fetch(:nav_only, false) logger.info("#{nav_only ? 'Navigating to' : 'Typing'} #{text.inspect} on the #{cur_layout.name} keyboard.") start_key = args[:start_key] # only verify if verify==true (from `type_keys?') verify_roi = args[:verify] ? args[:verify_roi] || @verify_roi : nil unless start_key.nil? @cur_coords = get_key_coords(start_key).first end unless text.is_a?(Array) text = [text] end first_char = true text.each do |val| if val.is_a?(String) && val.length > 1 # type the string val.split('').each do |key| return false unless type?(key, nav_only: nav_only) end else val = :SPACE if val == ' ' layout_index, shift, key = get_layout_for_key(val) # this is a defined key if layout_index != cur_layout_index # change to the specified layout return false unless type_key?(@layouts[layout_index].nav_to_key) last_row_count = cur_key_matrix.count last_row_width = cur_key_matrix[cur_coords[0]].count @cur_layout_index = layout_index last_nav_to_key = cur_layout.nav_to_key # check if we need to reset the coords due to difference in dimensions between last and new keyboard if cur_key_matrix.count != last_row_count || cur_key_matrix[cur_coords[0]].count != last_row_width @cur_coords = get_key_coords(last_nav_to_key).first end # check if we need to downshift to restore the default case of the new layout shift ||= (@shift_persists && @shifted) end # todo: implement shift lock for efficiency # if this layout auto-cases, this is the first character, and the key is uppercase, then shift is not req'd no_shift_reqd = cur_layout.auto_case && first_char && (key == key.upcase) if shift && !no_shift_reqd # press the shift key return false unless type_key?(cur_layout.shift_key) end if nav_only return false unless nav_to_key?(key) else return false unless type_key?(key) end first_char = false if first_char first_char = true if cur_layout.auto_case && key == :SPACE end end # all text typed successfully if verify_roi.nil? true else typed = verify_roi.retrieve.sub(/_$/, '') if typed == text true else logger.info(%Q(Tried to type "#{text}" but typed "#{typed}" instead!)) false end end end |