8

I'm making a form in Xcode using objective-c and cocoa. In the interface builder I have 2 textfields and 2 buttons. When I build the project and run it, neither of the two buttons are selected (highlighted in blue as opposed to white). When the user is writing in the text field the ENTER key needs to trigger one of the buttons to be pressed.

How do I do this?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Seb123
  • 471
  • 1
  • 7
  • 20

2 Answers2

18

You set the Key Equivalent value for your button in IB. Just click on that field in the attributes inspector and press the enter key.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thank you very much!!! That was exactly what I needed. I'm not sure how I managed to miss it when it was right in front of me... – Seb123 Aug 09 '12 at 16:11
4

A link to Apple's documentation is here: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Button/Articles/MakingaButtontheDefaultButton.html

this code sets the return key to be the default:

[myButton setKeyEquivalent:@"\r"];

not mentioned in the docs, but to programmatically remove that key, you would use:

[myButton setKeyEquivalent:@""];
Cœur
  • 37,241
  • 25
  • 195
  • 267
Daniel Saban
  • 478
  • 3
  • 16
  • Don't forget to use the **Push** button style as well or you won't see the blue button! – Jay Apr 01 '15 at 15:49