vosc
vosc

Reputation: 118

NSView keyDown not called during drag and drop in Cocoa application

I have a cocoa application where i need to respond to keyDown events during dragging sessions in order to trigger other things. The keys are not modifiers only but can be any kind of key.

The problem is, when i start a dragging session by calling NSWindow.dragImage, i don't get keyDown events during that dragging operation. When i drag an external resource over/into the application window (e.g. a file from Finder) it works fine, NSView.keyDown gets called.

Is there any way of getting around this? Thanks you!

Upvotes: 0

Views: 563

Answers (1)

Ryan Nichols
Ryan Nichols

Reputation: 308

This is probably because the dragging session is using a 'mouse-tracking loop' to handle the drag events. This effectively blocks all other events from being sent to their targets except for the drag events. You can read about this here in the event documentation.

The reason dragging items onto your app does NOT do this is because only the app initiating the drag session is event blocked, not the app receiving the drop.

Having said this, you could try to capture the keyEvents during the mouseDragged event using nextEventMatchingMask:untilDate:inMode:dequeue: this should allow you to pop-off any key events that might be in the event queue.

Also in the documentation linked above they discuss handling key events during mouse drag operations. However the examples don't specifically fit when initiating a drag session with dragImage:at:offset:event:pasteboard:source:slideBack: but you may try watching the performKeyEquivalent as they mention or even seeing if NSWindow's sendEvent: is receiving events during the drag session.

Upvotes: 1

Related Questions