如何使用 return/enter 键制作 UIKeyCommand?

问题描述 投票:0回答:2

我想制作一个仅使用

UIKeyCommand
键的
[return]
。到目前为止我已经尝试过:

UIKeyCommand *selectCommand = [UIKeyCommand keyCommandWithInput:@"\n" modifierFlags:0 action:@selector(chooseSelection:)];

回车键没有全局常量,就像上、下、左、右和退出键一样(来自

UIResponder.h
):

// These are pre-defined constants for use with the input property of UIKeyCommand objects.
UIKIT_EXTERN NSString *const UIKeyInputUpArrow         NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputDownArrow       NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputLeftArrow       NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputRightArrow      NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIKeyInputEscape          NS_AVAILABLE_IOS(7_0);

还有什么我可以尝试捕获返回/输入键吗?

ios objective-c ios7 uiresponder uikeycommand
2个回答
47
投票

使用

@"\r"
进行回车而不是
@"\n"
进行换行应该可以。


0
投票

带有

UIKeyCommand
\r
并不总是适用于现代 iOS 版本,您必须将
wantsPriorityOverSystemBehavior
属性设置为
true

let selectCommand = UIKeyCommand(input: "\r", modifierFlags: [], action: #selector(chooseSelection(_:)))
selectCommand.wantsPriorityOverSystemBehavior = true
© www.soinside.com 2019 - 2024. All rights reserved.