How to connect wxEVT_KEY_DOWN?

BlitzMax Forums/Brucey's Modules/How to connect wxEVT_KEY_DOWN?

d-bug(Posted 2008) [#1]
Hi,

is it true, that I only can connect wxEVT_KEY_DOWN events with ConnectAny? I mean, I like to be able to connect it to a canvas or an other gadget. Am I missing something?

Whats the right way for doing that?


DavidDC(Posted 2008) [#2]
I think you do need to use ConnectAny - but like you I've had trouble figuring out exactly why. Apparently wxKeyEvents don't propagate from a child window back to a parent window - so if the gadget has keyboard focus (SetFocus) you should in theory just connect it up via ConnectAny without your other gadgets getting confused.

I know it works fine for gadgets that use the keyboard inherently - like a textfield - but am not so sure about plain windows and canvases.

Brucey has played around with this more than me. I think I recall him saying that with a canvas, he had to use an accelerator table. But that's just a vague memory and could easily be wrong. I'm sure he'll be along any day soon...


d-bug(Posted 2008) [#3]
Thank you, thats working with some little workarounds, like catching wxEVT_MOUSE_ENTER and wxEVT_MOUSE_LEAVE events to check if it is the right canvas, SetFocus doesn't work on a glcanvas (not checked on max2d canvas nor other gadgets).

The only funny thing about it is, that when I use JEdit as the IDE (the better alternative on OSX) and compile it from there, keyhits will be produced in JEdits textarea. Quiet strange behaviour, I think. Maybe i forgot something while initializing wxMax (or its just a bug in JEdit or wxMax).

cheers


DavidDC(Posted 2008) [#4]
I'm wondering if the behaviour (false keyhit in JEdit) occurs when you run your compiled app standalone ie by double clicking on it. My guess is not. This could be somehow related to the "app window beneath the IDE" issue that wxMax has in OSX, which is related wxWidget's use of Carbon I think. Again, Brucey would be the person to talk to...


d-bug(Posted 2008) [#5]
Thank you again!

I think it's better to comment all userinput out until there is a solution for it. I'm still at the beginning of my LevelEditor, so there is much more to do then this!


Brucey(Posted 2008) [#6]
If you want to use Max2D style keyboard capture on a GLCanvas, you can do something like this :

...
Type MyCanvas Extends wxGLCanvas


	Method OnInit()
		
		EnablePolledInput(Self)

		ConnectAny(wxEVT_KEY_DOWN, OnKeyDown)
		ConnectAny(wxEVT_KEY_UP, OnKeyUp)

	End Method

	Function OnKeyDown(event:wxEvent)
		Local evt:wxKeyEvent = wxKeyEvent(event)
		EmitEvent(CreateEvent( EVENT_KEYDOWN, event.parent, MapWxKeyCodeToBlitz(evt.GetKeyCode())))
		event.Skip()
	End Function

	Function OnKeyUp(event:wxEvent)
		Local evt:wxKeyEvent = wxKeyEvent(event)
		EmitEvent(CreateEvent( EVENT_KEYUP, event.parent, MapWxKeyCodeToBlitz(evt.GetKeyCode())))
		event.Skip()
	End Function

...

End Type


And then, in your OnPaint event, you can work much like you would with Max2D, using things like KeyDown() and KeyUp() to determine current key states.

Are you using the custom appstub for OS X? That fixes numerous problems with BRL's appstub and the fact it is designed to work with Cocoa.


DavidDC(Posted 2008) [#7]
Wow, that's excellent. Didn't know you could do that!


d-bug(Posted 2008) [#8]
Great tip, but I meant it like MaxGUI-Panel style keyboard capture.

Yes, I'm using your appstub wich is part of some problems I have had with wxMax on Leopard. Compiling with -b bah.appstub forces carbon wich leeds to unstartable apps. But they are much better then crashing apps while development without your appstub. ;)

cheers

(I have to write more english *stumble on each word*)


Brucey(Posted 2008) [#9]
I'm currently working exclusively on Mac (while I await the ship to arrive in the UK with the rest of my stuff), and it works great here, both on 10.4 (PPC) and 10.5 (Intel). I am of course using David's slightly modified IDE which includes shortcuts for building/running apps using the custom appstub - you press cmd+shift-B/R. You can download the source from the maxmods site if you want to try it out.

Anyhoo... keyboard events should work by simply attaching the appropriate down/up connections to the control you wish to capture. Of course, your control will require focus.
My example above, assumed you wanted a Max2D-style of capture, but of course you can leave out the Max2D references and instead just use the OnKeyUp/Down functions if you wish.

If you have trouble connecting the event to a control (without first sub-classing it, as my above example shows), there is a second form of using ConnectAny which you may like to try. wxCodeGen uses this form extensively.
myControl.ConnectAny(wxEVT_KEY_DOWN, OnKeyDown, Null, Self)

Code like this would normally appear in the parent control's OnInit() method, with the OnKeyDown Function belonging to the parent, not the Child.
If you use this form you need to be aware that event.parent will refer to the control raising the event, not the parent to which the OnKeyDown function belongs. In this case you can use the "sink" field of the event to access the parent :
	Function OnKeyDown(event:wxEvent)
		TParentControl(event.sink).HandleSomeKeys(wxKeyEvent(event))
	End Function


If you want to code like MaxGUI, where all your events go to the same event callback function, you can still do that if you wish (you just point all your Connects to the same function), but I personally think it is easier to have it separated into individual functions.

I hope this was clear, about how some of this works?

btw, your English is far better than my German! When I was in Frankfurt, I think the best I could manage was to ask for a curry-wurst :-p


d-bug(Posted 2008) [#10]
Thank you again, Brucey! The last one was exactly what I'm looking for! That also stops the JEdit issue.
I'm no friend of all those hacks on the original MaxIDE. Its still not my kind of IDE, but I will give it another try if this would help to fix the nonstartable apps thing!

...most germans can't speak german too they have horrible slangs. Saxony is my favorite... *shutter* ;)

~edit~
That nonstartable-carbon-app-issue is also related to JEdit. Id didn't use your custom IDE, but by compiling in a normal Terminal instead of compiling in JEdit.Console this issue is fixed. I think it's time to use another alternative for JEdit. What about your Eclipse plugin? (just kidding)