KeyDown in Cocoa

I guess I have to try to switch from Carbon to Cocoa. I’ve managed to make a few changes and got it to compile. What I can’t figure out is why the form’s keydown event that fires in Carbon won’t fire in Cocoa?

If by form you mean window, if a textfield is placed over the window, it will get the focus and therefore “steal” keydown.

When there is no TextField, the keydown event works.

What do you need to achieve ?

We are porting a Delphi Windows CAD application to Xojo for osx.
The drawing area is a canvas. When the user presses an arrow key, we move the cursor. Using Carbon, we have that code in the window’s keydown event. It always fires regardless of what has focus.

That event doesn’t fire when compiled in Cocoa,
I tried putting the code in the canvas keydown event, but that doesn’t fire either.
I cleared out all code and put a msgbox in the window.keydown and in the canvas.keydown event. Neither fires.
Regardless of what control has focus, we need the cursor to move. It works flawlessly in carbon.

[quote=111430:@Charles Herndon]We are porting a Delphi Windows CAD application to Xojo for osx.
The drawing area is a canvas. When the user presses an arrow key, we move the cursor. Using Carbon, we have that code in the window’s keydown event. It always fires regardless of what has focus.

That event doesn’t fire when compiled in Cocoa,
I tried putting the code in the canvas keydown event, but that doesn’t fire either.
I cleared out all code and put a msgbox in the window.keydown and in the canvas.keydown event. Neither fires.
Regardless of what control has focus, we need the cursor to move. It works flawlessly in carbon.[/quote]

Indeed things work differently in Cocoa. This works in the canvas events :

Sub Open() me.AcceptFocus = true me.setfocus End Sub

Function KeyDown(Key As String) As Boolean beep End Function

That allows the canvas to get focus, but it doesn’t set the focus except in the Open event.
I guess that in Cocoa, we aren’t able to get a keydown to fire unless we first set the focus to the canvas.

Also, me.mousecursor=MyCursor doesn’t work in Cocoa. Still get the arrow cursor.

[quote=111494:@Charles Herndon]That allows the canvas to get focus, but it doesn’t set the focus except in the Open event.
I guess that in Cocoa, we aren’t able to get a keydown to fire unless we first set the focus to the canvas.[/quote]

Programming is all about being able to think out of the box. A bit of mental flexibility and patience goes a long way to achieving results. A quick experiment shows that ClearFocus works. When ClearFocus is applied, Window.Keydown fires.

Here is the code I placed into a TextField to keep focus away from that control :

Sub GotFocus() if isInTF = false then clearfocus End Sub

isInTF is a boolean variable I set to True in TextField.MouseEnter and False in MouseExit, so the user can still click into it to enter text. A similar thing has to be done when the user uses the Tab key.

Cocoa is neither worse or better than Carbon. It does things differently. Now, one can lament about the good old days and play the blame game, or be pragmatic about things. Carbon is dead. Long live Cocoa. Some time in the future Cocoa will die, and it will be necessary to adapt. That is called living.

[quote=111494:@Charles Herndon]Also, me.mousecursor=MyCursor doesn’t work in Cocoa. Still get the arrow cursor.
[/quote]

The format of custom cursors have changed from Carbon. Here is what works in Cocoa :

Sub Open() dim c as mousecursor c = new mousecursor(cursor_lifebuoy, 2,2) self.mousecursor = c End Sub

cursor_lifebuoy is an icon from the Fatcow collection. You can have cursors of any size you want !

Thanks,
If I do that in the canvas open event, it uses that cursor all over the window. How can I change it back to the standard cursor when the mouse leaves the canvas?
Do I have to make a copy of the standard cursor and switch in MouseEnter and MouseExit?

The standard cursor is available in the Cursors module.
To reset the cursor on MouseExit use:

Me.MouseCursor=System.Cursors.StandardPointer

Right. Thanks.
I think I have to use app.mousecursor and switch in the canvas mouse in & out.

[quote=111619:@Charles Herndon]Right. Thanks.
I think I have to use app.mousecursor and switch in the canvas mouse in & out.[/quote]

If you go Me.MouseCursor in the Canvas Open event, the custom cursor will apply only for the canvas. So you do not have to set app.MouseCursor. Try it.

I must be denser than usual today.

In the canvas open event I have:
If TargetCocoa then
dim c as mousecursor
c = new mousecursor(cross24, 11,11)
self.mousecursor = c
End if

In the canvas mouseExit:
If TargetCocoa then
me.mousecursor=System.Cursors.StandardPointer
end if

On startup, It uses my cursor over the canvas. When I leave the canvas, it still uses my cursor over other controls.
When I return to the canvas it is now the standard pointer.
When I leave the canvas again, over other controls, it uses my cursor. back on the canvas, it uses the standard pointer .

“self” is the window. “me” is the canvas. You set the window’s cursor.

[quote=111850:@Charles Herndon]On startup, It uses my cursor over the canvas. When I leave the canvas, it still uses my cursor over other controls.
When I return to the canvas it is now the standard pointer.
When I leave the canvas again, over other controls, it uses my cursor. back on the canvas, it uses the standard pointer .[/quote]

If TargetCocoa then
dim c as mousecursor
c = new mousecursor(cross24, 11,11)
me.mousecursor = c
End if

That is all you need in the Canvas.Open event. Leave Canvas.MouseExit empty. As soon as the cursor leaves the canvas, it reverts automatically to the default cursor for the window.

Yes it does. Thanks.