在 iOS 14 Objective-C 中连接多个键盘
Connecting multiple keyboards in iOS 14 Objective-C
iOS 14
介绍GCKeyboard
。我能够在我的应用程序中成功连接 BlueTooth NumberPad
,但我需要能够串联连接其中的两个。谁能给我指出示例代码?
在 GCController
class 中有一个“controllers
”方法,其中 returns 所有连接的控制器的数组,但没有任何类似的东西 GCKeyboard
.
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
[[UIApplication sharedApplication]setIdleTimerDisabled:YES];
// notifications for keyboard (dis)connect
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWasConnected:) name:GCKeyboardDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWasDisconnected:) name:GCKeyboardDidDisconnectNotification object:nil];
}
GCKeyboard *leftKeyboard;
GCKeyboard *rightKeyboard;
- (void)keyboardWasConnected:(NSNotification *)notification {
if (!self.leftKeyboard) {
leftKeyboard = (GCKeyboard *)notification.object;
NSLog(@"Left Keyboard connected: %@\n", leftKeyboard.description);
NSString *keyboardStatus = [NSString stringWithFormat:@"Left Keyboard CONNECTED:\n%@", leftKeyboard.description];
self.statusLabel.text = keyboardStatus;
self.leftKeyboard = leftKeyboard;
[self reactToKeyboardInput];
}
else if (!self.rightKeyboard) {
NSLog(@"Right Keyboard connected: %@\n", rightKeyboard.description);
rightKeyboard = (GCKeyboard *)notification.object;
NSString *keyboardStatus = [NSString stringWithFormat:@"Right Keyboard CONNECTED:\n%@", rightKeyboard.description];
self.statusLabel.text = keyboardStatus;
self.rightKeyboard = rightKeyboard;
[self reactToKeyboardInput];
}
}
- (void)keyboardWasDisconnected:(NSNotification *)notification {
if (self.leftKeyboard) {
GCKeyboard *keyboard = (GCKeyboard *)notification.object;
NSString *status = [NSString stringWithFormat:@"Left Keyboard DISCONNECTED:\n%@", keyboard.description];
self.statusLabel.text = status;
self.leftKeyboard = nil;
}
else if (self.rightKeyboard) {
GCKeyboard *keyboard = (GCKeyboard *)notification.object;
NSString *status = [NSString stringWithFormat:@"Right Keyboard DISCONNECTED:\n%@", keyboard.description];
self.statusLabel.text = status;
self.rightKeyboard = nil;
}
}
连接键盘或数字键盘后,我收到消息:
2020-09-18 13:09:15.845943-0700 Controller[1653:351628] Left Keyboard connected: GCController 0x280bb8dd0 ('Keyboard' - 0x27c3dc28cec4d818)
“将键盘和鼠标游戏带入 iPad”WWDC 视频抄本指出:
In the case of keyboards, you can't differentiate multiple keyboards, and all keyboard events from multiple keyboards are instead coalesced for you. So while you might use notifications to notice when a keyboard disconnects and perhaps pause the game to prompt for different input, in general, you'll find that just using GCKeyboard-Device.coalesced to check on the key state when non-nil is the right path.
因此,虽然您可能连接了多个键盘,但听起来它们都合并到一个 GCKeyboard
实例中,您无法区分它们。这就是为什么没有 +[GCKeyboard keyboards]
方法的原因。取而代之的是 +[GCKeyboard coalescedKeyboard]
iOS 14
介绍GCKeyboard
。我能够在我的应用程序中成功连接 BlueTooth NumberPad
,但我需要能够串联连接其中的两个。谁能给我指出示例代码?
在 GCController
class 中有一个“controllers
”方法,其中 returns 所有连接的控制器的数组,但没有任何类似的东西 GCKeyboard
.
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
[[UIApplication sharedApplication]setIdleTimerDisabled:YES];
// notifications for keyboard (dis)connect
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWasConnected:) name:GCKeyboardDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWasDisconnected:) name:GCKeyboardDidDisconnectNotification object:nil];
}
GCKeyboard *leftKeyboard;
GCKeyboard *rightKeyboard;
- (void)keyboardWasConnected:(NSNotification *)notification {
if (!self.leftKeyboard) {
leftKeyboard = (GCKeyboard *)notification.object;
NSLog(@"Left Keyboard connected: %@\n", leftKeyboard.description);
NSString *keyboardStatus = [NSString stringWithFormat:@"Left Keyboard CONNECTED:\n%@", leftKeyboard.description];
self.statusLabel.text = keyboardStatus;
self.leftKeyboard = leftKeyboard;
[self reactToKeyboardInput];
}
else if (!self.rightKeyboard) {
NSLog(@"Right Keyboard connected: %@\n", rightKeyboard.description);
rightKeyboard = (GCKeyboard *)notification.object;
NSString *keyboardStatus = [NSString stringWithFormat:@"Right Keyboard CONNECTED:\n%@", rightKeyboard.description];
self.statusLabel.text = keyboardStatus;
self.rightKeyboard = rightKeyboard;
[self reactToKeyboardInput];
}
}
- (void)keyboardWasDisconnected:(NSNotification *)notification {
if (self.leftKeyboard) {
GCKeyboard *keyboard = (GCKeyboard *)notification.object;
NSString *status = [NSString stringWithFormat:@"Left Keyboard DISCONNECTED:\n%@", keyboard.description];
self.statusLabel.text = status;
self.leftKeyboard = nil;
}
else if (self.rightKeyboard) {
GCKeyboard *keyboard = (GCKeyboard *)notification.object;
NSString *status = [NSString stringWithFormat:@"Right Keyboard DISCONNECTED:\n%@", keyboard.description];
self.statusLabel.text = status;
self.rightKeyboard = nil;
}
}
连接键盘或数字键盘后,我收到消息:
2020-09-18 13:09:15.845943-0700 Controller[1653:351628] Left Keyboard connected: GCController 0x280bb8dd0 ('Keyboard' - 0x27c3dc28cec4d818)
“将键盘和鼠标游戏带入 iPad”WWDC 视频抄本指出:
In the case of keyboards, you can't differentiate multiple keyboards, and all keyboard events from multiple keyboards are instead coalesced for you. So while you might use notifications to notice when a keyboard disconnects and perhaps pause the game to prompt for different input, in general, you'll find that just using GCKeyboard-Device.coalesced to check on the key state when non-nil is the right path.
因此,虽然您可能连接了多个键盘,但听起来它们都合并到一个 GCKeyboard
实例中,您无法区分它们。这就是为什么没有 +[GCKeyboard keyboards]
方法的原因。取而代之的是 +[GCKeyboard coalescedKeyboard]