Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setKeyEquivalent to Enter/Return key on NSButton?

Tags:

cocoa

nsbutton

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

Thanks.

like image 568
Randall Avatar asked Jan 09 '11 21:01

Randall


2 Answers

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

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

like image 77
Matt Bierner Avatar answered Nov 16 '22 21:11

Matt Bierner


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)
like image 44
seb Avatar answered Nov 16 '22 20:11

seb