From c0011b582d38605243075a0a5358c0d4a1176580 Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 27 Jul 2023 20:25:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=83=AD=E9=94=AE=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E7=9A=84=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 9 +++++++-- inject.js | 38 ++++++++++++++++++++++++++++++++++++++ main.py | 47 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 86 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index 14d0d8d..af6677e 100644 --- a/app.js +++ b/app.js @@ -43,8 +43,13 @@ bind($('.btn5'), 'click', async function () { }) bind($('.btn6'), 'click', async function () { - console.log(await native.screen.getAllDisplays()) - console.log(await native.screen.getPrimaryDisplay()) + // console.log(await native.globalShortcut.enabled) + + native.globalShortcut.register('2', function () { + alert('2被绑定了') + }) + // console.log(await native.screen.getAllDisplays()) + // console.log(await native.screen.getPrimaryDisplay()) }) bind($('textarea'), 'paste', async function (ev) { diff --git a/inject.js b/inject.js index 8c608ae..81ec033 100644 --- a/inject.js +++ b/inject.js @@ -37,6 +37,12 @@ const MIME_TYPES = { xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' } +const KEYS_MAP = { + shift: '', + ctrl: '', + alt: '', + super: '' +} function defer() { let obj = {} @@ -193,5 +199,37 @@ Object.assign(native, { return handler('monitor', { action: 'get-primary' }) } }, + globalShortcut: { + get enabled() { + return handler('keybinder', { action: 'supported' }) + }, + register(keyMap, callback) { + // + let shortcut_callback = 'blabla' + native.$off(shortcut_callback) + native.$on(shortcut_callback, callback) + return handler('keybinder', { + action: 'register', + value: keyMap, + shortcut_callback + }) + }, + registerAll(keyMap, callback) { + // + return handler('keybinder', { action: 'register-all', value: keyMap }) + }, + isRegistered(keyMap) { + // + return handler('keybinder', { action: 'is-registered', value: keyMap }) + }, + unregister(keyMap) { + // + return handler('keybinder', { action: 'unregister', value: keyMap }) + }, + unregisterAll(keyMap) { + // + return handler('keybinder', { action: 'unregister-all', value: keyMap }) + } + }, handler }) diff --git a/main.py b/main.py index bf67661..56114df 100755 --- a/main.py +++ b/main.py @@ -5,8 +5,9 @@ import gi, json, os gi.require_version("Gtk", "3.0") gi.require_version("WebKit2", "4.1") +gi.require_version("Keybinder", "3.0") -from gi.repository import Gtk, Gdk, WebKit2, GLib +from gi.repository import Gtk, Gdk, WebKit2, GLib, Keybinder from gi.repository.GdkPixbuf import Pixbuf # 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon @@ -17,6 +18,9 @@ try: except (ValueError, ImportError): AppIndicator3 = None +# 初始化 Keybinder +Keybinder.init() + def get_monitor_info(monitor): return { @@ -82,6 +86,7 @@ class WebKitWindow(Gtk.Window): self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) + self.add(self.webview) @@ -185,7 +190,7 @@ class WebKitWindow(Gtk.Window): # 退出app case 'quit': - Gtk.main_quit() + all_quit(self) # 读取图片, 返回图片像素数据 case 'image': @@ -227,6 +232,35 @@ class WebKitWindow(Gtk.Window): scripts = 'native.$emit("' + callback + '",' + json.dumps(info) + ')' self.webview.evaluate_javascript(scripts, -1) + case 'keybinder': + output = None + keymap = params.get('value') + shortcut_callback = params.get('shortcut_callback') or '' + scripts = 'native.$emit("' + shortcut_callback + '")' + + + if params['action'] == 'register': + # 绑定之前, 先解绑, 避免被重复绑定 + Keybinder.unbind(keymap) + + output = Keybinder.bind( + keymap, + lambda km : self.webview.evaluate_javascript('native.$emit("' + shortcut_callback + '")', -1) + ) + + + elif params['action'] == 'register': + pass + + elif params['action'] == 'is-registered': + pass + + elif params['action'] == 'supported': + output = Keybinder.supported() + + scripts = 'native.$emit("' + callback + '",' + json.dumps(output) + ')' + self.webview.evaluate_javascript(scripts, -1) + case _: if callback : res = {"foo": 123, "bar": (11,22,33)} @@ -235,17 +269,18 @@ class WebKitWindow(Gtk.Window): self.webview.evaluate_javascript(scripts, -1) - +def all_quit(win): + print('朕要休息了~~~') + Gtk.main_quit() win = WebKitWindow() -win.connect("destroy", Gtk.main_quit) +win.connect("destroy", all_quit) win.show_all() - - +print(Keybinder.supported()) tray = win.create_tray()