Skip to content

Commit 02cc37b

Browse files
committed
feat: implement keyboard connect listener, implement keyboard key listener
1 parent 9a4837d commit 02cc37b

31 files changed

+16054
-89
lines changed

ExternalKeyboardListener.podspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ require "json"
22

33
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
44

5+
Pod::UI.puts "[BDtren] Thank you for choosing react-native-external-keyboard-listener, it will be worth it 😎👌🔥"
6+
57
Pod::Spec.new do |s|
68
s.name = "ExternalKeyboardListener"
79
s.version = package["version"]

README.md

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,118 @@ React Native listen event from external keyboard
55
## Installation
66

77
```sh
8-
npm install react-native-external-keyboard-listener
8+
npm install react-native-external-keyboard-listener OR yarn add
9+
10+
npx pod-install
11+
```
12+
13+
### Permissions
14+
15+
- Android: Add these lines to your AndroidManifest.xml:
16+
```xml
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
18+
...
19+
20+
<!-- Android >= 12 -->
21+
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
22+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
23+
<!-- Android < 12 -->
24+
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
25+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
26+
<!-- common -->
27+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
28+
29+
<!-- Add this line if your application always requires BLE. More info can be found on:
30+
https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#permissions
31+
-->
32+
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
33+
34+
...
35+
```
36+
37+
- iOS: Add this key to your Info.plist:
38+
```xml
39+
<key>NSBluetoothAlwaysUsageDescription</key>
40+
<string>This app requires Bluetooth access</string>
941
```
1042

1143
## Usage
1244

45+
## API Reference
46+
47+
### `startListening()`
48+
• Signature: `startListening(): void`
49+
• Description: Begins listening for external keyboard events, emitting callbacks such as “OnKeyPress” or “KeyboardConnectionChanged.”
50+
• Example:
51+
```tsx
52+
ExternalKeyboardListenerEmitter.startListening();
53+
```
54+
55+
---
56+
57+
### `stopListening()`
58+
• Signature: `stopListening(): void`
59+
• Description: Stops listening for external keyboard events and releases related resources.
60+
• Example:
61+
```tsx
62+
ExternalKeyboardListenerEmitter.stopListening();
63+
```
64+
65+
---
66+
67+
### `checkKeyboardConnection()`
68+
• Signature: `checkKeyboardConnection(): Promise<boolean>`
69+
• Description: Checks if an external keyboard is present.
70+
• Example:
71+
```tsx
72+
const isConnected = await ExternalKeyboardListenerEmitter.checkKeyboardConnection();
73+
console.log('Keyboard connected:', isConnected);
74+
```
75+
76+
---
77+
78+
### `isBluetoothEnabled()`
79+
• Signature: `isBluetoothEnabled(): Promise<boolean>`
80+
• Description: Check if Bluetooth is enabled or not
81+
• Example:
82+
```tsx
83+
await ExternalKeyboardListenerEmitter.isBluetoothEnabled();
84+
```
85+
86+
---
87+
88+
### `enableBluetooth()`
89+
• Signature: `enableBluetooth(): Promise<void>`
90+
• Description: Opens system Bluetooth settings to allow enabling Bluetooth.
91+
• Example:
92+
```tsx
93+
await ExternalKeyboardListenerEmitter.enableBluetooth();
94+
```
95+
96+
---
97+
98+
### `requestBluetoothPermission()`
99+
• Signature: `requestBluetoothPermission(): Promise<boolean>`
100+
• Description: Requests Bluetooth permissions on Android.
101+
• Example:
102+
```tsx
103+
const granted = await ExternalKeyboardListenerEmitter.requestBluetoothPermission();
104+
console.log('Permission granted:', granted);
105+
```
13106

14-
```js
15-
import { multiply } from 'react-native-external-keyboard-listener';
107+
---
16108

17-
// ...
109+
### `startKeyPressListener(callback: (event: KeyPressEvent) => void)`
110+
• Signature: `startKeyPressListener(callback: (event: KeyPressEvent) => void): { remove: () => void }`
111+
• Description: Subscribes to key press events from an external keyboard.
112+
• Example:
113+
```tsx
114+
const subscription = ExternalKeyboardListenerEmitter.startKeyPressListener((evt) => {
115+
console.log('Key pressed:', evt);
116+
});
18117

19-
const result = await multiply(3, 7);
118+
// Stop listening:
119+
subscription.remove();
20120
```
21121

22122

android/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ buildscript {
1919
apply plugin: "com.android.library"
2020
apply plugin: "kotlin-android"
2121

22+
logger.warn("[BDtren] Thank you for choosing react-native-external-keyboard-listener, it will be worth it 😎👌🔥")
2223

2324
def getExtOrIntegerDefault(name) {
2425
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["ExternalKeyboardListener_" + name]).toInteger()
@@ -77,5 +78,6 @@ def kotlin_version = getExtOrDefault("kotlinVersion")
7778
dependencies {
7879
implementation "com.facebook.react:react-android"
7980
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
81+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1"
8082
}
8183

42.5 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)