new variant of working with keyboard keys

This commit is contained in:
DYefremov
2018-11-05 00:31:44 +03:00
parent f16b47ebf1
commit 4e34057d16
4 changed files with 74 additions and 51 deletions

View File

@@ -17,7 +17,7 @@ from app.tools.media import Player
from .new_download_dialog import DownloadDialog
from .iptv import IptvDialog, SearchUnavailableDialog, IptvListConfigurationDialog
from .search import SearchProvider
from .uicommons import Gtk, Gdk, UI_RESOURCES_PATH, LOCKED_ICON, HIDE_ICON, IPTV_ICON, MOVE_KEYS, KEY_MAP
from .uicommons import Gtk, Gdk, UI_RESOURCES_PATH, LOCKED_ICON, HIDE_ICON, IPTV_ICON, MOVE_KEYS, KeyboardKey
from .dialogs import show_dialog, DialogType, get_chooser_dialog, WaitDialog, get_message
from .main_helper import insert_marker, move_items, rename, ViewTarget, set_flags, locate_in_services, \
scroll_to, get_base_model, update_picons_data, copy_picon_reference, assign_picon, remove_picon, \
@@ -951,70 +951,71 @@ class MainAppWindow:
def on_tree_view_key_press(self, view, event):
""" Handling keystrokes on press """
key = event.keyval
map_key = KEY_MAP.get(key, None)
key = map_key if map_key else key
key = KeyboardKey(event.hardware_keycode)
if not key:
return
ctrl = event.state & Gdk.ModifierType.CONTROL_MASK
model_name, model = get_model_data(view)
if ctrl and key == Gdk.KEY_c or key == Gdk.KEY_C:
if ctrl and key is KeyboardKey.C:
if model_name == self._SERVICE_LIST_NAME:
self.on_copy(view, ViewTarget.FAV)
elif model_name == self._FAV_LIST_NAME:
self.on_copy(view, ViewTarget.SERVICES)
else:
self.on_copy(view, ViewTarget.BOUQUET)
elif ctrl and key == Gdk.KEY_x or key == Gdk.KEY_X:
elif ctrl and key is KeyboardKey.X:
if model_name == self._FAV_LIST_NAME:
self.on_cut(view, ViewTarget.FAV)
elif model_name == self._BOUQUETS_LIST_NAME:
self.on_cut(view, ViewTarget.BOUQUET)
elif ctrl and key == Gdk.KEY_v or key == Gdk.KEY_V:
elif ctrl and key is KeyboardKey.V:
if model_name == self._FAV_LIST_NAME:
self.on_paste(view, ViewTarget.FAV)
elif model_name == self._BOUQUETS_LIST_NAME:
self.on_paste(view, ViewTarget.BOUQUET)
elif key == Gdk.KEY_Delete:
elif key is KeyboardKey.DELETE:
self.on_delete(view)
def on_tree_view_key_release(self, view, event):
""" Handling keystrokes on release """
key = event.keyval
map_key = KEY_MAP.get(key, None)
key = map_key if map_key else key
key = KeyboardKey(event.hardware_keycode)
print(event.hardware_keycode)
if not key:
return
ctrl = event.state & Gdk.ModifierType.CONTROL_MASK
alt = event.state & Gdk.ModifierType.MOD1_MASK
model_name, model = get_model_data(view)
if ctrl and key in MOVE_KEYS:
self.move_items(key)
elif model_name == self._FAV_LIST_NAME and key == Gdk.KEY_Control_L or key == Gdk.KEY_Control_R:
elif model_name == self._FAV_LIST_NAME and key is KeyboardKey.CTRL_L or key is KeyboardKey.CTRL_R:
self.update_fav_num_column(model)
self.update_bouquet_list()
elif ctrl and key == Gdk.KEY_Insert:
elif ctrl and key is KeyboardKey.INSERT:
# Move items from app to fav list
if model_name == self._SERVICE_LIST_NAME:
self.on_to_fav_copy(view)
elif model_name == self._BOUQUETS_LIST_NAME:
self.on_new_bouquet(view)
elif ctrl and key == Gdk.KEY_BackSpace and model_name == self._SERVICE_LIST_NAME:
elif ctrl and key is KeyboardKey.BACK_SPACE and model_name == self._SERVICE_LIST_NAME:
self.on_to_fav_end_copy(view)
elif ctrl and key == Gdk.KEY_s or key == Gdk.KEY_S:
elif ctrl and key is KeyboardKey.S:
self.on_data_save()
elif ctrl and key == Gdk.KEY_l or key == Gdk.KEY_L:
elif ctrl and key is KeyboardKey.L:
self.on_locked(None)
elif ctrl and key == Gdk.KEY_h or key == Gdk.KEY_H:
elif ctrl and key is KeyboardKey.H:
self.on_hide(None)
elif ctrl and key == Gdk.KEY_R or key == Gdk.KEY_r or key == Gdk.KEY_F2:
elif ctrl and key is KeyboardKey.R or key is KeyboardKey.F2:
self.on_rename(view)
elif ctrl and key == Gdk.KEY_E or key == Gdk.KEY_e:
elif ctrl and key is KeyboardKey.E:
if model_name == self._BOUQUETS_LIST_NAME:
self.on_rename(view)
return
self.on_service_edit(view)
elif key == Gdk.KEY_Left or key == Gdk.KEY_Right:
elif key is KeyboardKey.LEFT or key is KeyboardKey.RIGHT:
view.do_unselect_all(view)
elif ctrl and model_name == self._FAV_LIST_NAME and key in (Gdk.KEY_P, Gdk.KEY_p):
elif ctrl and model_name == self._FAV_LIST_NAME and key is KeyboardKey.P:
self.on_play_stream()
def on_download(self, item):

View File

@@ -8,7 +8,7 @@ from app.eparser import Service
from app.eparser.ecommons import Flag, BouquetService, Bouquet, BqType
from app.eparser.enigma.bouquets import BqServiceType, to_bouquet_id
from app.properties import Profile
from .uicommons import ViewTarget, BqGenType, Gtk, Gdk, HIDE_ICON, LOCKED_ICON
from .uicommons import ViewTarget, BqGenType, Gtk, Gdk, HIDE_ICON, LOCKED_ICON, KeyboardKey
from .dialogs import show_dialog, DialogType, get_chooser_dialog, WaitDialog
@@ -67,17 +67,17 @@ def move_items(key, view: Gtk.TreeView):
parent_itr = model.iter_parent(model.get_iter(paths[0]))
parent_index = model.get_path(parent_itr)
children_num = model.iter_n_children(parent_itr)
if key in (Gdk.KEY_Page_Down, Gdk.KEY_KP_Page_Down, Gdk.KEY_End):
if key in (KeyboardKey.PAGE_DOWN, KeyboardKey.END):
children_num -= 1
min_path = Gtk.TreePath.new_from_string("{}:{}".format(parent_index, 0))
max_path = Gtk.TreePath.new_from_string("{}:{}".format(parent_index, children_num))
is_tree_store = True
if key == Gdk.KEY_Up:
if key is KeyboardKey.UP:
top_path = Gtk.TreePath(paths[0])
top_path.prev()
move_up(top_path, model, paths)
elif key == Gdk.KEY_Down:
elif key is KeyboardKey.DOWN:
down_path = Gtk.TreePath(paths[-1])
down_path.next()
if down_path < max_path:
@@ -85,9 +85,9 @@ def move_items(key, view: Gtk.TreeView):
else:
max_path.prev()
move_down(max_path, model, paths)
elif key in (Gdk.KEY_Page_Up, Gdk.KEY_KP_Page_Up, Gdk.KEY_Home, Gdk.KEY_KP_Home):
elif key in (KeyboardKey.PAGE_UP, KeyboardKey.HOME):
move_up(min_path if is_tree_store else cursor_path, model, paths)
elif key in (Gdk.KEY_Page_Down, Gdk.KEY_KP_Page_Down, Gdk.KEY_End, Gdk.KEY_KP_End):
elif key in (KeyboardKey.PAGE_DOWN, KeyboardKey.END):
move_down(max_path if is_tree_store else cursor_path, model, paths)

View File

@@ -7,7 +7,7 @@ from app.commons import run_idle, run_task
from app.eparser import get_satellites, write_satellites, Satellite, Transponder
from app.tools.satellites import SatellitesParser, SatelliteSource
from .search import SearchProvider
from .uicommons import Gtk, Gdk, UI_RESOURCES_PATH, TEXT_DOMAIN, MOVE_KEYS
from .uicommons import Gtk, Gdk, UI_RESOURCES_PATH, TEXT_DOMAIN, MOVE_KEYS, KeyboardKey
from .dialogs import show_dialog, DialogType, WaitDialog
from .main_helper import move_items, scroll_to, append_text_to_tview, get_base_model, on_popup_menu
@@ -106,31 +106,29 @@ class SatellitesDialog:
view.expand_row(path, column)
def on_up(self, item):
move_items(Gdk.KEY_Up, self._sat_view)
move_items(KeyboardKey.UP, self._sat_view)
def on_down(self, item):
move_items(Gdk.KEY_Down, self._sat_view)
move_items(KeyboardKey.DOWN, self._sat_view)
def on_key_release(self, view, event):
""" Handling keystrokes """
key = event.keyval
key = KeyboardKey(event.hardware_keycode)
ctrl = event.state & Gdk.ModifierType.CONTROL_MASK
if key == Gdk.KEY_Delete:
if key is KeyboardKey.DELETE:
self.on_remove(view)
elif key == Gdk.KEY_Insert:
elif key is KeyboardKey.INSERT:
pass
elif ctrl and key == Gdk.KEY_E or key == Gdk.KEY_e:
elif ctrl and key is KeyboardKey.E:
self.on_edit(view)
elif ctrl and key == Gdk.KEY_s or key == Gdk.KEY_S:
elif ctrl and key is KeyboardKey.S:
self.on_satellite()
elif ctrl and key == Gdk.KEY_t or key == Gdk.KEY_T:
elif ctrl and key is KeyboardKey.T:
self.on_transponder()
elif key == Gdk.KEY_space:
pass
elif ctrl and key in MOVE_KEYS:
move_items(key, self._sat_view)
elif key == Gdk.KEY_Left or key == Gdk.KEY_Right:
elif key is KeyboardKey.LEFT or key is KeyboardKey.RIGHT:
view.do_unselect_all(view)
def on_popover_release(self, menu, event):

View File

@@ -26,19 +26,43 @@ HIDE_ICON = theme.load_icon("go-jump", 16, 0) if theme.lookup_icon("go-jump", 16
TV_ICON = theme.load_icon("tv-symbolic", 16, 0) if theme.lookup_icon("tv-symbolic", 16, 0) else _IMAGE_MISSING
IPTV_ICON = theme.load_icon("emblem-shared", 16, 0) if theme.load_icon("emblem-shared", 16, 0) else None
class KeyboardKey(Enum):
""" The raw(hardware) codes of the keyboard keys """
E = 26
R = 27
T = 28
P = 33
S = 39
H = 43
L = 46
X = 53
C = 54
V = 55
INSERT = 118
HOME = 110
END = 115
UP = 111
DOWN = 116
PAGE_UP = 112
PAGE_DOWN = 117
LEFT = 113
RIGHT = 114
F2 = 23
DELETE = 119
BACK_SPACE = 22
CTRL_L = 37
CTRL_R = 105
@classmethod
def _missing_(cls, value):
""" Overridden for skip ValueError if value is not found. """
pass
# Keys for move in lists. KEY_KP_(NAME) for laptop!!!
MOVE_KEYS = (Gdk.KEY_Up, Gdk.KEY_Page_Up, Gdk.KEY_Down, Gdk.KEY_Page_Down, Gdk.KEY_Home, Gdk.KEY_KP_Home, Gdk.KEY_End,
Gdk.KEY_KP_End, Gdk.KEY_KP_Page_Up, Gdk.KEY_KP_Page_Down)
KEY_MAP = {Gdk.KEY_Cyrillic_es: Gdk.KEY_c, Gdk.KEY_Cyrillic_ES: Gdk.KEY_c,
Gdk.KEY_Cyrillic_che: Gdk.KEY_x, Gdk.KEY_Cyrillic_CHE: Gdk.KEY_x,
Gdk.KEY_Cyrillic_em: Gdk.KEY_v, Gdk.KEY_Cyrillic_EM: Gdk.KEY_v,
Gdk.KEY_Cyrillic_ka: Gdk.KEY_r, Gdk.KEY_Cyrillic_KA: Gdk.KEY_r,
Gdk.KEY_Cyrillic_u: Gdk.KEY_e, Gdk.KEY_Cyrillic_U: Gdk.KEY_e,
Gdk.KEY_Cyrillic_de: Gdk.KEY_l, Gdk.KEY_Cyrillic_DE: Gdk.KEY_l,
Gdk.KEY_Cyrillic_er: Gdk.KEY_h, Gdk.KEY_Cyrillic_ER: Gdk.KEY_h,
Gdk.KEY_Cyrillic_ze: Gdk.KEY_p, Gdk.KEY_Cyrillic_ZE: Gdk.KEY_p}
MOVE_KEYS = (KeyboardKey.UP, KeyboardKey.PAGE_UP, KeyboardKey.DOWN, KeyboardKey.PAGE_DOWN, KeyboardKey.HOME,
KeyboardKey.END)
class ViewTarget(Enum):