0

I found some example code when building a menu bar item in OS X. It makes use of the single | and I'm unsure what it actually means.

(What I'm trying to do is have a function called on left click of the menu item, but have it show the menu on right click)

Here's my code

//Get reference to main system status bar
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
statusItem.image = icon
statusItem.menu = menuBar
if let statusButton = statusItem.button
{
    statusButton.target = self
    statusButton.action = #selector(statusItemClicked)

    statusButton.sendActionOn(Int(NSEventMask.RightMouseUpMask.rawValue | NSEventMask.LeftMouseUpMask.rawValue))

}

Original Answer with code Left vs Right Click Status Bar Item Mac Swift 2

Community
  • 1
  • 1
Dallas
  • 1,788
  • 2
  • 13
  • 24

2 Answers2

2

Bitwise OR, just like it does in most C-like languages. In this context, it's being used to combine flags.

  • Do you understand exactly what its doing in this code? Im going to do a bit more research on it. This code doesn't actually work and I'm unsure if thats the issue. – Dallas Aug 06 '16 at 04:52
  • @Dallas It is old code and should be rewriten, like matt said. Take a look at the link from his (very good) book – FredericP Aug 06 '16 at 07:54
1

That must be really old code. Nowadays, in modern Swift, NSEventMask is an Option Set. You can just say [NSEventMask.rightMouseUp, NSEventMask.leftMouseUp], and you don't need the Int cast at all. (Or if you haven't updated to Swift 3 yet, the case names would start with capital letters.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    For more information on bitmasks, the old way (your way) of dealing with them and the new way (Option Sets), see my book: http://www.apeth.com/swiftBook/ch04.html#SECoptionsSets – matt Aug 06 '16 at 04:56