react-native - 在不使用输入字段的情况下确定在 React Native 上按下了哪个键
问题描述
有了 Mac 上硅芯片的机会。我们开始使用 React Native 开发 Mac 应用程序。我们没有使用任何输入字段,但我们需要确定当用户专注于我们的应用程序时从键盘按下了哪个键。基本上我们想在用户使用我们的应用程序时监听所有键盘事件。解决这个问题的最佳方法是什么?
解决方案
如果您在 macOS 上使用 react-native,那么您可能会使用react-native-web
或electron
直接在 Web 浏览器中使用。
这是我的解决方案:
import React, { Component } from 'react';
import { Text, View } from 'react-native';
export default class App extends Component {
handleKeyDown = (e) => {
console.log(`${e.code}`);
// Do your stuff...
};
componentDidMount = () => {
document.onkeydown = this.handleKeyDown;
};
componentWillUnmount = () => {
document.onkeydown = null;
};
render = () => {
return (
<View>
<Text>{'Example'}</Text>
</View>
);
};
}
推荐阅读
- sql-server - 在 SQL Server 2014 中添加 dll 时出错。CLR 有问题
- c# - NuGet 将项目依赖项打包为 nuget 依赖项
- ide - 开发网站的最佳 IDE 是什么?
- php - Docker Toolbox - 仅通过浏览器进行缓慢的 mysql 连接
- symfony - 在 symfony 3 中添加 Google APIs 客户端库
- python - 如果数字低于零,如何将数字设置为零?
- c++ - 即使保证零值,GCC 也会生成非零测试
- javascript - Transform() div 在点击时无法正常工作
- c# - 如何签署 DLL 的文件版本和产品版本?
- spring-boot - 如何使用 @ApiModelProperty 添加多个数据类型