开发者

How to setKeyEquivalent to Enter/Return key on NSButton?

开发者 https://www.devze.com 2023-02-03 20:37 出处:网络
I\'ve tried [myButton setKeyEquivalent:@\"\\n\"]开发者_如何学Python but that didn\'t work. I feel like there should be some predefined constant for it that I\'m just missing.

I've tried [myButton setKeyEquivalent:@"\n"]开发者_如何学Python but that didn't work. I feel like there should be some predefined constant for it that I'm just missing.

Thanks.


Try using "\r" instead of "\n" in this case.

I believe [myButton setKeyEquivalent:@"\r"] should do what you are looking for.


Example for Swift 2.0:

let key = String(utf16CodeUnits: [unichar(NSEnterCharacter)], count: 1) as String
item.keyEquivalent = key
item.keyEquivalentModifierMask = Int(NSEventModifierFlags.CommandKeyMask.rawValue | NSEventModifierFlags.ControlKeyMask.rawValue)


Beware that "as is", this will require to type Command + Return (which I think is Enter). To have this work without Command I had to add [myButton setKeyEquivalentModifierMask:0];.

0

精彩评论

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