Window.KeyUp doesn't fire

Hi,
I am having issues with Window.KeyUp. I’m currently running Xojo 2013r4.1 on OS X (Cocoa).

Basically, the KeyUp event of my main window never fires. I mean NEVER.
I tried putting a MsgBox(Str(Asc(Key))) inside to see when events fired, and it just doesn’t appear.

What I am trying to achieve is to make some key on the keyboard trigger a method, but only do so once even if I hold the key down. To trigger it once more, I want the user to release the key and press it again.

I thought of achieving this by mixing KeyDown and KeyUp, but it doesn’t seem to be working.

Any suggestions?
Thanks

Are you returning True in KeyDown? If not KeyUp will not be raised by the framework.

Yes I am!
I tried in every way: with Return True, without, by commenting out the whole KeyDown… Nothing!

Here’s the code in my KeyDown event (lastKey is a public property defined in the window as an Integer):

[code] if Asc(Key) <> lastKey then
Select Case Asc(Key)

Case 8
  // do something
  
Case 56
  // do something
  
Case 53
  // do something
  
Case 50
  // do something
  
Case 48
  // do something
  
Case 3
  // do something
  
Case 52
  // do something
  
End Select
lastKey = Asc(key)

end if

Return True[/code]

This is what I have in KeyUp:

MsgBox(Str(Asc(Key)))

Thanks for the help!

Window.KeyUp ( Key as String )

Fires when the passed Key is released and no other object on the window has captured the event.
It is not guaranteed to be the same key that received the KeyDown event.

Of course it’ll only work if no control has focus. So issue ClearFocus() so that no control has focus.

Thank you both.
Axel, what is that supposed to mean? I imagine it meant that another key could have been released before the one that last triggered KeyDown.

Eli, ClearFocus() did not work. If I simply open the window, without touching anything, and press any key, no KeyUp event fires.

KeyUp seems to work as expected in Windows, Linux and OS X Carbon, but not Cocoa so I’d suspect it is a bug. Be sure to create a Feedback case with a small example.

The problem extends to ContainerControl as well, and I could not get a Canvas to receive the keys.

Here is a workaround I verified before posting :

  • Add a TextField to the window. Make sure it has a TabIndex of 0.
  • Add to it KeyDown with Return True inside
  • Add KeyUp
  • Drag the TextField out of the window

When run, the TextField is invisible, but it does receive keyboard events. So this will enable them for the window, as long as the TextField keeps the focus.

Is the canvas AcceptFocus (and AcceptTabs) set to true?
I just tested KeyDown and KeyUp for Window, ContainerControl and Canvas, and it’s working on Cocoa and Windows as long as no other control like text fields, checkboxes, or similar has focus.

[quote=83840:@Eli Ott]Is the canvas AcceptFocus (and AcceptTabs) set to true?
I just tested KeyDown and KeyUp for Window, ContainerControl and Canvas, and it’s working on Cocoa and Windows as long as no other control like text fields, checkboxes, or similar has focus.[/quote]

You are right. Canvas and container control do trigger keydown/keyup when acceptfocus and accepttab are true. I must have been tired. Thank you.

Problem with Cocoa is that there is a bug : when a textfield is present, it steals the focus no matter the TabIndex. I used it to its advantage.

No, quick test shows it works.

  1. Place a canvas on a window.
  2. Set AcceptFocus and AcceptTab to true.
  3. Place a text field on the window.
    On opening of the window, the focus is on the canvas.

[quote=83851:@Eli Ott]No, quick test shows it works.

  1. Place a canvas on a window.
  2. Set AcceptFocus and AcceptTab to true.
  3. Place a text field on the window.
    On opening of the window, the focus is on the canvas.[/quote]

Then everything is dandy… Have a good day.