Skip to main content
Updated Swift call
Source Link
mmackh
  • 3.7k
  • 3
  • 38
  • 53

For iOS 7.0 or later, you can return UIKeyCommands for the keyCommands property from any UIResponder, such as UIViewController:

Objective-C

// In a view or view controller subclass: - (BOOL)canBecomeFirstResponder { return YES; } - (NSArray *)keyCommands { return @[ [UIKeyCommand keyCommandWithInput:@"\r" modifierFlags:0 action:@selector(enterPressed)] ]; } - (void)enterPressed { NSLog(@"Enter pressed"); } 

Swift

// In a UIView/UIViewController subclass: override funcvar canBecomeFirstResponder() ->: Bool {   return true } override var keyCommands: [UIKeyCommand]? { return [ UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterPressed)) ] } @objc func enterPressed() { print("Enter pressed") } 

For iOS 7.0 or later, you can return UIKeyCommands for the keyCommands property from any UIResponder, such as UIViewController:

Objective-C

// In a view or view controller subclass: - (BOOL)canBecomeFirstResponder { return YES; } - (NSArray *)keyCommands { return @[ [UIKeyCommand keyCommandWithInput:@"\r" modifierFlags:0 action:@selector(enterPressed)] ]; } - (void)enterPressed { NSLog(@"Enter pressed"); } 

Swift

// In a UIView/UIViewController subclass: override func canBecomeFirstResponder() -> Bool {   return true } override var keyCommands: [UIKeyCommand]? { return [ UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterPressed)) ] } @objc func enterPressed() { print("Enter pressed") } 

For iOS 7.0 or later, you can return UIKeyCommands for the keyCommands property from any UIResponder, such as UIViewController:

Objective-C

// In a view or view controller subclass: - (BOOL)canBecomeFirstResponder { return YES; } - (NSArray *)keyCommands { return @[ [UIKeyCommand keyCommandWithInput:@"\r" modifierFlags:0 action:@selector(enterPressed)] ]; } - (void)enterPressed { NSLog(@"Enter pressed"); } 

Swift

// In a UIView/UIViewController subclass: override var canBecomeFirstResponder: Bool { true } override var keyCommands: [UIKeyCommand]? { return [ UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterPressed)) ] } @objc func enterPressed() { print("Enter pressed") } 
Add Swift code
Source Link
Patrick Pijnappel
  • 7.7k
  • 4
  • 42
  • 40

For iOS 7.0 or later, you can return UIKeyCommands for the keyCommands property from any UIResponder, such as UIViewController:

Objective-C

// In a view or view controller subclass: - (BOOL)canBecomeFirstResponder { return YES; } - (NSArray *)keyCommands { return @[ [UIKeyCommand keyCommandWithInput:@"\r" modifierFlags:0 action:@selector(enterPressed)] ]; } - (void)enterPressed { NSLog(@"Enter pressed"); } 

Swift

  
// In a UIView/UIViewController subclass: override func canBecomeFirstResponder() -> Bool { return true } override var keyCommands: [UIKeyCommand]? { return [ UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterPressed)) ] } @objc func enterPressed() { print("Enter pressed") } 

For iOS 7.0 or later, you can return UIKeyCommands for the keyCommands property from any UIResponder, such as UIViewController:

Objective-C

// In a view or view controller subclass: - (BOOL)canBecomeFirstResponder { return YES; } - (NSArray *)keyCommands { return @[ [UIKeyCommand keyCommandWithInput:@"\r" modifierFlags:0 action:@selector(enterPressed)] ]; } - (void)enterPressed { NSLog(@"Enter pressed"); } 

Swift

 
// In a UIView/UIViewController subclass: override func canBecomeFirstResponder() -> Bool { return true } override var keyCommands: [UIKeyCommand]? { return [ UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterPressed)) ] } @objc func enterPressed() { print("Enter pressed") } 

For iOS 7.0 or later, you can return UIKeyCommands for the keyCommands property from any UIResponder, such as UIViewController:

Objective-C

// In a view or view controller subclass: - (BOOL)canBecomeFirstResponder { return YES; } - (NSArray *)keyCommands { return @[ [UIKeyCommand keyCommandWithInput:@"\r" modifierFlags:0 action:@selector(enterPressed)] ]; } - (void)enterPressed { NSLog(@"Enter pressed"); } 

Swift

 
// In a UIView/UIViewController subclass: override func canBecomeFirstResponder() -> Bool { return true } override var keyCommands: [UIKeyCommand]? { return [ UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterPressed)) ] } @objc func enterPressed() { print("Enter pressed") } 
Add Swift code
Source Link
Patrick Pijnappel
  • 7.7k
  • 4
  • 42
  • 40

For iOS 7.0 or later, you can return UIKeyCommands for the keyCommands property from any UIResponder, such as UIViewController:

Objective-C

// In a view or view controller subclass: - (BOOL)canBecomeFirstResponder { return YES; } - (NSArray *)keyCommands { return @[ [UIKeyCommand keyCommandWithInput:@"\r" modifierFlags:0 action:@selector(enterPressed)] ]; } - (void)enterPressed { NSLog(@"Enter pressed"); } 

Swift

// In a UIView/UIViewController subclass: override func canBecomeFirstResponder() -> Bool { return true } override var keyCommands: [UIKeyCommand]? { return [ UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterPressed)) ] } @objc func enterPressed() { print("Enter pressed") } 

For iOS 7.0 or later, you can return UIKeyCommands for the keyCommands property from any UIResponder, such as UIViewController:

// In a view or view controller subclass: - (BOOL)canBecomeFirstResponder { return YES; } - (NSArray *)keyCommands { return @[ [UIKeyCommand keyCommandWithInput:@"\r" modifierFlags:0 action:@selector(enterPressed)] ]; } - (void)enterPressed { NSLog(@"Enter pressed"); } 

For iOS 7.0 or later, you can return UIKeyCommands for the keyCommands property from any UIResponder, such as UIViewController:

Objective-C

// In a view or view controller subclass: - (BOOL)canBecomeFirstResponder { return YES; } - (NSArray *)keyCommands { return @[ [UIKeyCommand keyCommandWithInput:@"\r" modifierFlags:0 action:@selector(enterPressed)] ]; } - (void)enterPressed { NSLog(@"Enter pressed"); } 

Swift

// In a UIView/UIViewController subclass: override func canBecomeFirstResponder() -> Bool { return true } override var keyCommands: [UIKeyCommand]? { return [ UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(enterPressed)) ] } @objc func enterPressed() { print("Enter pressed") } 
Tested code (removed disclamer), add minor code corrections
Source Link
Patrick Pijnappel
  • 7.7k
  • 4
  • 42
  • 40
Loading
added 61 characters in body
Source Link
Patrick Pijnappel
  • 7.7k
  • 4
  • 42
  • 40
Loading
Source Link
Patrick Pijnappel
  • 7.7k
  • 4
  • 42
  • 40
Loading