- 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
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