This repository has been archived on 2023-09-06. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
py-gtk-notes
Archived
1
0
Fork 0

增加热键绑定的笔记

master
yutent 2023-07-27 20:25:43 +08:00
parent 1c3595c23a
commit c0011b582d
3 changed files with 86 additions and 8 deletions

9
app.js
View File

@ -43,8 +43,13 @@ bind($('.btn5'), 'click', async function () {
}) })
bind($('.btn6'), 'click', async function () { bind($('.btn6'), 'click', async function () {
console.log(await native.screen.getAllDisplays()) // console.log(await native.globalShortcut.enabled)
console.log(await native.screen.getPrimaryDisplay())
native.globalShortcut.register('<Ctrl>2', function () {
alert('<Ctrl>2被绑定了')
})
// console.log(await native.screen.getAllDisplays())
// console.log(await native.screen.getPrimaryDisplay())
}) })
bind($('textarea'), 'paste', async function (ev) { bind($('textarea'), 'paste', async function (ev) {

View File

@ -37,6 +37,12 @@ const MIME_TYPES = {
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
} }
const KEYS_MAP = {
shift: '<Shift>',
ctrl: '<Ctrl>',
alt: '<Alt>',
super: '<Super>'
}
function defer() { function defer() {
let obj = {} let obj = {}
@ -193,5 +199,37 @@ Object.assign(native, {
return handler('monitor', { action: 'get-primary' }) 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 handler
}) })

47
main.py
View File

@ -5,8 +5,9 @@ import gi, json, os
gi.require_version("Gtk", "3.0") gi.require_version("Gtk", "3.0")
gi.require_version("WebKit2", "4.1") 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 from gi.repository.GdkPixbuf import Pixbuf
# 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon # 优先尝试使用指示器, 没有再使用 Gtk.StatusIcon
@ -17,6 +18,9 @@ try:
except (ValueError, ImportError): except (ValueError, ImportError):
AppIndicator3 = None AppIndicator3 = None
# 初始化 Keybinder
Keybinder.init()
def get_monitor_info(monitor): def get_monitor_info(monitor):
return { return {
@ -82,6 +86,7 @@ class WebKitWindow(Gtk.Window):
self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
self.add(self.webview) self.add(self.webview)
@ -185,7 +190,7 @@ class WebKitWindow(Gtk.Window):
# 退出app # 退出app
case 'quit': case 'quit':
Gtk.main_quit() all_quit(self)
# 读取图片, 返回图片像素数据 # 读取图片, 返回图片像素数据
case 'image': case 'image':
@ -227,6 +232,35 @@ class WebKitWindow(Gtk.Window):
scripts = 'native.$emit("' + callback + '",' + json.dumps(info) + ')' scripts = 'native.$emit("' + callback + '",' + json.dumps(info) + ')'
self.webview.evaluate_javascript(scripts, -1) 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 _: case _:
if callback : if callback :
res = {"foo": 123, "bar": (11,22,33)} res = {"foo": 123, "bar": (11,22,33)}
@ -235,17 +269,18 @@ class WebKitWindow(Gtk.Window):
self.webview.evaluate_javascript(scripts, -1) self.webview.evaluate_javascript(scripts, -1)
def all_quit(win):
print('朕要休息了~~~')
Gtk.main_quit()
win = WebKitWindow() win = WebKitWindow()
win.connect("destroy", Gtk.main_quit) win.connect("destroy", all_quit)
win.show_all() win.show_all()
print(Keybinder.supported())
tray = win.create_tray() tray = win.create_tray()