Skip to content

Commit a8f7ef5

Browse files
committed
Initial commit
0 parents  commit a8f7ef5

10 files changed

Lines changed: 6548 additions & 0 deletions

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @generated by expo-module-scripts
2+
module.exports = require('expo-module-scripts/eslintrc.base.js');

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.DS_Store

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# react-native-keyevent
2+
3+
Config plugin to auto configure react-native-keyevent on prebuild
4+
5+
# API documentation
6+
7+
- [Documentation for the main branch](https://github.com/expo/expo/blob/main/docs/pages/versions/unversioned/sdk/react-native-keyevent.md)
8+
- [Documentation for the latest stable release](https://docs.expo.dev/versions/latest/sdk/react-native-keyevent/)
9+
10+
# Installation in managed Expo projects
11+
12+
For [managed](https://docs.expo.dev/versions/latest/introduction/managed-vs-bare/) Expo projects, please follow the installation instructions in the [API documentation for the latest stable release](#api-documentation). If you follow the link and there is no documentation available then this library is not yet usable within managed projects — it is likely to be included in an upcoming Expo SDK release.
13+
14+
# Installation in bare React Native projects
15+
16+
For bare React Native projects, you must ensure that you have [installed and configured the `expo` package](https://docs.expo.dev/bare/installing-expo-modules/) before continuing.
17+
18+
### Add the package to your npm dependencies
19+
20+
```
21+
npm install react-native-keyevent
22+
```
23+
24+
25+
26+
27+
# Contributing
28+
29+
Contributions are very welcome! Please refer to guidelines described in the [contributing guide]( https://github.com/expo/expo#contributing).

app.plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./build/withReactNativeKeyevent");

build/withReactNativeKeyevent.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { ConfigPlugin } from "@expo/config-plugins";
2+
declare const initPlugin: ConfigPlugin;
3+
export default initPlugin;

build/withReactNativeKeyevent.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const config_plugins_1 = require("@expo/config-plugins");
4+
const generateCode_1 = require("@expo/config-plugins/build/utils/generateCode");
5+
const withIosAppDelegateImport = (config) => {
6+
// @ts-ignore
7+
const newConfig = (0, config_plugins_1.withAppDelegate)(config, (config) => {
8+
const newSrc = ['#import <RNKeyEvent.h>'];
9+
const newConfig = (0, generateCode_1.mergeContents)({
10+
tag: 'react-native-keyevent-import',
11+
src: config.modResults.contents,
12+
newSrc: newSrc.join('\n'),
13+
anchor: `#import "AppDelegate.h"`,
14+
offset: 1,
15+
comment: '//',
16+
});
17+
return {
18+
...config,
19+
modResults: newConfig,
20+
};
21+
});
22+
return newConfig;
23+
};
24+
const withIosAppDelegateBody = (config) => {
25+
// @ts-ignore
26+
const newConfig = (0, config_plugins_1.withAppDelegate)(config, (config) => {
27+
const newSrc = [
28+
'RNKeyEvent *keyEvent = nil;',
29+
' ',
30+
'- (NSMutableArray<UIKeyCommand *> *)keyCommands {',
31+
' NSMutableArray *keys = [NSMutableArray new];',
32+
' ',
33+
' if (keyEvent == nil) {',
34+
' keyEvent = [[RNKeyEvent alloc] init];',
35+
' }',
36+
' ',
37+
' if ([keyEvent isListening]) {',
38+
' NSArray *namesArray = [[keyEvent getKeys] componentsSeparatedByString:@","];',
39+
' ',
40+
' NSCharacterSet *validChars = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"];',
41+
' ',
42+
' for (NSString* names in namesArray) {',
43+
' NSRange range = [names rangeOfCharacterFromSet:validChars];',
44+
' ',
45+
' if (NSNotFound != range.location) {',
46+
' [keys addObject: [UIKeyCommand keyCommandWithInput:names modifierFlags:UIKeyModifierShift action:@selector(keyInput:)]];',
47+
' } else {',
48+
' [keys addObject: [UIKeyCommand keyCommandWithInput:names modifierFlags:0 action:@selector(keyInput:)]];',
49+
' }',
50+
' }',
51+
' }',
52+
' ',
53+
' return keys;',
54+
'}',
55+
'',
56+
'- (void)keyInput:(UIKeyCommand *)sender {',
57+
' NSString *selected = sender.input;',
58+
' [keyEvent sendKeyEvent:selected];',
59+
'}',
60+
];
61+
const newConfig = (0, generateCode_1.mergeContents)({
62+
tag: 'react-native-keyevent-body',
63+
src: config.modResults.contents,
64+
newSrc: newSrc.join('\n'),
65+
anchor: `@implementation AppDelegate`,
66+
offset: 1,
67+
comment: '//',
68+
});
69+
return {
70+
...config,
71+
modResults: newConfig,
72+
};
73+
});
74+
return newConfig;
75+
};
76+
const withAndroidMainActivityImport = (config) => {
77+
// @ts-ignore
78+
const newConfig = (0, config_plugins_1.withMainActivity)(config, (config) => {
79+
const newSrc = [
80+
'import android.view.KeyEvent;',
81+
'import com.github.kevinejohn.keyevent.KeyEventModule;',
82+
];
83+
const newConfig = (0, generateCode_1.mergeContents)({
84+
tag: 'react-native-keyevent-import',
85+
src: config.modResults.contents,
86+
newSrc: newSrc.join('\n'),
87+
anchor: `package com.morrow.flint;`,
88+
offset: 1,
89+
comment: '//',
90+
});
91+
return {
92+
...config,
93+
modResults: newConfig,
94+
};
95+
});
96+
return newConfig;
97+
};
98+
const withAndroidMainActivityBody = (config) => {
99+
// @ts-ignore
100+
const newConfig = (0, config_plugins_1.withMainActivity)(config, (config) => {
101+
const newSrc = [
102+
'@Override',
103+
'public boolean onKeyDown(int keyCode, KeyEvent event) {',
104+
'',
105+
' // // Uncomment this is key events should only trigger once when key is held down',
106+
' // if (event.getRepeatCount() == 0) {',
107+
' // KeyEventModule.getInstance().onKeyDownEvent(keyCode, event);',
108+
' // }',
109+
'',
110+
' // // This will trigger the key repeat if the key is held down',
111+
' // // Comment this out if uncommenting the above',
112+
' KeyEventModule.getInstance().onKeyDownEvent(keyCode, event);',
113+
'',
114+
' // // Uncomment this if you want the default keyboard behavior',
115+
' // return super.onKeyDown(keyCode, event);',
116+
'',
117+
' // // The default keyboard behaviour wll be overridden',
118+
' // // This is similar to what e.preventDefault() does in a browser',
119+
' // // comment this if uncommenting the above',
120+
' super.onKeyDown(keyCode, event);',
121+
' return true;',
122+
'}',
123+
'',
124+
'@Override',
125+
'public boolean onKeyUp(int keyCode, KeyEvent event) {',
126+
' KeyEventModule.getInstance().onKeyUpEvent(keyCode, event);',
127+
'',
128+
' // // Uncomment this if you want the default keyboard behavior',
129+
' // return super.onKeyUp(keyCode, event);',
130+
'',
131+
' // // The default keyboard behaviour wll be overridden',
132+
' // // This is similar to what e.preventDefault() does in a browser',
133+
' // // comment this if uncommenting the above',
134+
' super.onKeyUp(keyCode, event);',
135+
' return true;',
136+
'}',
137+
'',
138+
'@Override',
139+
'public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {',
140+
' KeyEventModule.getInstance().onKeyMultipleEvent(keyCode, repeatCount, event);',
141+
' return super.onKeyMultiple(keyCode, repeatCount, event);',
142+
'}',
143+
];
144+
const newConfig = (0, generateCode_1.mergeContents)({
145+
tag: 'react-native-keyevent-body',
146+
src: config.modResults.contents,
147+
newSrc: newSrc.join('\n'),
148+
anchor: `public class MainActivity extends ReactActivity {`,
149+
offset: 1,
150+
comment: '//',
151+
});
152+
return {
153+
...config,
154+
modResults: newConfig,
155+
};
156+
});
157+
return newConfig;
158+
};
159+
const initPlugin = (config) => {
160+
config = withIosAppDelegateImport(config);
161+
config = withIosAppDelegateBody(config);
162+
config = withAndroidMainActivityImport(config);
163+
config = withAndroidMainActivityBody(config);
164+
return config;
165+
};
166+
exports.default = initPlugin;

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "react-native-keyevent-expo-config-plugin",
3+
"version": "1.0.0",
4+
"description": "Config plugin to auto configure react-native-keyevent on prebuild",
5+
"main": "build/withReactNativeKeyevent.js",
6+
"types": "build/withReactNativeKeyevent.d.ts",
7+
"sideEffects": false,
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/chronsyn/react-native-keyevent-expo-config-plugin.git"
11+
},
12+
"scripts": {
13+
"build": "expo-module build",
14+
"clean": "expo-module clean",
15+
"lint": "expo-module lint",
16+
"test": "expo-module test",
17+
"prepare": "expo-module prepare",
18+
"prepublishOnly": "expo-module prepublishOnly",
19+
"expo-module": "expo-module"
20+
},
21+
"files": [
22+
"build",
23+
"app.plugin.js"
24+
],
25+
"author": "Scott Pritchard",
26+
"devDependencies": {
27+
"expo-module-scripts": "^2.1.1"
28+
},
29+
"peerDependencies": {
30+
"expo": "^46.0.2"
31+
},
32+
"dependencies": {
33+
"@expo/config-plugins": "^5.0.1"
34+
}
35+
}

0 commit comments

Comments
 (0)