Hi. I’m converting a very old Mac Carbon-based app to a Mac Cocoa-based app using the latest Xojo on a Mac. On my app’s main window, I need to look for a user keystroke. In my old app, I was easily able to get and handle a new keystroke using the KeyDown event. On my new Cocoa-based app, it appears I’m not even getting a KeyDown event. Doing a little experimenting, I found that I was able to switch from getting the required keystroke by using the KeyUP event instead of the KeyDOWN event. Now that part of the app works perfectly. But, I’m wondering why that is. Did the move from Carbon to Cocoa switch the way you are supposed to get a keystroke from using KeyDown to using KeyUp? I’ve already fixed things, but I am curious why this fix was required and if I really found and fixed a problem, or just have a workaround that works. Thanks for thoughts.
Firstly, keyDown and Keyup events work exactly the same on Cocoa as they did on Carbon for me. You must return True in keyDown to make Keyup fire as before. Something else is going on in your code. The window keyDown will only receive the event if no control such as a textArea intercepts it first. Maybe that’s what’s happening.
Thanks Roger. “something else going on in your code”…that’s ALWAYS possible. And, thank you, I did read about how other controls in a window might be getting and using the KeyDown event before it gets to the main window. I don’t think that’s the case here, but I’m still re-examining everything…thanks for thoughts.
One major difference is that in Cocoa, TextField tends to “steal” focus, so if you have a TextField on the window, the window keydown apparently won’t fire.
A solution is to use ClearFocus in Activate. Another is to monitor Keyboard.AsyncKeydown in a timer instead of in Window.Keydown, so wherever the focus is, you always get the keyboard.
Or put this into the KeyDown event of the control(s) stealing the focus:
Function KeyDown(Key As String) As Boolean
If Self.Event_KeyDown(key) Then // raises the window's KeyDown event
Note that raising an event with Event_EventName is not documented and might not work in future versions of Xojo.
Thanks Michel. There’s no TextFields on my window, but I appreciate the info on ClearFocus, etc. (Thanks Eli too…still experimenting and reading here. Learning from everyone.)