Closed Srh07 closed 6 months ago
dependencies {
if (hermesEnabled.toBoolean()) {
implementation("io.github.react-native-tvos:hermes-android")
} else {
implementation jscFlavor
}
implementation project(':react-native-keyevent') // Записать так, compile метод работать не будет
}
class MainApplication : Application(), ReactApplication {
override val reactNativeHost: ReactNativeHost =
object : DefaultReactNativeHost(this) {
override fun getPackages(): List<ReactPackage> =
PackageList(this).packages.apply {
// Packages that cannot be autolinked yet can be added manually here, for example:
// add(MyReactNativePackage())
// add(KeyEventPackage()) <--- Добавлять не надо, иначе будет ошибка, так как пакет подключается автоматически при сборке
}
class MainActivity : ReactActivity() {
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
override fun getMainComponentName(): String = "App"
/**
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
*/
override fun createReactActivityDelegate(): ReactActivityDelegate =
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
//---Добавить то что ниже
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
KeyEventModule.getInstance().onKeyDownEvent(keyCode, event)
super.onKeyDown(keyCode, event)
return true
}
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
KeyEventModule.getInstance().onKeyUpEvent(keyCode, event)
super.onKeyUp(keyCode, event)
return true
}
override fun onKeyMultiple(keyCode: Int, repeatCount: Int, event: KeyEvent): Boolean {
KeyEventModule.getInstance().onKeyMultipleEvent(keyCode, repeatCount, event)
return super.onKeyMultiple(keyCode, repeatCount, event)
}
}
Тестировал на AndroidTV
@sasha-ld Thank you so much for this! I implemented your code and I'm able to build my app again. I'll test full functionality tomorrow.
@sasha-ld Works like a charm!
I also added this code based on https://github.com/kevinejohn/react-native-keyevent/issues/67#issuecomment-1226051216
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
KeyEventModule.getInstance().onKeyDownEvent(event.getKeyCode(), event);
return false;
}
return super.dispatchKeyEvent(event);
}
Hi everyone. I used react-native-keyevent-expo-config-plugin and seems to not working with latest expo. Who know what need to change in this plugin?
@sasha-ld Works like a charm!
I also added this code based on https://github.com/kevinejohn/react-native-keyevent/issues/67#issuecomment-1226051216
override fun dispatchKeyEvent(event: KeyEvent): Boolean { if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { KeyEventModule.getInstance().onKeyDownEvent(event.getKeyCode(), event); return false; } return super.dispatchKeyEvent(event); }
Can you share me your config. i try follow you but not working
#MainActivity.KT
package com.bigcontact
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
class MainActivity : ReactActivity() {
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
override fun getMainComponentName(): String = "BigContact"
/**
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
*/
override fun createReactActivityDelegate(): ReactActivityDelegate =
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
/**
* React native key event
*/
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
KeyEventModule.getInstance().onKeyDownEvent(keyCode, event)
super.onKeyDown(keyCode, event)
return true
}
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
KeyEventModule.getInstance().onKeyUpEvent(keyCode, event)
super.onKeyUp(keyCode, event)
return true
}
override fun onKeyMultiple(keyCode: Int, repeatCount: Int, event: KeyEvent): Boolean {
KeyEventModule.getInstance().onKeyMultipleEvent(keyCode, repeatCount, event)
return super.onKeyMultiple(keyCode, repeatCount, event)
}
}
PhamMinhKha Hi Looks like you didn't import the module
package com.your_app_name
import android.app.Application
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactHost
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
import com.facebook.react.defaults.DefaultReactNativeHost
import com.facebook.soloader.SoLoader
import com.github.kevinejohn.keyevent.KeyEventPackage; //<--- import
class MainApplication : Application(), ReactApplication {
override val reactNativeHost: ReactNativeHost =
object : DefaultReactNativeHost(this) {
override fun getPackages(): List<ReactPackage> =
PackageList(this).packages.apply {
// Packages that cannot be autolinked yet can be added manually here, for example:
// add(MyReactNativePackage())
}
override fun getJSMainModuleName(): String = "index"
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
}
override val reactHost: ReactHost
get() = getDefaultReactHost(this.applicationContext, reactNativeHost)
override fun onCreate() {
super.onCreate()
SoLoader.init(this, false)
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// If you opted-in for the New Architecture, we load the native entry point for this app.
load()
}
}
}
package com.you_app_name
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
import android.view.KeyEvent; // <--- import
import com.github.kevinejohn.keyevent.KeyEventModule; // <--- import
class MainActivity : ReactActivity() {
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
override fun getMainComponentName(): String = "you_app_name"
/**
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
*/
override fun createReactActivityDelegate(): ReactActivityDelegate =
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
// A. Prevent multiple events on long button press
// In the default behavior multiple events are fired if a button
// is pressed for a while. You can prevent this behavior if you
// forward only the first event:
// if (event.getRepeatCount() == 0) {
// KeyEventModule.getInstance().onKeyDownEvent(keyCode, event);
// }
//
// B. If multiple Events shall be fired when the button is pressed
// for a while use this code:
// KeyEventModule.getInstance().onKeyDownEvent(keyCode, event);
//
// Using B.
KeyEventModule.getInstance().onKeyDownEvent(keyCode, event)
// There are 2 ways this can be done:
// 1. Override the default keyboard event behavior
// super.onKeyDown(keyCode, event);
// return true;
// 2. Keep default keyboard event behavior
// return super.onKeyDown(keyCode, event);
// Using method #2 without blocking multiple
return super.onKeyDown(keyCode, event);
}
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
KeyEventModule.getInstance().onKeyUpEvent(keyCode, event)
// There are 2 ways this can be done:
// 1. Override the default keyboard event behavior
// super.onKeyUp(keyCode, event);
// return true;
// 2. Keep default keyboard event behavior
// return super.onKeyUp(keyCode, event);
// Using method #2
return super.onKeyUp(keyCode, event);
}
// override fun onKeyMultiple(keyCode: Int, repeatCount: Int, event: KeyEvent?): Boolean {
// KeyEventModule.getInstance().onKeyMultipleEvent(keyCode, repeatCount, event)
// return super.onKeyMultiple(keyCode, repeatCount, event)
// }
}
rootProject.name = 'your_app_name'
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
include ':app'
includeBuild('../node_modules/@react-native/gradle-plugin')
include ':react-native-keyevent'
project(':react-native-keyevent').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-keyevent/android')
apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
apply plugin: "com.facebook.react"
/**
* This is the configuration block to customize your React Native Android app.
* By default you don't need to apply any configuration, just uncomment the lines you need.
*/
react {
/* Folders */
// The root of your project, i.e. where "package.json" lives. Default is '..'
// root = file("../")
// The folder where the react-native NPM package is. Default is ../node_modules/react-native
// reactNativeDir = file("../node_modules/react-native")
// The folder where the react-native Codegen package is. Default is ../node_modules/@react-native/codegen
// codegenDir = file("../node_modules/@react-native/codegen")
// The cli.js file which is the React Native CLI entrypoint. Default is ../node_modules/react-native/cli.js
// cliFile = file("../node_modules/react-native/cli.js")
/* Variants */
// The list of variants to that are debuggable. For those we're going to
// skip the bundling of the JS bundle and the assets. By default is just 'debug'.
// If you add flavors like lite, prod, etc. you'll have to list your debuggableVariants.
// debuggableVariants = ["liteDebug", "prodDebug"]
/* Bundling */
// A list containing the node command and its flags. Default is just 'node'.
// nodeExecutableAndArgs = ["node"]
//
// The command to run when bundling. By default is 'bundle'
// bundleCommand = "ram-bundle"
//
// The path to the CLI configuration file. Default is empty.
// bundleConfig = file(../rn-cli.config.js)
//
// The name of the generated asset file containing your JS bundle
// bundleAssetName = "MyApplication.android.bundle"
//
// The entry file for bundle generation. Default is 'index.android.js' or 'index.js'
// entryFile = file("../js/MyApplication.android.js")
//
// A list of extra flags to pass to the 'bundle' commands.
// See https://github.com/react-native-community/cli/blob/main/docs/commands.md#bundle
// extraPackagerArgs = []
/* Hermes Commands */
// The hermes compiler command to run. By default it is 'hermesc'
// hermesCommand = "$rootDir/my-custom-hermesc/bin/hermesc"
//
// The list of flags to pass to the Hermes compiler. By default is "-O", "-output-source-map"
// hermesFlags = ["-O", "-output-source-map"]
}
/**
* Set this to true to Run Proguard on Release builds to minify the Java bytecode.
*/
def enableProguardInReleaseBuilds = false
/**
* The preferred build flavor of JavaScriptCore (JSC)
*
* For example, to use the international variant, you can use:
* `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
*
* The international variant includes ICU i18n library and necessary data
* allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
* give correct results when using with locales other than en-US. Note that
* this variant is about 6MiB larger per architecture than default.
*/
def jscFlavor = 'org.webkit:android-jsc:+'
android {
ndkVersion rootProject.ext.ndkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
compileSdk rootProject.ext.compileSdkVersion
namespace "com.your_app_name"
defaultConfig {
applicationId "com.your_app_name"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
// Caution! In production, you need to generate your own keystore file.
// see https://reactnative.dev/docs/signed-apk-android.
signingConfig signingConfigs.debug
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
}
dependencies {
// The version of react-native is set by the React Native Gradle Plugin
// For the TV repo,
// we use the io.github.react-native-tvos group for the react-android and hermes-android dependencies
implementation("io.github.react-native-tvos:react-android")
// No Flipper for TV
// implementation("com.facebook.react:flipper-integration")
if (hermesEnabled.toBoolean()) {
implementation("io.github.react-native-tvos:hermes-android")
} else {
implementation jscFlavor
}
implementation project(':react-native-keyevent')
}
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
@sasha-ld Thank you for your response, your instructions were very detailed and they worked. I really appreciate it.
Thank you! Exactly what I needed!!
Hi,
The java files have changed to Kotlin files. Could you please provide us with how to add the correct code in these files? I understand that the overrides need to be written differently, but there also seems to be something else going on.
Example in MainActivity.kt
Example MainApplication.kt
Example android/app/build.gradle
With the above code I get the following error: Failed to install the app. Command failed with exit code 1: ./gradlew app:installDebug -PreactNativeDevServerPort=8081 FAILURE: Build failed with an exception. * What went wrong: Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
When I don't implement the code in build.gradle I get this error: Unresolved reference: KeyEventPackage FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:compileDebugKotlin'.
Versions: "react-native": "0.73.2", "react": "18.2.0", "react-native-keyevent": "^0.3.2"
Java 17.0.9 SDK 34 KotlinVersion 1.8.0