Unity 如安在 iOS 新增键盘 KeyCode 响应事件

打印 上一主题 下一主题

主题 888|帖子 888|积分 2664

1.定位到文件UnityView+Keyboard.mm同如下路径:


2.打开该Objective-C脚本举行编辑,找到关键函数: createKeyboard:
  1. - (void)createKeyboard
  2. {
  3.     // only English keyboard layout is supported
  4.     NSString* baseLayout = @"1234567890-=qwertyuiop[]asdfghjkl;'\\`zxcvbnm,./!@#$%^&*()_+{}:"|<>?~ \t\r\b\";
  5.     NSString* numpadLayout = @"1234567890-=*+/.\r";
  6.     NSString* upperCaseLetters = @"QWERTYUIOPASDFGHJKLZXCVBNM";
  7.     size_t sizeOfKeyboardCommands = baseLayout.length + numpadLayout.length + upperCaseLetters.length + 11;
  8.     NSMutableArray* commands = [NSMutableArray arrayWithCapacity: sizeOfKeyboardCommands];
  9.     void (^addKey)(NSString *keyName, UIKeyModifierFlags modifierFlags) = ^(NSString *keyName, UIKeyModifierFlags modifierFlags)
  10.     {
  11.         UIKeyCommand* command = [UIKeyCommand keyCommandWithInput: keyName modifierFlags: modifierFlags action: @selector(handleCommand:)];
  12. #if UNITY_HAS_IOSSDK_15_0
  13.         if (@available(iOS 15.0, tvOS 15.0, *))
  14.             command.wantsPriorityOverSystemBehavior = YES;
  15. #endif
  16.         [commands addObject:command];
  17.     };
  18.     for (NSInteger i = 0; i < baseLayout.length; ++i)
  19.     {
  20.         NSString* input = [baseLayout substringWithRange: NSMakeRange(i, 1)];
  21.         NSLog(@"%@ !!!",input);
  22.         addKey(input, kNilOptions);
  23.     }
  24.     for (NSInteger i = 0; i < numpadLayout.length; ++i)
  25.     {
  26.         NSString* input = [numpadLayout substringWithRange: NSMakeRange(i, 1)];
  27.         addKey(input, UIKeyModifierNumericPad);
  28.     }
  29.     for (NSInteger i = 0; i < upperCaseLetters.length; ++i)
  30.     {
  31.         NSString* input = [upperCaseLetters substringWithRange: NSMakeRange(i, 1)];
  32.         addKey(input, UIKeyModifierShift);
  33.     }
  34.     // pageUp, pageDown
  35.     addKey(@"UIKeyInputPageUp", kNilOptions);
  36.     addKey(@"UIKeyInputPageDown", kNilOptions);
  37.     // up, down, left, right, esc
  38.     addKey(UIKeyInputUpArrow, kNilOptions);
  39.     addKey(UIKeyInputDownArrow, kNilOptions);
  40.     addKey(UIKeyInputLeftArrow, kNilOptions);
  41.     addKey(UIKeyInputRightArrow, kNilOptions);
  42.     addKey(UIKeyInputEscape, kNilOptions);
  43.     // caps Lock, shift, control, option, command
  44.     addKey(@"", UIKeyModifierAlphaShift);
  45.     addKey(@"", UIKeyModifierShift);
  46.     addKey(@"", UIKeyModifierControl);
  47.     addKey(@"", UIKeyModifierAlternate);
  48.     addKey(@"", UIKeyModifierCommand);
  49.     keyboardCommands = commands.copy;
  50. }
复制代码

此函数由Unity定义,通过addKey函数负责初始化注册全部必要响应的按键.
  1.    void (^addKey)(NSString *keyName, UIKeyModifierFlags modifierFlags) = ^(NSString *keyName, UIKeyModifierFlags modifierFlags)
  2.     {
  3.         UIKeyCommand* command = [UIKeyCommand keyCommandWithInput: keyName modifierFlags: modifierFlags action: @selector(handleCommand:)];
  4. #if UNITY_HAS_IOSSDK_15_0
  5.         if (@available(iOS 15.0, tvOS 15.0, *))
  6.             command.wantsPriorityOverSystemBehavior = YES;
  7. #endif
  8.         [commands addObject:command];
  9.     };
复制代码

函数接受两个参数第一个是keyName表现接收的按键名称比方键盘上的a-z,第二个参数为UIKeyModifierFlags表现作为Modifier的按键种类如下:
  1.   typedef   NS_OPTIONS(NSInteger, UIKeyModifierFlags) {
  2.       UIKeyModifierAlphaShift     = 1 << 16,  // This bit indicates CapsLock
  3.       UIKeyModifierShift          = 1 << 17,
  4.       UIKeyModifierControl        = 1 << 18,
  5.       UIKeyModifierAlternate      = 1 << 19,
  6.       UIKeyModifierCommand        = 1 << 20,
  7.       UIKeyModifierNumericPad     = 1 << 21,
  8.   } API_AVAILABLE(ios(7.0));
复制代码

使用时比方用户必要接收处理惩罚command a,需调用addKey(@"a", UIKeyModifierCommand); 针对于特别按键对应的NSString存储于UIResponder.h中:
  1. // These are pre-defined constants for use with the input property of UIKeyCommand objects.
  2. UIKIT_EXTERN NSString *const UIKeyInputUpArrow         API_AVAILABLE(ios(7.0));
  3. UIKIT_EXTERN NSString *const UIKeyInputDownArrow       API_AVAILABLE(ios(7.0));
  4. UIKIT_EXTERN NSString *const UIKeyInputLeftArrow       API_AVAILABLE(ios(7.0));
  5. UIKIT_EXTERN NSString *const UIKeyInputRightArrow      API_AVAILABLE(ios(7.0));
  6. UIKIT_EXTERN NSString *const UIKeyInputEscape          API_AVAILABLE(ios(7.0));
  7. UIKIT_EXTERN NSString *const UIKeyInputPageUp          API_AVAILABLE(ios(8.0));
  8. UIKIT_EXTERN NSString *const UIKeyInputPageDown        API_AVAILABLE(ios(8.0));
  9. UIKIT_EXTERN NSString *const UIKeyInputHome            API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  10. UIKIT_EXTERN NSString *const UIKeyInputEnd             API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  11. UIKIT_EXTERN NSString *const UIKeyInputF1              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  12. UIKIT_EXTERN NSString *const UIKeyInputF1              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  13. UIKIT_EXTERN NSString *const UIKeyInputF2              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  14. UIKIT_EXTERN NSString *const UIKeyInputF3              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  15. UIKIT_EXTERN NSString *const UIKeyInputF4              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  16. UIKIT_EXTERN NSString *const UIKeyInputF5              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  17. UIKIT_EXTERN NSString *const UIKeyInputF6              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  18. UIKIT_EXTERN NSString *const UIKeyInputF7              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  19. UIKIT_EXTERN NSString *const UIKeyInputF8              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  20. UIKIT_EXTERN NSString *const UIKeyInputF9              API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  21. UIKIT_EXTERN NSString *const UIKeyInputF10             API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  22. UIKIT_EXTERN NSString *const UIKeyInputF11             API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  23. UIKIT_EXTERN NSString *const UIKeyInputF12             API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
  24. UIKIT_EXTERN NSString *const UIKeyInputDelete          API_AVAILABLE(ios(15.0), tvos(15.0)) API_UNAVAILABLE(watchos);
复制代码

此文档以F1-F12按键添加为例:使用addKey(UIKeyInputF4, kNilOptions);
  1.     addKey(UIKeyInputF1, kNilOptions);
  2.     addKey(UIKeyInputF2, kNilOptions);
  3.     addKey(UIKeyInputF3, kNilOptions);
  4.     addKey(UIKeyInputF4, kNilOptions);
复制代码

  • 注册好按键后还必要在handleCommand函数中举行响应处理惩罚:
  1. - (void)handleCommand:(UIKeyCommand *)command
  2. {
  3.     NSString* input = command.input;
  4.     UIKeyModifierFlags modifierFlags = command.modifierFlags;
  5.     char inputChar = ([input length] > 0) ? [input characterAtIndex: 0] : 0;
  6.     int code = (int)inputChar; // ASCII code
  7.     UnitySendKeyboardCommand(command);
  8.     if (![self isValidCodeForButton: code])
  9.     {
  10.         code = 0;
  11.     }
  12.     if ((modifierFlags & UIKeyModifierAlphaShift) != 0)
  13.         code = UnityStringToKey("caps lock");
  14.     if ((modifierFlags & UIKeyModifierShift) != 0)
  15.         code = UnityStringToKey("left shift");
  16.     if ((modifierFlags & UIKeyModifierControl) != 0)
  17.         code = UnityStringToKey("left ctrl");
  18.     if ((modifierFlags & UIKeyModifierAlternate) != 0)
  19.         code = UnityStringToKey("left alt");
  20.     if ((modifierFlags & UIKeyModifierCommand) != 0)
  21.         code = UnityStringToKey("left cmd");
  22.     if ((modifierFlags & UIKeyModifierNumericPad) != 0)
  23.     {
  24.         switch (inputChar)
  25.         {
  26.             case '0':
  27.                 code = UnityStringToKey("[0]");
  28.                 break;
  29.             case '1':
  30.                 code = UnityStringToKey("[1]");
  31.                 break;
  32.             case '2':
  33.                 code = UnityStringToKey("[2]");
  34.                 break;
  35.             case '3':
  36.                 code = UnityStringToKey("[3]");
  37.                 break;
  38.             case '4':
  39.                 code = UnityStringToKey("[4]");
  40.                 break;
  41.             case '5':
  42.                 code = UnityStringToKey("[5]");
  43.                 break;
  44.             case '6':
  45.                 code = UnityStringToKey("[6]");
  46.                 break;
  47.             case '7':
  48.                 code = UnityStringToKey("[7]");
  49.                 break;
  50.             case '8':
  51.                 code = UnityStringToKey("[8]");
  52.                 break;
  53.             case '9':
  54.                 code = UnityStringToKey("[9]");
  55.                 break;
  56.             case '-':
  57.                 code = UnityStringToKey("[-]");
  58.                 break;
  59.             case '=':
  60.                 code = UnityStringToKey("equals");
  61.                 break;
  62.             case '*':
  63.                 code = UnityStringToKey("[*]");
  64.                 break;
  65.             case '+':
  66.                 code = UnityStringToKey("[+]");
  67.                 break;
  68.             case '/':
  69.                 code = UnityStringToKey("[/]");
  70.                 break;
  71.             case '.':
  72.                 code = UnityStringToKey("[.]");
  73.                 break;
  74.             case '\r':
  75.                 code = UnityStringToKey("enter");
  76.                 break;
  77.             default:
  78.                 break;
  79.         }
  80.     }
  81.     if (input == UIKeyInputUpArrow)
  82.         code = UnityStringToKey("up");
  83.     else if (input == UIKeyInputDownArrow)
  84.         code = UnityStringToKey("down");
  85.     else if (input == UIKeyInputRightArrow)
  86.         code = UnityStringToKey("right");
  87.     else if (input == UIKeyInputLeftArrow)
  88.         code = UnityStringToKey("left");
  89.     else if (input == UIKeyInputEscape)
  90.         code = UnityStringToKey("escape");
  91.     else if ([input isEqualToString: @"UIKeyInputPageUp"])
  92.         code = UnityStringToKey("page up");
  93.     else if ([input isEqualToString: @"UIKeyInputPageDown"])
  94.         code = UnityStringToKey("page down");
  95.     KeyMap::iterator item = GetKeyMap().find(code);
  96.     if (item == GetKeyMap().end())
  97.     {
  98.         // New key is down, register it and its time
  99.         UnitySetKeyboardKeyState(code, true);
  100.         GetKeyMap()[code] = GetTimeInSeconds();
  101.     }
  102.     else
  103.     {
  104.         // Still holding the key, update its time
  105.         item->second = GetTimeInSeconds();
  106.     }
  107. }
复制代码
函数中input表现createKeyboard函数注册对应的keyName,modifierFlags表现注册时传入的modifierFlags.函数中的Code对应Unity的KeyCodeEnum:
  1. using System;
  2. namespace UnityEngine
  3. {
  4.     // Key codes returned by Event.keyCode. These map directly to a physical key on the keyboard.
  5.     public enum KeyCode
  6.     {
  7.         // Not assigned (never returned as the result of a keystroke)
  8.         None = 0,
  9.         // The backspace key
  10.         Backspace       = 8,
  11.         // The forward delete key
  12.         Delete      = 127,
  13.         // The tab key
  14.         Tab     = 9,
  15.         // The Clear key
  16.         Clear       = 12,
  17.         // Return key
  18.         Return      = 13,
  19.         // Pause on PC machines
  20.         Pause       = 19,
  21.         // Escape key
  22.         Escape      = 27,
  23.         // Space key
  24.         Space       = 32,
  25.         // Numeric keypad 0
  26.         Keypad0     = 256,
  27.         // Numeric keypad 1
  28.         Keypad1     = 257,
复制代码
前添加判断并给code举行赋值即可,F1-F12对应282-294以此为例代码如下:
  1.    if (input == UIKeyInputF1)
  2.         code = 282;
  3.     else if (input == UIKeyInputF2)
  4.         code = 283;
  5.     else if (input == UIKeyInputF3)
  6.         code = 284;
复制代码
[code][/code]

  • 测试效果如下:



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

吴旭华

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表