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

2.打开该Objective-C脚本举行编辑,找到关键函数: createKeyboard:
- - (void)createKeyboard
- {
- // only English keyboard layout is supported
- NSString* baseLayout = @"1234567890-=qwertyuiop[]asdfghjkl;'\\`zxcvbnm,./!@#$%^&*()_+{}:"|<>?~ \t\r\b\";
- NSString* numpadLayout = @"1234567890-=*+/.\r";
- NSString* upperCaseLetters = @"QWERTYUIOPASDFGHJKLZXCVBNM";
- size_t sizeOfKeyboardCommands = baseLayout.length + numpadLayout.length + upperCaseLetters.length + 11;
- NSMutableArray* commands = [NSMutableArray arrayWithCapacity: sizeOfKeyboardCommands];
- void (^addKey)(NSString *keyName, UIKeyModifierFlags modifierFlags) = ^(NSString *keyName, UIKeyModifierFlags modifierFlags)
- {
- UIKeyCommand* command = [UIKeyCommand keyCommandWithInput: keyName modifierFlags: modifierFlags action: @selector(handleCommand:)];
- #if UNITY_HAS_IOSSDK_15_0
- if (@available(iOS 15.0, tvOS 15.0, *))
- command.wantsPriorityOverSystemBehavior = YES;
- #endif
- [commands addObject:command];
- };
- for (NSInteger i = 0; i < baseLayout.length; ++i)
- {
- NSString* input = [baseLayout substringWithRange: NSMakeRange(i, 1)];
- NSLog(@"%@ !!!",input);
- addKey(input, kNilOptions);
- }
- for (NSInteger i = 0; i < numpadLayout.length; ++i)
- {
- NSString* input = [numpadLayout substringWithRange: NSMakeRange(i, 1)];
- addKey(input, UIKeyModifierNumericPad);
- }
- for (NSInteger i = 0; i < upperCaseLetters.length; ++i)
- {
- NSString* input = [upperCaseLetters substringWithRange: NSMakeRange(i, 1)];
- addKey(input, UIKeyModifierShift);
- }
- // pageUp, pageDown
- addKey(@"UIKeyInputPageUp", kNilOptions);
- addKey(@"UIKeyInputPageDown", kNilOptions);
- // up, down, left, right, esc
- addKey(UIKeyInputUpArrow, kNilOptions);
- addKey(UIKeyInputDownArrow, kNilOptions);
- addKey(UIKeyInputLeftArrow, kNilOptions);
- addKey(UIKeyInputRightArrow, kNilOptions);
- addKey(UIKeyInputEscape, kNilOptions);
- // caps Lock, shift, control, option, command
- addKey(@"", UIKeyModifierAlphaShift);
- addKey(@"", UIKeyModifierShift);
- addKey(@"", UIKeyModifierControl);
- addKey(@"", UIKeyModifierAlternate);
- addKey(@"", UIKeyModifierCommand);
- keyboardCommands = commands.copy;
- }
复制代码
此函数由Unity定义,通过addKey函数负责初始化注册全部必要响应的按键.
- void (^addKey)(NSString *keyName, UIKeyModifierFlags modifierFlags) = ^(NSString *keyName, UIKeyModifierFlags modifierFlags)
- {
- UIKeyCommand* command = [UIKeyCommand keyCommandWithInput: keyName modifierFlags: modifierFlags action: @selector(handleCommand:)];
- #if UNITY_HAS_IOSSDK_15_0
- if (@available(iOS 15.0, tvOS 15.0, *))
- command.wantsPriorityOverSystemBehavior = YES;
- #endif
- [commands addObject:command];
- };
复制代码
函数接受两个参数第一个是keyName表现接收的按键名称比方键盘上的a-z,第二个参数为UIKeyModifierFlags表现作为Modifier的按键种类如下:
- typedef NS_OPTIONS(NSInteger, UIKeyModifierFlags) {
- UIKeyModifierAlphaShift = 1 << 16, // This bit indicates CapsLock
- UIKeyModifierShift = 1 << 17,
- UIKeyModifierControl = 1 << 18,
- UIKeyModifierAlternate = 1 << 19,
- UIKeyModifierCommand = 1 << 20,
- UIKeyModifierNumericPad = 1 << 21,
- } API_AVAILABLE(ios(7.0));
复制代码
使用时比方用户必要接收处理惩罚command a,需调用addKey(@"a", UIKeyModifierCommand); 针对于特别按键对应的NSString存储于UIResponder.h中:
- // These are pre-defined constants for use with the input property of UIKeyCommand objects.
- UIKIT_EXTERN NSString *const UIKeyInputUpArrow API_AVAILABLE(ios(7.0));
- UIKIT_EXTERN NSString *const UIKeyInputDownArrow API_AVAILABLE(ios(7.0));
- UIKIT_EXTERN NSString *const UIKeyInputLeftArrow API_AVAILABLE(ios(7.0));
- UIKIT_EXTERN NSString *const UIKeyInputRightArrow API_AVAILABLE(ios(7.0));
- UIKIT_EXTERN NSString *const UIKeyInputEscape API_AVAILABLE(ios(7.0));
- UIKIT_EXTERN NSString *const UIKeyInputPageUp API_AVAILABLE(ios(8.0));
- UIKIT_EXTERN NSString *const UIKeyInputPageDown API_AVAILABLE(ios(8.0));
- UIKIT_EXTERN NSString *const UIKeyInputHome API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputEnd API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF1 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF1 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF2 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF3 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF4 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF5 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF6 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF7 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF8 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF9 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF10 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF11 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputF12 API_AVAILABLE(ios(13.4), tvos(13.4)) API_UNAVAILABLE(watchos);
- UIKIT_EXTERN NSString *const UIKeyInputDelete API_AVAILABLE(ios(15.0), tvos(15.0)) API_UNAVAILABLE(watchos);
复制代码
此文档以F1-F12按键添加为例:使用addKey(UIKeyInputF4, kNilOptions);
- addKey(UIKeyInputF1, kNilOptions);
- addKey(UIKeyInputF2, kNilOptions);
- addKey(UIKeyInputF3, kNilOptions);
- addKey(UIKeyInputF4, kNilOptions);
复制代码
- 注册好按键后还必要在handleCommand函数中举行响应处理惩罚:
函数中input表现createKeyboard函数注册对应的keyName,modifierFlags表现注册时传入的modifierFlags.函数中的Code对应Unity的KeyCodeEnum:
- using System;
- namespace UnityEngine
- {
- // Key codes returned by Event.keyCode. These map directly to a physical key on the keyboard.
- public enum KeyCode
- {
- // Not assigned (never returned as the result of a keystroke)
- None = 0,
- // The backspace key
- Backspace = 8,
- // The forward delete key
- Delete = 127,
- // The tab key
- Tab = 9,
- // The Clear key
- Clear = 12,
- // Return key
- Return = 13,
- // Pause on PC machines
- Pause = 19,
- // Escape key
- Escape = 27,
- // Space key
- Space = 32,
- // Numeric keypad 0
- Keypad0 = 256,
- // Numeric keypad 1
- Keypad1 = 257,
复制代码 前添加判断并给code举行赋值即可,F1-F12对应282-294以此为例代码如下:
- if (input == UIKeyInputF1)
- code = 282;
- else if (input == UIKeyInputF2)
- code = 283;
- else if (input == UIKeyInputF3)
- code = 284;
复制代码 [code][/code]
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |