【问题标题】:gtkmm: How to detect arrow key is pressedgtkmm:如何检测箭头键被按下
【发布时间】:2016-11-11 07:45:23
【问题描述】:

当用户在窗口中时,我可以连接到什么事件来检测箭头键被按下。

到目前为止,我已经尝试通过on_key_press_event 进行连接,并且我检查了keyvalhardware_keycodestate

#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;
}

此代码在箭头键上不生成任何输出,这意味着该事件甚至没有被触发。

【问题讨论】:

  • 投反对票有什么意义?在租赁评论并说出我应该做些什么来改善这个问题。我进行了搜索,之前没有问过这样的问题,而且从阅读文档中不清楚。我还提供了代码和我的推理。

标签: c++ keypress gtkmm


【解决方案1】:

如果你改变了

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,这意味着其他信号处理程序可能会在MyWindow::onKeyPress之前拦截信号,因为它是最后一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 2014-09-03
    • 2021-12-15
    • 2022-01-12
    相关资源
    最近更新 更多