0

I am studying keyboard events from the docs and came across this method

Updated

Here is the simplified version of the code in a signle file with the same error but now it show extra errors

/home/test/Desktop/not_working/main.cpp:15:52: error: ‘GdkEventKey’ has not been declared
   15 |                 bool on_key_press_or_release_event(GdkEventKey * event){
      |                                                    ^~~~~~~~~~~
/home/test/Desktop/not_working/main.cpp: In constructor ‘App::App()’:
/home/test/Desktop/not_working/main.cpp:13:13: error: ‘class Gtk::Box’ has no member named ‘signal_key_press_event’
   13 |         box.signal_key_press_event().connect(sigc::mem_fun(*this, &App::on_key_press_or_release_event));
      |             ^~~~~~~~~~~~~~~~~~~~~~
/home/test/Desktop/not_working/main.cpp: In member function ‘bool App::on_key_press_or_release_event(int*)’:
/home/test/Desktop/not_working/main.cpp:16:38: error: request for member ‘type’ in ‘* event’, which is of non-class type ‘int’
   16 |                           if (event->type == GDK_KEY_PRESS && event->keyval == GDK_KEY_Return)
      |                                      ^~~~
/home/test/Desktop/not_working/main.cpp:16:70: error: request for member ‘keyval’ in ‘* event’, which is of non-class type ‘int’
   16 |                           if (event->type == GDK_KEY_PRESS && event->keyval == GDK_KEY_Return)
      |     

main.cpp

#include <gtkmm.h>
#include <stdio.h>

class App: public Gtk::Window{
    protected:
        Gtk::Box box;
        Gtk::Text text;
    public:
        App(){
                set_child(box);
                box.append(text);
                text.set_text("hello world");
                //box.signal_key_press_event().connect( sigc::ptr_fun(&on_key_press_or_release_event) );
        }
        bool on_key_press_or_release_event(GdkEventKey * event){
              if (event->type == GDK_KEY_PRESS && event->keyval == GDK_KEY_Return)
              {
                printf("Hello there"); 
                return true;
              }
              return false;
        }
};

int main(int argc, char * argv[])
{
    auto app = Gtk::Application::create("org.example.github.basic");
    app->make_window_and_run<App>(argc, argv);
    return 0;
}

and I am using cmake/make to compile it

cmake_minimum_required(VERSION 3.0)
project(example)
find_package(PkgConfig)
pkg_check_modules(GTKMM REQUIRED gtkmm-4.0)
add_executable(app main.cpp)
target_include_directories(app PRIVATE ${GTKMM_INCLUDE_DIRS})
target_compile_options(app PRIVATE ${GTKMM_CFLAGS_OTHER})
target_link_libraries(app PRIVATE ${GTKMM_LIBRARIES})

why does this code state that the GdkEventKey isn't declared anywhere?

misha
  • 31
  • 5
  • Missing header file include or version mismatch. They seem like the likeliest scenarios. Your code above is both incomplete and contains code not needed to diagnose the problem. Please work on producing a [mre]. You could demonstrate this issue, with a single line of code, plus `#includes`, plus `main` function. – john Jun 28 '23 at 11:12
  • @john kindly check the updated question – misha Jun 28 '23 at 12:43
  • 1
    I don't know the answer but I did find [this SO question](https://stackoverflow.com/questions/73505424/gtkmm-4-0-get-key-event-from-gtkentry) with exactly the same problem. That question didn't get any answers but the questioner seems to think it some kind of confusion between `GdkEventKey` and `GdkKeyEvent` or maybe some kind of versioning issue. – john Jun 28 '23 at 16:36

0 Answers0