class Gtk2KeyPress123

Public Class Methods

new(timeout: 0.8, notifier: nil, exit_on_success: false, keywords: nil, terminator: 'qq', debug: false) click to toggle source
Calls superclass method
# File lib/gtk2keypress123.rb, line 13
def initialize(timeout: 0.8, notifier: nil, exit_on_success: false, 
                keywords: nil, terminator: 'qq', debug: false)

  @terminator, @debug = terminator, debug

  @h = if keywords then
  
    puts 'keywords : ' + keywords.inspect if @debug
    
    obj, _ = RXFHelper.read keywords, debug: debug, auto: true
    
    puts 'obj: ' + obj.inspect if @debug
    
    if obj and obj.respond_to? :to_h then
      obj.to_h 
    else
      {}
    end
  end
  
  puts '@h: ' + @h.inspect if @debug
  
  @buffer = []
  @timeout, @notifier, @exit = timeout, notifier, exit_on_success

  window = Gtk::Window.new 'Gtk2KeyPress123'

  window.add(Gtk::Label.new("Press 1 or more keys!"))

  key = super(window)
  window.set_default_size(300, 120).show_all

  window.signal_connect( "destroy" ) {
     destroy( nil )
  }

end

Public Instance Methods

on_keypress(e) click to toggle source
# File lib/gtk2keypress123.rb, line 51
def on_keypress(e)

  t = Time.now
  puts 'key press ' + e.name if @debug
  @buffer ||= []
  @buffer << e.name

  Thread.new do

    # after the time has elapsed the message will be sent unless
    # the buffer is empty

    while t + @timeout > Time.now do        
      sleep 0.2
    end

    if @buffer.length > 0 then

      puts ('buffer: ' + @buffer.inspect).debug if @debug
      destroy(nil) if @h and @terminator == @buffer.join

      if @h and @notifier then
        s = @buffer.all? {|x| x.length  < 2} ? '' : ', '          
        puts ('@h: ' + @h.inspect).debug if @debug
        fqm = @h[@buffer.join(s)]
        puts ('fqm: ' + fqm.strip.inspect).debug if @debug
        @notifier.notice fqm.strip if fqm
        #sleep 0.4
      end

      @buffer = []

      if @exit then
        puts 'bye'
        sleep 0.4
        exit
      end
    end

  end

end
start() click to toggle source
# File lib/gtk2keypress123.rb, line 94
def start
  Gtk.main
end

Private Instance Methods

destroy( widget) click to toggle source
# File lib/gtk2keypress123.rb, line 100
def destroy( widget)
   Gtk.main_quit
end