diff --git a/lightweight_charts/abstract.py b/lightweight_charts/abstract.py index 9506757..bf115bb 100644 --- a/lightweight_charts/abstract.py +++ b/lightweight_charts/abstract.py @@ -2,7 +2,7 @@ import asyncio import os from base64 import b64decode from datetime import datetime -from typing import Union, Literal, List, Optional +from typing import Union, Literal, List import pandas as pd from .table import Table @@ -849,31 +849,24 @@ class AbstractChart(Candlestick, Pane): def spinner(self, visible): self.run_script(f"{self.id}.spinner.style.display = '{'block' if visible else 'none'}'") - def hotkey( - self, - keys: Union[str, tuple, int], - func: callable, - modifier_key: Optional[Literal['ctrl', 'alt', 'shift', 'meta']] = None - ): + def hotkey(self, modifier_key: Literal['ctrl', 'alt', 'shift', 'meta', None], + keys: Union[str, tuple, int], func: callable): if not isinstance(keys, tuple): keys = (keys,) for key in keys: - # default to taking the key code as is - key_code = key - # if the key code is only one character or digit, build the correct key code - if len(str(key)) == 1: - key_code = 'Key' + key.upper() if isinstance(key, str) else 'Digit' + str(key) - - # default to no modifier key - modifier_key_js = '' - # if there is a modifier key, create the js + # when there is no modifier key use the key value + key_event_condition = f"event.key === '{key}'" + # if there is a modifier key if modifier_key: - modifier_key_js = f"event.{modifier_key}Key && " + # use the key code instead + key_code = 'Key' + key.upper() if isinstance(key, str) else 'Digit' + str(key) + # change the condition to also require the modifier + key_event_condition = f"event.{modifier_key}Key && event.code === '{key_code}'" self.run_script(f''' {self.id}.commandFunctions.unshift((event) => {{ - if ({modifier_key_js}event.code === '{key_code}') {{ + if ({key_event_condition}) {{ event.preventDefault() window.callbackFunction(`{modifier_key, keys}_~_{key}`) return true