Menu

Noticing "Shift held during a file drag" from a macOS menu bar app
📰

Noticing "Shift held during a file drag" from a macOS menu bar app

DEV Community: swift·Usukhbayar·about 12 hours ago
#zXxtHB2l
#devtoswift#devto#dev#drag#convrt
Reading 0:00
15s threshold

Convrt is a small open-source menu bar app: you start dragging files from Finder or the desktop, hold Shift, and a wheel opens under the cursor. Drop on a slice and the file is converted (HEIC to JPG, MOV to MP4, images to PDF and so on).

The interesting part isn't the conversion. It's how a background app with no window finds out that some other app is dragging files and that Shift is down, without breaking the drag. Here is how it works in the shipped code.

1. Watch mouse and modifier events globally

A menu bar app never gets these events directly, so it uses a global monitor:

let mask : NSEvent . EventTypeMask = [ . leftMouseDown , . leftMouseDragged , . leftMouseUp , . flagsChanged , . scrollWheel , . keyDown ] monitor = NSEvent . addGlobalMonitorForEvents ( matching : mask ) { [ weak self ] event in self ? .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.