Skip to content

Commit

Permalink
添加 grapInputEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
SOVLOOKUP committed Oct 2, 2023
1 parent 0710a66 commit 5d2057e
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 23 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ pnpm add rubick-native
{+CTRL}a{-CTRL}{+SHIFT}Hello World{-SHIFT}
```

9. 监听输入事件
9. onInputEvent: 键鼠事件监听

##### 入参 eg

```js
(event: object) => console.log(event);
(event: object) => void;
```

- callback: function 监听输入事件的函数
Expand All @@ -61,6 +61,18 @@ pnpm add rubick-native
}
```

10. grapInputEvent 键鼠事件监听&拦截

比 onInputEvent 多了拦截键鼠事件的功能,但是在 MacOS 下需要被授予 Accessibility 权限此 API 才能工作,故无拦截需求建议使用 onInputEvent

##### 入参 eg

```js
(event: object) => boolean;
```

- callback: function 监听输入事件的函数, 返回是否将事件发送给系统

### 2. Clipboard ✅

可以获取剪贴板中复制的内容:
Expand Down
2 changes: 1 addition & 1 deletion addon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version = "0.0.0"
crate-type = ["cdylib"]

[dependencies]
rdev = { version = "0.5.3", features = ["serialize"] }
rdev = { version = "0.5.3", features = ["serialize", "unstable_grab"] }
clipboard-files = "0.1.1"
copypasta = "0.8.2"
enigo = "0.1.3"
Expand Down
1 change: 1 addition & 0 deletions addon/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ClipBoardContentJson {
}
export function getClipboardContent(): ClipBoardContentJson | null
export function onInputEvent(callback: (event: string) => void): void
export function grabInputEvent(callback: (event: string) => boolean): void
export function exeLookBase64(fileName: string): string | null
export function parseLnk(path: string): string | null
export function parseLnkFallback(path: string): LnkData
Expand Down
3 changes: 2 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,11 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { getClipboardContent, onInputEvent, ShorCutImg, exeLookBase64, parseLnk, LnkData, parseLnkFallback, sendKeyboardSimulation, MouseBtn, MouseAction, sendMouseSimulation } = nativeBinding
const { getClipboardContent, onInputEvent, grabInputEvent, ShorCutImg, exeLookBase64, parseLnk, LnkData, parseLnkFallback, sendKeyboardSimulation, MouseBtn, MouseAction, sendMouseSimulation } = nativeBinding

module.exports.getClipboardContent = getClipboardContent
module.exports.onInputEvent = onInputEvent
module.exports.grabInputEvent = grabInputEvent
module.exports.ShorCutImg = ShorCutImg
module.exports.exeLookBase64 = exeLookBase64
module.exports.parseLnk = parseLnk
Expand Down
21 changes: 20 additions & 1 deletion addon/src/main.rs
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
fn main() {}
// #[cfg(feature = "unstable_grab")]
use rdev::{grab, Event};

fn main() {
// #[cfg(feature = "unstable_grab")]
let callback = |event: Event| -> Option<Event> {
println!("{}", serde_json::to_string_pretty(&event).unwrap());
None // CapsLock is now effectively disabled
// if let EventType::KeyPress(Key::CapsLock) = event.event_type {
// } else {
// Some(event)
// }
};
// This will block.
// #[cfg(feature = "unstable_grab")]
println!("{}", 1);
if let Err(error) = grab(callback) {
println!("Error: {:?}", error)
}
}
45 changes: 43 additions & 2 deletions addon/src/monitor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use napi::{
bindgen_prelude::*,
threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode},
JsBoolean,
};
use rdev::{grab, listen, Event};
use std::{
sync::mpsc::{self, Sender},
thread::spawn,
};
use rdev::listen;
use std::thread::spawn;

#[napi(ts_args_type = "callback: (event: string) => void")]
pub fn on_input_event(callback: JsFunction) {
Expand All @@ -22,3 +26,40 @@ pub fn on_input_event(callback: JsFunction) {
}
});
}

#[napi(ts_args_type = "callback: (event: string) => boolean")]
pub fn grab_input_event(callback: JsFunction) {
let jsfn: ThreadsafeFunction<String, ErrorStrategy::Fatal> = callback
.create_threadsafe_function(0, |ctx| Ok(vec![ctx.value]))
.unwrap();

let gcallback = move |event: Event| -> Option<Event> {
let (s, r): (Sender<bool>, mpsc::Receiver<bool>) = mpsc::channel::<bool>();
jsfn.call_with_return_value(
serde_json::to_string(&event).unwrap(),
ThreadsafeFunctionCallMode::NonBlocking,
move |e: JsBoolean| {
if let Ok(goon) = e.get_value() {
if !goon {
// 需要拦截事件
s.send(false).unwrap();
}
}
s.send(true).unwrap();
Ok(())
},
);
for i in r {
if !i {
return None;
}
}
Some(event)
};

spawn(|| {
if let Err(error) = grab(gcallback) {
println!("GrabError: {:?}", error)
}
});
}
5 changes: 3 additions & 2 deletions src/monitor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { onInputEvent as oie } from "../addon"
import { onInputEvent as oie, grabInputEvent as gie } from "../addon"

export const onInputEvent = (callback: (event: string) => void) => oie((event) => callback(JSON.parse(event)))
export const onInputEvent = (callback: (event: object) => void) => oie((event) => callback(JSON.parse(event)))
export const grabInputEvent = (callback: (event: object) => boolean) => gie((event) => callback(JSON.parse(event)))
26 changes: 12 additions & 14 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// import { onInputEvent } from "./src"


// onInputEvent((event) => {
// console.log(event);
// })

import * as addon from "./dist"

console.log(addon);
// for await (const i of shortcutWin()) {
// console.log(i);
// // sss++
// }
import { grabInputEvent } from "./addon"

grabInputEvent((event) => {
const e = JSON.parse(event)?.event_type
if (e?.ButtonRelease === "Right") {
return false
}
console.log(e);
return true
})

console.log(1);

0 comments on commit 5d2057e

Please sign in to comment.