Skip to content

Commit 14ca09a

Browse files
committed
initial commit
0 parents  commit 14ca09a

12 files changed

+5777
-0
lines changed

.eslintrc.js

+2
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

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# OSX
2+
#
3+
.DS_Store
4+
5+
# VSCode
6+
.vscode/
7+
jsconfig.json
8+
9+
# Cocoapods
10+
#
11+
example/ios/Pods
12+
13+
# Ruby
14+
example/vendor/
15+
16+
# node.js
17+
#
18+
node_modules/
19+
npm-debug.log
20+
yarn-debug.log
21+
yarn-error.log
22+
23+
# Expo
24+
.expo/*

.npmignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# @generated by expo-module-scripts
2+
3+
# Exclude all top-level hidden directories by convention
4+
/.*/
5+
6+
# Exclude tarballs generated by `npm pack`
7+
/*.tgz
8+
9+
__mocks__
10+
__tests__
11+
__rsc_tests__
12+
13+
/babel.config.js
14+
/android/src/androidTest/
15+
/android/src/test/

README.md

Whitespace-only changes.

app.plugin.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./plugin/build");

package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "react-native-keyevent-config-plugin",
3+
"version": "0.0.1",
4+
"scripts": {
5+
"build": "expo-module build",
6+
"clean": "expo-module clean",
7+
"test": "expo-module test",
8+
"prepublishOnly": "expo-module prepublishOnly",
9+
"expo-module": "expo-module"
10+
},
11+
"devDependencies": {
12+
"expo-module-scripts": "^4.0.2"
13+
}
14+
}

plugin/build/index.d.ts

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

plugin/build/index.js

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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`, // /#import "AppDelegate\.h"/g,
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: `;`,
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+
"@Override",
144+
"public boolean dispatchKeyEvent(KeyEvent event) {",
145+
" if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {",
146+
" KeyEventModule.getInstance().onKeyUpEvent(event.getKeyCode(), event);",
147+
" return false;",
148+
" }",
149+
" return super.dispatchKeyEvent(event);",
150+
"}",
151+
];
152+
const newConfig = (0, generateCode_1.mergeContents)({
153+
tag: "react-native-keyevent-body",
154+
src: config.modResults.contents,
155+
newSrc: newSrc.join("\n"),
156+
anchor: `public class MainActivity extends ReactActivity {`,
157+
offset: 1,
158+
comment: "//",
159+
});
160+
return {
161+
...config,
162+
modResults: newConfig,
163+
};
164+
});
165+
return newConfig;
166+
};
167+
const withReactNativeKeyEventPlugin = (config) => {
168+
return (0, config_plugins_1.withPlugins)(config, [
169+
withIosAppDelegateImport,
170+
withIosAppDelegateBody,
171+
withAndroidMainActivityImport,
172+
withAndroidMainActivityBody,
173+
]);
174+
};
175+
exports.default = withReactNativeKeyEventPlugin;

0 commit comments

Comments
 (0)