Make copying text work with different layouts

This makes it possible so text can be copied from the conversation textview with other keyboard layouts than english
This commit is contained in:
Philipp Hörist 2018-12-17 14:49:50 +01:00
parent 9eed0338d9
commit 50435ba885
2 changed files with 18 additions and 1 deletions

View File

@ -55,6 +55,7 @@ from gajim.message_textview import MessageTextView
from gajim.gtk.dialogs import NonModalConfirmationDialog from gajim.gtk.dialogs import NonModalConfirmationDialog
from gajim.gtk.util import convert_rgb_to_hex from gajim.gtk.util import convert_rgb_to_hex
from gajim.gtk.util import get_primary_accel_mod from gajim.gtk.util import get_primary_accel_mod
from gajim.gtk.util import get_hardware_key_codes
from gajim.gtk.emoji_chooser import emoji_chooser from gajim.gtk.emoji_chooser import emoji_chooser
@ -80,6 +81,13 @@ class ChatControlBase(MessageControl, ChatCommandProcessor, CommandTools):
A base class containing a banner, ConversationTextview, MessageTextView A base class containing a banner, ConversationTextview, MessageTextView
""" """
# This is needed so copying text from the conversation textview
# works with different language layouts. Pressing the key c on a russian
# layout yields another keyval than with the english layout.
# So we match hardware keycodes instead of keyvals.
# Multiple hardware keycodes can trigger a keyval like Gdk.KEY_c.
keycodes_c = get_hardware_key_codes(Gdk.KEY_c)
def make_href(self, match): def make_href(self, match):
url_color = app.css_config.get_value('.gajim-url', StyleAttr.COLOR) url_color = app.css_config.get_value('.gajim-url', StyleAttr.COLOR)
color = convert_rgb_to_hex(url_color) color = convert_rgb_to_hex(url_color)
@ -570,7 +578,7 @@ class ChatControlBase(MessageControl, ChatCommandProcessor, CommandTools):
def _conv_textview_key_press_event(self, _widget, event): def _conv_textview_key_press_event(self, _widget, event):
if (event.get_state() & get_primary_accel_mod() and if (event.get_state() & get_primary_accel_mod() and
event.keyval in (Gdk.KEY_c, Gdk.KEY_Insert)): event.hardware_keycode in self.keycodes_c):
return Gdk.EVENT_PROPAGATE return Gdk.EVENT_PROPAGATE
if (event.get_state() & Gdk.ModifierType.SHIFT_MASK and if (event.get_state() & Gdk.ModifierType.SHIFT_MASK and

View File

@ -255,3 +255,12 @@ def get_primary_accel_mod():
cmd on osx, ctrl everywhere else. cmd on osx, ctrl everywhere else.
""" """
return Gtk.accelerator_parse("<Primary>")[1] return Gtk.accelerator_parse("<Primary>")[1]
def get_hardware_key_codes(keyval):
keymap = Gdk.Keymap.get_for_display(Gdk.Display.get_default())
valid, key_map_keys = keymap.get_entries_for_keyval(keyval)
if not valid:
return []
return [key.keycode for key in key_map_keys]