Merge pull request #114 from jamesbaber1/main

Made modifier key optional and adding support for more than just single alphnumeric keycodes
This commit is contained in:
louisnw01
2023-09-15 12:20:22 +01:00
committed by GitHub

View File

@ -849,15 +849,24 @@ class AbstractChart(Candlestick, Pane):
def spinner(self, visible): def spinner(self, visible):
self.run_script(f"{self.id}.spinner.style.display = '{'block' if visible else 'none'}'") self.run_script(f"{self.id}.spinner.style.display = '{'block' if visible else 'none'}'")
def hotkey(self, modifier_key: Literal['ctrl', 'alt', 'shift', 'meta'], def hotkey(self, modifier_key: Literal['ctrl', 'alt', 'shift', 'meta', None],
keys: Union[str, tuple, int], func: callable): keys: Union[str, tuple, int], func: callable):
if not isinstance(keys, tuple): if not isinstance(keys, tuple):
keys = (keys,) keys = (keys,)
for key in keys: for key in keys:
key_code = 'Key' + key.upper() if isinstance(key, str) else 'Digit' + str(key) # when there is no modifier key use the key value
condition = f"event.key.toLowerCase() === '{str(key).lower()}'"
# if there is a modifier key
if modifier_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
condition = f"event.{modifier_key}Key && event.code === '{key_code}'"
self.run_script(f''' self.run_script(f'''
{self.id}.commandFunctions.unshift((event) => {{ {self.id}.commandFunctions.unshift((event) => {{
if (event.{modifier_key + 'Key'} && event.code === '{key_code}') {{ if ({condition}) {{
event.preventDefault() event.preventDefault()
window.callbackFunction(`{modifier_key, keys}_~_{key}`) window.callbackFunction(`{modifier_key, keys}_~_{key}`)
return true return true