Sha256: 1ddecd565cb27bee04a9b551e1b6b5bfb4150dc182771079c5e8af2ca29bb030

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require_relative './device.rb'

module Fusuma
  module Plugin
    module Sendkey
      # Emulate Keyboard
      class Keyboard
        def initialize(device: nil)
          @device = device || Device.new
        end

        # @param param [String]
        def type_command(param:)
          return unless param.is_a?(String)

          codes = param.split('+')
          press_commands = codes.map { |code| press_command(code) }
          release_commands = codes.reverse.map { |code| release_command(code) }

          (press_commands | release_commands).join(' && ')
        end

        def press_command(code)
          return unless support?(code)

          @device.emulate(code: code, press: true)
        end

        def release_command(code)
          return unless support?(code)

          @device.emulate(code: code, press: false)
        end

        def support?(code)
          @supported_code ||= {}
          @supported_code[code] ||= if @device.support?(code)
                                      true
                                    else
                                      warn "sendkey: #{code} is unsupported."
                                      warn 'Please check your config.yml.'
                                      exit 1
                                    end
        end

        def available_codes
          @device.search_codes
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fusuma-plugin-sendkey-0.2.1 lib/fusuma/plugin/sendkey/keyboard.rb
fusuma-plugin-sendkey-0.2.0 lib/fusuma/plugin/sendkey/keyboard.rb
fusuma-plugin-sendkey-0.1.1 lib/fusuma/plugin/sendkey/keyboard.rb