- reverted arg order change

- switched to using event.key when no modifier
This commit is contained in:
jamesbaber1
2023-09-14 09:52:05 -05:00
parent 36fae9fe15
commit 5b792fbaa2

View File

@ -2,7 +2,7 @@ import asyncio
import os import os
from base64 import b64decode from base64 import b64decode
from datetime import datetime from datetime import datetime
from typing import Union, Literal, List, Optional from typing import Union, Literal, List
import pandas as pd import pandas as pd
from .table import Table from .table import Table
@ -849,31 +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( def hotkey(self, modifier_key: Literal['ctrl', 'alt', 'shift', 'meta', None],
self, keys: Union[str, tuple, int], func: callable):
keys: Union[str, tuple, int],
func: callable,
modifier_key: Optional[Literal['ctrl', 'alt', 'shift', 'meta']] = None
):
if not isinstance(keys, tuple): if not isinstance(keys, tuple):
keys = (keys,) keys = (keys,)
for key in keys: for key in keys:
# default to taking the key code as is # when there is no modifier key use the key value
key_code = key key_event_condition = f"event.key === '{key}'"
# if the key code is only one character or digit, build the correct key code # if there is a modifier key
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
if 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.run_script(f'''
{self.id}.commandFunctions.unshift((event) => {{ {self.id}.commandFunctions.unshift((event) => {{
if ({modifier_key_js}event.code === '{key_code}') {{ if ({key_event_condition}) {{
event.preventDefault() event.preventDefault()
window.callbackFunction(`{modifier_key, keys}_~_{key}`) window.callbackFunction(`{modifier_key, keys}_~_{key}`)
return true return true