[erlang-questions] Capturing keystrokes in wxWindows Issues + Segmentation Fault

Dan Gudmundsson dangud@REDACTED
Fri May 4 09:37:14 CEST 2012


 How about this:

    Wx = wx:new(),
    Frame = wxFrame:new(Wx, ?wxID_ANY, "Press any key"),
    Panel = wxPanel:new(Frame, []),
    wxFrame:connect(Frame, enter_window, [{callback, fun(_,_) ->
							     io:format("Set focus~n"),
							     wxWindow:setFocus(Panel)
						     end}]),
    KeyEvent = fun(Ev,Obj) -> io:format("Got ~p~n",[Ev]), wxEvent:skip(Obj) end,
    [wxWindow:connect(Panel, Types, [{callback,KeyEvent}])
     || Types <- [key_down, key_up, char]],
    wxWindow:connect(Frame, char_hook, [{callback,KeyEvent}]),

    wxFrame:show(Frame).

Regarding Issue #5 yes it will crash since the parent of frame is null
pointer, you could have checked with
wx:is_null(Obj), the erlang wx api is just wrapper of wxWidgets, so if
you do bad things, bad things will happen.

/Dan

On Fri, May 4, 2012 at 3:09 AM, Tom Parker <thpr@REDACTED> wrote:
> Background: I'm new to Erlang, so just getting the hang of things... I'm
> trying to get key events back from a window, and exploring how to do that.
>
> If I do this in the erl shell:
>
> Wx=wx:new().
> F=wxFrame:new(Wx,-1,"Hello, World!").
> wxFrame:show(F).
> Canvas = wxPanel:new(F, []).
> wxWindow:setSize(Canvas,200,100).
> DC=wxPaintDC:new(Canvas).
> wxDC:clear(DC).
> wxEvtHandler:connect(F,key_down, [{callback, fun(Rec, Obj) ->
> io:format("Received:~p~n",[Rec]) end}]).
>
> Issue #1: With this setup, I get no events back (regardless of what I select
> in the UI prior to pressing any keys).
>
> If I change to key_up, I will get events, but ...
>
> Issue #2: ... the events only show up after I click into the (white due to
> the wxDC:clear call) Canvas, not just the title bar of the window or the
> other (gray) part of the Frame (see below for where the focus is before
> clicking in the Canvas).  I'm unsure why it has to be a child that has focus
> for me to get a callback.
>
> I also get char_hook events if I use that instead of key_down
>
> Issue #3: ... but no char_hook for things like the "Shift" key - the ONLY
> event I seem to be able to get from Shift is "key_up"
>
> Issue #4: No char events at all if I connect to those (just like key_down)
>
> (I think those are the 4 types of keyboard events I might find)
>
> Since I wanted to try to capture events even when the title bar was clicked
> (vs just contents of the window), I set up a test to identify where the
> focus is when I select the title bar.  Code not relevant here, but clicking
> on the title bar of the window puts focus (as reported by
> wxWindow.findFocus() into a wxWindow that is not Frame F.
> Focus:{wx_ref,0,wxWindow,[]}
> Transferring Focus to the Frame doesn't help receipt of events:
> Focus:{wx_ref,35,wxWindow,[]}
> Clicking into the Canvas puts focus on Canvas, and allows me to receive
> key_up and (some) char_hook events:
> Focus:{wx_ref,36,wxWindow,[]}
>
> Attempting the connect the event handler to Wx produces errors, so that
> doesn't work (fair enough), but it turns out Wx really isn't the parent of
> F.  The wxWindow (ID 0) that has the focus IS the parent of Frame F as
> reported by wxWindow.getParent(F).... so I tried this:
>
> Wx=wx:new().
> F=wxFrame:new(Wx,-1,"Hello, World!").
> wxFrame:show(F).
> Fparent=wxWindow:getParent(F).
> wxEvtHandler:connect(Fparent,key_down, [{callback, fun(Rec, Obj) ->
> io:format("Received:~p~n",[Rec]) end}]).
>
> Issue #5: This causes a segmentation fault.
>
> Erlang 15B01 from a source build (plain untar, make, make install...
> necessary since I wanted wxWindows) on Ubuntu 10.04-LTS
>
> Any thoughts, ideas or feedback?  Thanks.
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list