Question

I'm a beginner of both gtk and GtkD.
Now, I'm trying to get input from keyboard with reference to this .
But, It seems that three years have made some changes in Toolkits.
I wrote code below. However, I got strange values of ev in callback function.

I could not see any prospect of resolution with going alone.
So, could you show me where to modify?

I appreciate you in advance, and also your patient with my poor English.


I'm using gtkD-2.1.1 and gtk+3.2.3.
this is the full code:

    import std.stdio;  
    import gtkc.gdktypes;  
    import gtk.MainWindow;  
    import gtk.Widget;  
    import gdk.Event;  
    import gtk.Main;  

    class Window : MainWindow{  
    immutable width = 200;  
    immutable height = 200;   
        this(){    
            super("input test");   
            setDefaultSize(width,height);  
            setEvents(EventMask.KEY_PRESS_MASK);  // Actually I don't know how this works

            auto callback_func = cast(bool delegate(Event,Widget))&get_key; // I doubt this cast 
            this.addOnKeyPress(callback_func);

            showAll();
        }
        bool get_key(GdkEventKey* ev, Widget widget){
            writefln("sender %s", widget);

            writefln("type %x",ev.type);
            writefln("window* %x",ev.window);
            writefln("send_event %x",ev.sendEvent);
            writefln("time %x",ev.time);
            writefln("state %x",ev.state);
            writefln("keyval %x",ev.keyval);
            writefln("length %x",ev.length);
            writefln("gchar* %x",ev.string);
            writefln("hardware_keycode %x",ev.hardwareKeycode);
            writefln("group %x",ev.group);
            writefln("is_modifier %x\n",ev.bitfield0);

            return true;
        }
    }

    void main(string[] args){  
        Main.init(args);  
        auto win = new Window();  
        Main.run();  
    }
Was it helpful?

Solution

Yes, that cast is wrong. I guess that Signature with GdkEventKey* is outdated. Change your get_key to take an Event and you should get proper results:

    ...
    auto call = &get_key;
    ...
    bool get_key(Event e, Widget widget){
        GdkEventKey* ev = e.key();
    ...

I have never done anything with GtkD, and this is just the result of some glances over the docs. So, it's probably not best practice, but it should get you back on the road.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top