当用户在窗口中时,我可以连接到哪个事件以检测按下的箭头键。
到目前为止,我已尝试通过on_key_press_event
进行连接,并检查了keyval
,hardware_keycode
和state
。
#include <gtkmm.h>
#include <iostream>
class MyWindow : public Gtk::Window
{
public:
MyWindow();
bool onKeyPress(GdkEventKey*);
};
MyWindow::MyWindow()
{
set_title("arrow_button_test");
this->signal_key_press_event().connect( sigc::mem_fun( *this, &MyWindow::onKeyPress ) );
}
bool MyWindow::onKeyPress(GdkEventKey* event)
{
std::cout << event->keyval << ' ' << event->hardware_keycode << ' ' << event->state << std::endl;
return false;
}
int main(int argc, char** argv)
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "com.almost-university.gtkmm.arrow_button_press");
MyWindow window;
app->run(window);
return 0;
}
此代码不会在箭头键上生成任何输出,这意味着事件甚至不会被触发。
答案 0 :(得分:2)
如果你改变了
RecipeTitleButton
到
this->signal_key_press_event().connect(
sigc::mem_fun(*this, &MyWindow::onKeyPress));
您的信号处理程序应该接收事件。 this->signal_key_press_event().connect(
sigc::mem_fun(*this, &MyWindow::onKeyPress), false);
参数用于false
标志。默认情况下为after
,这意味着其他信号处理程序可能会在true
之前拦截信号,因为它是最后一个。