Modify hotkey implementation

This commit is contained in:
louisnw
2023-09-15 12:30:38 +01:00
parent 2f1ad83b4d
commit 43eab9854d

View File

@ -853,26 +853,26 @@ class AbstractChart(Candlestick, Pane):
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:
# when there is no modifier key use the key value key = str(key)
condition = f"event.key.toLowerCase() === '{str(key).lower()}'" if key.isalnum() and len(key) == 1:
# if there is a modifier key key_code = f'Digit{key}' if key.isdigit() else f'Key{key.upper()}'
if modifier_key: key_condition = f'event.code === "{key_code}"'
# use the key code instead else:
key_code = 'Key' + key.upper() if isinstance(key, str) else 'Digit' + str(key) key_condition = f'event.key === "{key}"'
# change the condition to also require the modifier if modifier_key is not None:
condition = f"event.{modifier_key}Key && event.code === '{key_code}'" key_condition += f'&& event.{modifier_key}Key'
self.run_script(f''' self.run_script(f'''
{self.id}.commandFunctions.unshift((event) => {{ {self.id}.commandFunctions.unshift((event) => {{
if ({condition}) {{ console.log(event.key)
event.preventDefault() if ({key_condition}) {{
window.callbackFunction(`{modifier_key, keys}_~_{key}`) event.preventDefault()
return true window.callbackFunction(`{modifier_key, keys}_~_{key}`)
}} return true
else return false }}
}})''') else return false
}})''')
self.win.handlers[f'{modifier_key, keys}'] = func self.win.handlers[f'{modifier_key, keys}'] = func
def create_table(self, width: NUM, height: NUM, def create_table(self, width: NUM, height: NUM,