开发者

How to set Command+ as a key equivalent?

开发者 https://www.devze.com 2023-04-12 18:55 出处:网络
I want to have command-plus as a key equivalent for a menu item, however, when I try to enter command-plus, it shows up as command-shift-equals instead of a plus.And when I press com开发者_Go百科mand-

I want to have command-plus as a key equivalent for a menu item, however, when I try to enter command-plus, it shows up as command-shift-equals instead of a plus. And when I press com开发者_Go百科mand-plus, it shows up as command-equals. Does anyone know how I can get around this?


In Xcode 8.2 when entering a key equivalent, if you hit ⇧⌘= you get a little popup button saying "alternates" inside the field. It presents you ⇧⌘= and ⌘+ as alternates (importantly, this only shows if you enter the key equivalent from the text field in the inspector, not by double-clicking the key equivalent slot for a menu item in IB). This is where I presume you are intended to choose ⌘+ if you want it presented like that.

However, that doesn't actually appear to work at least when compiling my app in macOS Sierra 10.12 / with Xcode 8.2.1. I'm posting this as a solution as the key equivalent looks correct in Interface Builder after doing this, and issue with it happens only during runtime so I assume there is a version of the Xcode build toolchain or Cocoa where this actually works.

Interestingly, editing the storyboard gives the same symptom (the equivalent works fine in IB but shows up blank ):

  <menuItem title="Increase Image Size" keyEquivalent="+" id="SCO-e4-3zd">
    <modifierMask key="keyEquivalentModifierMask" command="YES" />
    <connections>
      <action selector="increaseRowHeight:" target="Ady-hI-5gd" id="pu1-6E-HSr"/>
      </connections>
  </menuItem>

A third solution has the same runtime symptoms too (assign an outlet to the item and set it in code):

    decreaseThumbnailSizeItem.keyEquivalent = "+"
    decreaseThumbnailSizeItem.keyEquivalentModifierMask = NSCommandKeyMask

The same works when setting either NSShiftKeyMask or NSAlternateKeyMask together with + as the key.


To type a + sign you actually have to press Shift+'=', that's why when you put Cmd+ into key equivalent it shows up as Cmd+Shift+=.


This can be done programmatically. Connect the menu item to a property ("zoomInItem") in interface builder. Then set the key equivalent when the application is finished launching.

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
    [self.zoomInItem setKeyEquivalent:@"+"];
}

How to set Command+ as a key equivalent?


If you have access to a keyboard with a numerical keypad, you could use the numpad + key.


You can open the xib file in a text editor add edit the menu item's key equivalent there:

<menuItem title="Zoom In" keyEquivalent="+" id="hXP-3c-xDr"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号