<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><span>Background: </span><span>I'm new to Erlang, so just getting the hang of things...</span><span> I'm trying to get key events back from a window, and exploring how to do that. <br></span></div><div><span><br></span></div>If I do this in the erl shell:<br><br><div><span>Wx=wx:new().<br>
F=wxFrame:new(Wx,-1,"Hello, World!").<br>
wxFrame:show(F).<br>
Canvas = wxPanel:new(F, []).<br>wxWindow:setSize(Canvas,200,100).</span></div><div><span>DC=wxPaintDC:new(Canvas).<br>wxDC:clear(DC).<br>wxEvtHandler:connect(F,key_down, [{callback, fun(Rec, Obj) -> io:format("Received:~p~n",[Rec]) end}]).<br>
</span></div>
<br>Issue #1: With this setup, I get no events back (regardless of what I select in the UI prior to pressing any keys).<br><br>If I change to key_up, I will get events, but ...<br><br>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.<br><br>I also get char_hook events if I use that instead of key_down<br><br>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"<br><br>Issue #4: No char events at all if I connect to those (just like key_down)<br><br>(I think those are the 4 types of keyboard events I might find)<br><br>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. <br>Focus:{wx_ref,0,wxWindow,[]}<br>Transferring Focus to the Frame doesn't help receipt of events:<br>Focus:{wx_ref,35,wxWindow,[]}<br>
Clicking into the Canvas puts focus on Canvas, and allows me to receive key_up and (some) char_hook events:<br>Focus:{wx_ref,36,wxWindow,[]}<br><br>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:<br><br>Wx=wx:new().<br><div><span>
F=wxFrame:new(Wx,-1,"Hello, World!").<br>
wxFrame:show(F).<br>
</span><span></span><span>Fparent=wxWindow:getParent(F).<br>
wxEvtHandler:connect(Fparent,key_down, [{callback, fun(Rec, Obj) -> io:format("Received:~p~n",[Rec]) end}]).<br>
</span></div>
<br>Issue #5: This causes a segmentation fault.<br><br>Erlang 15B01 from a source build (plain untar, make, make install... necessary since I wanted wxWindows) on Ubuntu 10.04-LTS<br><br>
Any thoughts, ideas or feedback? Thanks.<br><br><br>
</div></body></html>