npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-common-keyevent

v0.0.3

Published

为使RN能满足TV端遥控器需求,获取Android原生键盘事件封装的RN键盘事件类

Downloads

5

Readme

react-native-common-keyevent

该组件用于React-Native中获取Android原生层面的键盘响应事件。

安装

使用npm

$ npm install react-native-common-keyevent --save

Android端环境配置

  1. android/setting.gradle文件增加以下代码

     ...
     include ':react-native-common-keyevent'
     project(':react-native-common-keyevent').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-common-keyevent/android')
  2. android/app/build.gradle文件增加以下代码

     ...
     dependencies {
         ...
         compile project(':react-native-common-keyevent')
     }
  3. android代码中修改(Application方式或Activity方式二选一)

    • Application方式注册模块

        import import com.jianghe.keyevent.KeyEventPackage;  //--> 导入模块包
      		
        public class MainApplication extends Application implements ReactApplication {
        	......
        	@Override
        	protected List<ReactPackage> getPackages() {
        		return Arrays.<ReactPackage>asList(
        			new MainReactPackage(),
        			new KeyEventPackage() //-->增加组件的ReactPackage文件
        		);
        	}		
        	......	
        }	
    • Activity方式注册模块

        import com.jianghe.keyevent.*;	//--> 导入模块包
      
        public class MyActivity extends Activity implements DefaultHardwareBackBtnHandler {
      		
            private ReactRootView mReactRootView;
            private ReactInstanceManager mReactInstanceManager;
      		
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                mReactRootView = new ReactRootView(this);
                mReactInstanceManager = ReactInstanceManager.builder()
                        .setApplication(getApplication())
                        .setBundleAssetName("index.android.bundle")
                        .setJSMainModuleName("index.android")
                        .addPackage(new MainReactPackage())
                        .addPackage(new KeyEventPackage()) //-->增加组件的ReactPackage文件
                        .setUseDeveloperSupport(true)
                        .setInitialLifecycleState(LifecycleState.RESUMED)
                        .build();
                mReactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", null);
                setContentView(mReactRootView);
            }
      		
        }
  4. Activity中增加按键监听并传递给RN的方法

     import android.view.KeyEvent; // -->引入Android键盘事件包
     import com.jianghe.keyevent.KeyEventModule; // -->引入该组件模块包
    
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
     	//利用RN的DeviceEventEmitter对象将按键消息发送给js
         KeyEventModule.getkeyEventModule().onKeyDownEvent(keyCode);
         super.onKeyDown(keyCode, event);
         return true;
     }
    
     @Override
     public boolean onKeyUp(int keyCode, KeyEvent event) {
     	//利用RN的DeviceEventEmitter对象将按键消息发送给js
         KeyEventModule.getkeyEventModule().onKeyUpEvent(keyCode);
         super.onKeyUp(keyCode, event);
         return true;
     }

React-Native中使用

在任何你想要使用按键监听的地方引入该组件模块

import KeyEvent from 'react-native-common-keyevent';
  1. 在组件中申明3个事件响应方法

     keydown: function (keycode) {
         console.log("按下" + keycode);
     },
     keyup: function (keycode) {
         console.log("抬起" + keycode);
     },
     keypress: function (keycode) {
         console.log("点击" + keycode);
     },
  2. 在componentDidMount生命周期方法中初始化KeyEvent对象

     this.ke = KeyEvent.initKeyEvent({
         state: true,//当前对象初始化时是否处于激活状态
         onKeyDown: this.keydown,//键盘按下的回调方法
         onKeyUp: this.keyup,//键盘抬起的回调方法
         onKeyPress: this.keypress,//键盘单击的回调方法
     })

注意

RN中的多个页面都会被渲染到一个Activity中,这样存在的问题就是RN中多个页面响应的其实都是1个Activity的键盘事件。 那么会出现以下问题:

1.现在有2个页面A.js , B.js 现在A中的回车事件是 Alert.alert('A'); B中的上事件是 Alert.alert('B');

当前的焦点页在A上,回车打出的是A;现在通过react-navigation跳转到B页面,这时回车依然会弹出A,这不是我们预期的结果。所以在KeyEvent对象初始化的时候传入了一个state属性,这个属性就是用来控制当前KeyEvent对象是否生效的。 上面情况的解决方案:

A现在通过react-navigation跳转到B页面是使用this.ke.updateState(false)来将A页面的激活状态调整为false这样Android原生的按键事件就会分配到激活的组件对象上