Implement keyboard shortcuts for paste
This commit is contained in:
parent
7342367679
commit
030228571c
@ -19,7 +19,7 @@ import glfw_constants
|
|||||||
|
|
||||||
from .constants import appname
|
from .constants import appname
|
||||||
from .char_grid import CharGrid
|
from .char_grid import CharGrid
|
||||||
from .keys import interpret_text_event, interpret_key_event
|
from .keys import interpret_text_event, interpret_key_event, get_shortcut
|
||||||
from .utils import sanitize_title
|
from .utils import sanitize_title
|
||||||
from .fast_data_types import (
|
from .fast_data_types import (
|
||||||
BRACKETED_PASTE_START, BRACKETED_PASTE_END, Screen, read_bytes_dump, read_bytes
|
BRACKETED_PASTE_START, BRACKETED_PASTE_END, Screen, read_bytes_dump, read_bytes
|
||||||
@ -110,16 +110,18 @@ class Boss(Thread):
|
|||||||
def on_mouse_button(self, window, button, action, mods):
|
def on_mouse_button(self, window, button, action, mods):
|
||||||
if action == glfw_constants.GLFW_RELEASE:
|
if action == glfw_constants.GLFW_RELEASE:
|
||||||
if button == glfw_constants.GLFW_MOUSE_BUTTON_MIDDLE:
|
if button == glfw_constants.GLFW_MOUSE_BUTTON_MIDDLE:
|
||||||
# glfw has no way to get the primary selection
|
self.paste_from_selection()
|
||||||
# text = glfw.glfwGetClipboardString(window)
|
return
|
||||||
text = subprocess.check_output(['xsel'])
|
|
||||||
if text:
|
|
||||||
if self.screen.in_bracketed_paste_mode():
|
|
||||||
text = BRACKETED_PASTE_START.encode('ascii') + text + BRACKETED_PASTE_END.encode('ascii')
|
|
||||||
self.write_to_child(text)
|
|
||||||
|
|
||||||
def on_key(self, window, key, scancode, action, mods):
|
def on_key(self, window, key, scancode, action, mods):
|
||||||
if action == glfw_constants.GLFW_PRESS or action == glfw_constants.GLFW_REPEAT:
|
if action == glfw_constants.GLFW_PRESS or action == glfw_constants.GLFW_REPEAT:
|
||||||
|
func = get_shortcut(self.opts.keymap, mods, key)
|
||||||
|
if func is not None:
|
||||||
|
func = getattr(self, func, None)
|
||||||
|
if func is not None:
|
||||||
|
passthrough = func()
|
||||||
|
if not passthrough:
|
||||||
|
return
|
||||||
data = interpret_key_event(key, scancode, mods)
|
data = interpret_key_event(key, scancode, mods)
|
||||||
if data:
|
if data:
|
||||||
self.write_to_child(data)
|
self.write_to_child(data)
|
||||||
@ -260,3 +262,23 @@ class Boss(Thread):
|
|||||||
self.char_grid.change_colors(self.pending_color_changes)
|
self.char_grid.change_colors(self.pending_color_changes)
|
||||||
self.pending_color_changes = {}
|
self.pending_color_changes = {}
|
||||||
glfw.glfwPostEmptyEvent()
|
glfw.glfwPostEmptyEvent()
|
||||||
|
|
||||||
|
# actions {{{
|
||||||
|
|
||||||
|
def paste(self, text):
|
||||||
|
if text:
|
||||||
|
if self.screen.in_bracketed_paste_mode():
|
||||||
|
text = BRACKETED_PASTE_START.encode('ascii') + text + BRACKETED_PASTE_END.encode('ascii')
|
||||||
|
self.write_to_child(text)
|
||||||
|
|
||||||
|
def paste_from_clipboard(self):
|
||||||
|
text = glfw.glfwGetClipboardString(self.window)
|
||||||
|
self.paste(text)
|
||||||
|
|
||||||
|
def paste_from_selection(self):
|
||||||
|
# glfw has no way to get the primary selection
|
||||||
|
# https://github.com/glfw/glfw/issues/894
|
||||||
|
text = subprocess.check_output(['xsel'])
|
||||||
|
self.paste(text)
|
||||||
|
|
||||||
|
# }}}
|
||||||
|
|||||||
@ -230,11 +230,14 @@ def parse_key(val, keymap):
|
|||||||
def map_mod(m):
|
def map_mod(m):
|
||||||
return {'CTRL': 'CONTROL', 'CMD': 'CONTROL'}.get(m, m)
|
return {'CTRL': 'CONTROL', 'CMD': 'CONTROL'}.get(m, m)
|
||||||
|
|
||||||
|
mods = 0
|
||||||
|
for m in parts[:-1]:
|
||||||
try:
|
try:
|
||||||
mods = frozenset(getattr(glfw, 'GLFW_MOD_' + map_mod(m.upper())) for m in parts[:-1])
|
mods |= getattr(glfw, 'GLFW_MOD_' + map_mod(m.upper()))
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
print('Shortcut: {} has an unknown modifier, ignoring'.format(val), file=sys.stderr)
|
print('Shortcut: {} has an unknown modifier, ignoring'.format(val), file=sys.stderr)
|
||||||
return
|
return
|
||||||
|
|
||||||
key = getattr(glfw, 'GLFW_KEY_' + parts[-1].upper(), None)
|
key = getattr(glfw, 'GLFW_KEY_' + parts[-1].upper(), None)
|
||||||
if key is None:
|
if key is None:
|
||||||
print('Shortcut: {} has an unknown key, ignoring'.format(val), file=sys.stderr)
|
print('Shortcut: {} has an unknown key, ignoring'.format(val), file=sys.stderr)
|
||||||
|
|||||||
@ -61,3 +61,7 @@ def interpret_text_event(codepoint, mods):
|
|||||||
return b'' # Handled by interpret_key_event above
|
return b'' # Handled by interpret_key_event above
|
||||||
data = chr(codepoint).encode('utf-8')
|
data = chr(codepoint).encode('utf-8')
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def get_shortcut(keymap, mods, key):
|
||||||
|
return keymap.get((mods & 0b1111, key))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user