键盘快捷方式在ruby-gtk2 TextView中不起作用

时间:2014-11-08 02:59:37

标签: ruby gtk ruby-gnome2

我正在基于Gtk和Ruby的GUI上练习。我打算制作一个带有TextView和按钮的小窗口。当您在TextView中输入文本并按下按钮时,它会调用Twitter gem的方法并向Twitter发送推文 在成功之后,我想做一个键盘快捷方式来执行此操作:例如,当您按Ctrl + Enter它推文时。
这是我编写的源代码的一部分。 (仅限GUI)

# require "gtk2"
# require "twitter"

## error classes and methods first

class Error_pop
  def initialize(text)
    @pop = Gtk::Dialog.new(
        "Error",
        nil,
        Gtk::Dialog::MODAL,
        [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK]
    )
    @pop.default_response = Gtk::Dialog::RESPONSE_OK
    @pop.set_size_request(400, 100)
    @label = Gtk::Label.new("#{ text }")
    @table = Gtk::Table.new(3, 1, false)
    @table.attach_defaults(@label, 0, 1, 0, 2)
    @pop.vbox.add(@table)
  end
  attr_accessor :pop, :table, :label
end


module Tueet
  def tueet(text, account)
    begin
      account.rest_api.update(text)          # calls gem: twitter
    rescue => reason
      error_pop = Error_pop.new(reason)
      error_pop.pop.show_all
      error_pop.pop.run do |response|
        if response == Gtk::Dialog::RESPONSE_OK
        end
      end
      error_pop.pop.destroy
      return text
    else
      return nil
    end
  end
  module_function :tueet
end


## making gui

window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
window.title = "Tweet Box"
window.set_size_request(300, 150) 
window.resizable = false
window.signal_connect('delete_event') do
  Gtk.main_quit
  false
end

inputter = Gtk::TextView.new
inputter.editable = true
inputter.wrap_mode = Gtk::TextTag::WRAP_WORD_CHAR
inputter_event = Gtk::EventBox.new                  # for key binding
inputter_event.events = Gdk::Event::KEY_PRESS_MASK

tweetbutton = Gtk::Button.new("tweet")

table = Gtk::Table.new(4, 9, true)
table.attach(inputter, 0, 9, 0, 3, Gtk::FILL||Gtk::EXPAND, Gtk::FILL||Gtk::EXPAND, 5, 5)
table.attach(tweetbutton, 3, 6, 3, 4)

inputter_event.add(table)
window.add(inputter_event)

inputter_event.realize


## signals and stuff

text = nil

tweetbutton.signal_connect('clicked') do
  text = inputter.buffer.text
  returned_text = Tueet.tueet(text, client[0])    # client[n] holds the authorized account information
  inputter.buffer.text = String(returned_text)
end

inputter_event.signal_connect('key-press-event') do |wdt, evt|
  key = Gdk::Keyval.to_name(evt.keyval)
  p ("KeyPress: #{ key }was pressed")      # these lines
  p evt.state                              # are for 
  p evt.keyval                             # debugging

  if key == 'Return' &&                    # when "Ctrl + Return", do Tueet.tueet
      ( evt.state & Gdk::Window::CONTROL_MASK ==
          Gdk::Window::ModifierType::CONTROL_MASK )
    Tueet.tueet(text, client[0])
  end
end

window.show_all
Gtk.main

而且,这不起作用。当我按下Ctrl + Enter时,它只是在TextView内部的换行符 仅在执行

inputter = Gtk::TextView.new
inputter.editable = false                         # <- making this false
inputter.wrap_mode = Gtk::TextTag::WRAP_WORD_CHAR

使键绑定工作,但禁止我输入我想要发送到TextView的内容。

我该如何解决这个问题?我想知道有多少GUI应用程序这样做。

0 个答案:

没有答案