Unity 如何在 iOS 新增键盘 KeyCode 响应事件
1.定位到文件UnityView+Keyboard.mm同如下路径:

2.打开该Objective-C脚本进行编辑,找到关键函数: createKeyboard:
- (void)createKeyboard
{// only English keyboard layout is supportedNSString* 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_0if (@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, pageDownaddKey(@"UIKeyInputPageUp", kNilOptions);addKey(@"UIKeyInputPageDown", kNilOptions);// up, down, left, right, escaddKey(UIKeyInputUpArrow, kNilOptions);addKey(UIKeyInputDownArrow, kNilOptions);addKey(UIKeyInputLeftArrow, kNilOptions);addKey(UIKeyInputRightArrow, kNilOptions);addKey(UIKeyInputEscape, kNilOptions);// caps Lock, shift, control, option, commandaddKey(@"", 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_0if (@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 CapsLockUIKeyModifierShift = 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函数中进行响应处理:
- (void)handleCommand:(UIKeyCommand *)command
{NSString* input = command.input;UIKeyModifierFlags modifierFlags = command.modifierFlags;char inputChar = ([input length] > 0) ? [input characterAtIndex: 0] : 0;int code = (int)inputChar; // ASCII codeUnitySendKeyboardCommand(command);if (![self isValidCodeForButton: code]){code = 0;}if ((modifierFlags & UIKeyModifierAlphaShift) != 0)code = UnityStringToKey("caps lock");if ((modifierFlags & UIKeyModifierShift) != 0)code = UnityStringToKey("left shift");if ((modifierFlags & UIKeyModifierControl) != 0)code = UnityStringToKey("left ctrl");if ((modifierFlags & UIKeyModifierAlternate) != 0)code = UnityStringToKey("left alt");if ((modifierFlags & UIKeyModifierCommand) != 0)code = UnityStringToKey("left cmd");if ((modifierFlags & UIKeyModifierNumericPad) != 0){switch (inputChar){case '0':code = UnityStringToKey("[0]");break;case '1':code = UnityStringToKey("[1]");break;case '2':code = UnityStringToKey("[2]");break;case '3':code = UnityStringToKey("[3]");break;case '4':code = UnityStringToKey("[4]");break;case '5':code = UnityStringToKey("[5]");break;case '6':code = UnityStringToKey("[6]");break;case '7':code = UnityStringToKey("[7]");break;case '8':code = UnityStringToKey("[8]");break;case '9':code = UnityStringToKey("[9]");break;case '-':code = UnityStringToKey("[-]");break;case '=':code = UnityStringToKey("equals");break;case '*':code = UnityStringToKey("[*]");break;case '+':code = UnityStringToKey("[+]");break;case '/':code = UnityStringToKey("[/]");break;case '.':code = UnityStringToKey("[.]");break;case '\r':code = UnityStringToKey("enter");break;default:break;}}if (input == UIKeyInputUpArrow)code = UnityStringToKey("up");else if (input == UIKeyInputDownArrow)code = UnityStringToKey("down");else if (input == UIKeyInputRightArrow)code = UnityStringToKey("right");else if (input == UIKeyInputLeftArrow)code = UnityStringToKey("left");else if (input == UIKeyInputEscape)code = UnityStringToKey("escape");else if ([input isEqualToString: @"UIKeyInputPageUp"])code = UnityStringToKey("page up");else if ([input isEqualToString: @"UIKeyInputPageDown"])code = UnityStringToKey("page down");KeyMap::iterator item = GetKeyMap().find(code);if (item == GetKeyMap().end()){// New key is down, register it and its timeUnitySetKeyboardKeyState(code, true);GetKeyMap()[code] = GetTimeInSeconds();}else{// Still holding the key, update its timeitem->second = GetTimeInSeconds();}
}
函数中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 keyBackspace = 8,// The forward delete keyDelete = 127,// The tab keyTab = 9,// The Clear keyClear = 12,// Return keyReturn = 13,// Pause on PC machinesPause = 19,// Escape keyEscape = 27,// Space keySpace = 32,// Numeric keypad 0Keypad0 = 256,// Numeric keypad 1Keypad1 = 257,
前添加判断并给code进行赋值即可,F1-F12对应282-294以此为例代码如下:
if (input == UIKeyInputF1)code = 282;else if (input == UIKeyInputF2)code = 283;else if (input == UIKeyInputF3)code = 284;
-
测试结果如下:

相关文章:
Unity 如何在 iOS 新增键盘 KeyCode 响应事件
1.定位到文件UnityViewKeyboard.mm同如下路径: 2.打开该Objective-C脚本进行编辑,找到关键函数: createKeyboard: - (void)createKeyboard {// only English keyboard layout is supportedNSString* baseLayout "1234567890-qwertyuiop[]asdfghjkl;\\zxcvbnm,./!#$%^&am…...
pytorh学习笔记——波士顿房价预测
机器学习的“hello world”:波士顿房价预测 波士顿房价预测的背景不用提了,简单了解一下数据集的结构。 波士顿房价的数据集,共有506组数据,每组数据共14项,前13项是影响房价的各种因素,比如&…...
让AI像人一样思考和使用工具,reAct机制详解
reAct机制详解 reAct是什么reAct的关键要素reAct的思维过程reAct的代码实现查看效果引入依赖,定义模型定义相关工具集合工具创建代理启动测试完整代码 思考 reAct是什么 reAct的核心思想是将**推理(Reasoning)和行动(Acting&…...
Linux系列-常见的指令(二)
🌈个人主页: 羽晨同学 💫个人格言:“成为自己未来的主人~” mv 剪切文件,目录 重命名 比如说,我们在最开始创建一个新的文件hello.txt 然后我们将这个文件改一个名字,改成world.txt 所以,…...
Leecode刷题之路第17天之电话号码的字母组合
题目出处 17-电话号码的字母组合-题目出处 题目描述 个人解法 思路: todo 代码示例:(Java) todo复杂度分析 todo 官方解法 17-电话号码的字母组合-官方解法 方法1:回溯 思路: 代码示例:&a…...
2023牛客暑期多校训练营3(题解)
今天下午也是小小的做了一下,OI,也是感觉手感火热啊,之前无意间看到的那个哥德巴赫定理今天就用到了,我以为根本用不到的,当时也只是感兴趣看了一眼,还是比较激动啊 话不多说,直接开始看题 Wo…...
Magnum IO
NVIDIA Magnum IO 文章目录 前言加速数据中心 IO 性能,随时随地助力 AINVIDIA Magnum IO 优化堆栈1. 存储 IO2. 网络 IO3. 网内计算4. IO 管理跨数据中心应用加速 IO1. 数据分析Magnum IO 库和数据分析工具2. 高性能计算Magnum IO 库和 HPC 应用3. 深度学习Magnum IO 库和深度…...
Flink job的提交流程
在Flink中,作业(Job)的提交流程是一个复杂的过程,涉及多个组件和模块,包括作业的编译、优化、序列化、任务分发、任务调度、资源分配等。Flink通过分布式架构来管理作业的生命周期,确保作业在不同节点上以高…...
git操作pull的时候出现冲突怎么解决
问: PS C:\Users\fury_123\Desktop\consumptionforecast> git branch * dev main PS C:\Users\fury_123\Desktop\consumptionforecast> git add . PS C:\Users\fury_123\Desktop\consumptionforecast> git commit -m 修改部分样式 [dev 74693e0] 修改部分样…...
Sentinel 1.80(CVE-2021-44139)
Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性 Report a Sentinel Security Vulnerability …...
黑马程序员C++提高编程学习笔记
黑马程序员C提高编程 提高阶段主要针对泛型编程和STL技术 文章目录 黑马程序员C提高编程一、模板1.1 函数模板1.1.1 函数模板基础知识 案例一: 数组排序1.2.1 普通函数与函数模板1.2.2 函数模板的局限性 1.2 类模板1.2.1 类模板的基础知识1.2.2 类模板与函数模板1.…...
力扣第1题:两数之和(图解版)
Golang版本 func twoSum(nums []int, target int) []int {m : make(map[int]int)for i : range nums {if _, ok : m[target - nums[i]]; ok {return []int{i, m[target - nums[i]]}} m[nums[i]] i}return nil }...
aws(学习笔记第三课) AWS CloudFormation
aws(学习笔记第三课) 使用AWS CloudFormation 学习内容: AWS CloudFormation的模板解析使用AWS CloudFormation启动ec2 server 1. AWS CloudFormation 的模版解析 CloudFormation模板结构 CloudFormation是AWS的配置管理工具,属于Infrastructure as Co…...
浅学React和JSX
往期推荐 一文搞懂大数据流式计算引擎Flink【万字详解,史上最全】-CSDN博客 数仓架构:离线数仓、实时数仓Lambda和Kappa、湖仓一体数据湖-CSDN博客 一文入门大数据准流式计算引擎Spark【万字详解,全网最新】_大数据 spark-CSDN博客 浅谈维度建…...
React 为什么 “虚拟 DOM 顶部有很多 provider“?
1、介绍React中的Context Provider 在 React 中,虚拟 DOM(Virtual DOM)是 React 用来高效更新 UI 的核心机制,它通过对比前后两次虚拟 DOM 树,确定哪些部分需要更新,以减少直接操作真实 DOM 的开销。而 “…...
忘记了 MySQL 8.0 的 root 密码,应该怎么办?
如果你忘记了 MySQL 8.0 的 root 密码,可以通过以下步骤来重置密码。请注意,这些步骤需要你有对 MySQL 服务器的物理或命令行访问权限。 步骤 1: 停止 MySQL 服务 首先,你需要停止正在运行的 MySQL 服务。你可以使用以下命令来停止 MySQL 服…...
Promise.reject()
Promise.reject() 静态方法返回一个已拒绝(rejected)的 Promise 对象,拒绝原因为给定的参数。 语法 Promise.reject(reason)参数 reason 该 Promise 对象被拒绝的原因。 返回值 返回一个已拒绝(rejected)的 Promi…...
大数据-163 Apache Kylin 全量增量Cube的构建 手动触发合并 JDBC 操作 Scala
点一下关注吧!!!非常感谢!!持续更新!!! 目前已经更新到了: Hadoop(已更完)HDFS(已更完)MapReduce(已更完&am…...
云手机与传统手机的区别是什么?
随着科技的快速进步,云手机逐渐成为手机市场的热门选择。与传统的智能手机相比,云手机具有许多独特的功能和优势,尤其在多账号管理和高效操作方面备受关注。那么,云手机究竟与普通手机有哪些区别呢? 1. 更灵活的操作与…...
微知-Bluefield DPU命名规则各字段作用?BF2 BF3全系列命名大全
文章目录 背景字段命名C是bmc的意思NOT的N是是否加密S表示不加密但是secureboot enable倒数第四个都是E倒数第五个是速率 V和H是200GM表示E serials,H表示P serials(区别参考兄弟篇:[more](https://blog.csdn.net/essencelite/article/detail…...
接口测试中缓存处理策略
在接口测试中,缓存处理策略是一个关键环节,直接影响测试结果的准确性和可靠性。合理的缓存处理策略能够确保测试环境的一致性,避免因缓存数据导致的测试偏差。以下是接口测试中常见的缓存处理策略及其详细说明: 一、缓存处理的核…...
c++ 面试题(1)-----深度优先搜索(DFS)实现
操作系统:ubuntu22.04 IDE:Visual Studio Code 编程语言:C11 题目描述 地上有一个 m 行 n 列的方格,从坐标 [0,0] 起始。一个机器人可以从某一格移动到上下左右四个格子,但不能进入行坐标和列坐标的数位之和大于 k 的格子。 例…...
el-switch文字内置
el-switch文字内置 效果 vue <div style"color:#ffffff;font-size:14px;float:left;margin-bottom:5px;margin-right:5px;">自动加载</div> <el-switch v-model"value" active-color"#3E99FB" inactive-color"#DCDFE6"…...
鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院查看报告小程序
一、开发环境准备 工具安装: 下载安装DevEco Studio 4.0(支持HarmonyOS 5)配置HarmonyOS SDK 5.0确保Node.js版本≥14 项目初始化: ohpm init harmony/hospital-report-app 二、核心功能模块实现 1. 报告列表…...
【Zephyr 系列 10】实战项目:打造一个蓝牙传感器终端 + 网关系统(完整架构与全栈实现)
🧠关键词:Zephyr、BLE、终端、网关、广播、连接、传感器、数据采集、低功耗、系统集成 📌目标读者:希望基于 Zephyr 构建 BLE 系统架构、实现终端与网关协作、具备产品交付能力的开发者 📊篇幅字数:约 5200 字 ✨ 项目总览 在物联网实际项目中,**“终端 + 网关”**是…...
《基于Apache Flink的流处理》笔记
思维导图 1-3 章 4-7章 8-11 章 参考资料 源码: https://github.com/streaming-with-flink 博客 https://flink.apache.org/bloghttps://www.ververica.com/blog 聚会及会议 https://flink-forward.orghttps://www.meetup.com/topics/apache-flink https://n…...
Unit 1 深度强化学习简介
Deep RL Course ——Unit 1 Introduction 从理论和实践层面深入学习深度强化学习。学会使用知名的深度强化学习库,例如 Stable Baselines3、RL Baselines3 Zoo、Sample Factory 和 CleanRL。在独特的环境中训练智能体,比如 SnowballFight、Huggy the Do…...
CMake控制VS2022项目文件分组
我们可以通过 CMake 控制源文件的组织结构,使它们在 VS 解决方案资源管理器中以“组”(Filter)的形式进行分类展示。 🎯 目标 通过 CMake 脚本将 .cpp、.h 等源文件分组显示在 Visual Studio 2022 的解决方案资源管理器中。 ✅ 支持的方法汇总(共4种) 方法描述是否推荐…...
安宝特方案丨船舶智造的“AR+AI+作业标准化管理解决方案”(装配)
船舶制造装配管理现状:装配工作依赖人工经验,装配工人凭借长期实践积累的操作技巧完成零部件组装。企业通常制定了装配作业指导书,但在实际执行中,工人对指导书的理解和遵循程度参差不齐。 船舶装配过程中的挑战与需求 挑战 (1…...
排序算法总结(C++)
目录 一、稳定性二、排序算法选择、冒泡、插入排序归并排序随机快速排序堆排序基数排序计数排序 三、总结 一、稳定性 排序算法的稳定性是指:同样大小的样本 **(同样大小的数据)**在排序之后不会改变原始的相对次序。 稳定性对基础类型对象…...
