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

Tom Parker <>
Fri May 4 03:09:37 CEST 2012


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120503/d8530813/attachment.html>


More information about the erlang-questions mailing list