From 3e31a100eeff5f04d9d86af101773e6e93fc08fc Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Thu, 2 Feb 2023 11:48:30 -0500 Subject: [PATCH 1/4] Patch to fix sticky Media Keys --- components/ble_keyboard/ble_keyboard.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/ble_keyboard/ble_keyboard.cpp b/components/ble_keyboard/ble_keyboard.cpp index 178d316..9e5243f 100644 --- a/components/ble_keyboard/ble_keyboard.cpp +++ b/components/ble_keyboard/ble_keyboard.cpp @@ -99,6 +99,8 @@ void Esp32BleKeyboard::press(MediaKeyReport key, bool with_timer) { this->update_timer(); } bleKeyboard.press(key); + delay(100); + bleKeyboard.release(key); } } From df3f1e59171dee074ca5cc63b83f78c10f224b21 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Thu, 2 Feb 2023 13:24:34 -0500 Subject: [PATCH 2/4] Reduced Delay --- components/ble_keyboard/ble_keyboard.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/ble_keyboard/ble_keyboard.cpp b/components/ble_keyboard/ble_keyboard.cpp index 9e5243f..d264db6 100644 --- a/components/ble_keyboard/ble_keyboard.cpp +++ b/components/ble_keyboard/ble_keyboard.cpp @@ -99,7 +99,7 @@ void Esp32BleKeyboard::press(MediaKeyReport key, bool with_timer) { this->update_timer(); } bleKeyboard.press(key); - delay(100); + delay(8); bleKeyboard.release(key); } } From f82720fefb10a9616ecc9957ab523e5da0fe158c Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Tue, 21 Feb 2023 12:46:32 -0500 Subject: [PATCH 3/4] Added the ability to release a specific Key instead of just all keys. --- components/ble_keyboard/__init__.py | 21 +++++++++++++++++++-- components/ble_keyboard/automation.h | 14 +++++++++++++- components/ble_keyboard/ble_keyboard.cpp | 17 +++++++++++++++-- components/ble_keyboard/ble_keyboard.h | 2 ++ components/ble_keyboard/const.py | 2 +- 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/components/ble_keyboard/__init__.py b/components/ble_keyboard/__init__.py index 2e79757..bf65381 100644 --- a/components/ble_keyboard/__init__.py +++ b/components/ble_keyboard/__init__.py @@ -188,7 +188,17 @@ def adding_dependencies(use_default_libs: bool = True) -> None: @automation.register_action( f"{DOMAIN}.release", BLEKeyboardReleaseAction, - maybe_simple_id(OPERATION_BASE_SCHEMA), + OPERATION_BASE_SCHEMA.extend( + { + cv.Optional(CONF_CODE): cv.Any( + cv.templatable(cv.positive_int), + cv.All( + [cv.uint8_t], + cv.Length(min=2, max=2), + ), + ), + } + ), ) async def ble_keyboard_release_to_code( config: dict, action_id: ID, template_arg: TemplateArguments, args: list @@ -203,8 +213,15 @@ async def ble_keyboard_release_to_code( """ paren: MockObj = await cg.get_variable(config[CONF_ID]) + var: MockObj = cg.new_Pvariable(action_id, template_arg, paren) + if CONF_CODE in config: + if isinstance(config[CONF_CODE], list): + cg.add(var.set_keys(config[CONF_CODE])) + else: + template_: LambdaExpression = await cg.templatable(config[CONF_CODE], args, int) + cg.add(var.set_code(template_)) - return cg.new_Pvariable(action_id, template_arg, paren) + return var BLEKeyboardPrintAction = ble_keyboard_ns.class_(ACTION_PRINT_CLASS, automation.Action) diff --git a/components/ble_keyboard/automation.h b/components/ble_keyboard/automation.h index 96076b3..fccbc16 100644 --- a/components/ble_keyboard/automation.h +++ b/components/ble_keyboard/automation.h @@ -45,10 +45,22 @@ template class Esp32BleKeyboardPressAction : public Action class Esp32BleKeyboardReleaseAction : public Action { public: explicit Esp32BleKeyboardReleaseAction(Esp32BleKeyboard *ble_keyboard) : ble_keyboard_(ble_keyboard) {} + TEMPLATABLE_VALUE(uint8_t, code) + + void play(Ts... x) override { + if(keys_.size() > 1) { + MediaKeyReport mediaKey = {keys_[0], keys_[1]}; + + this->ble_keyboard_->release(mediaKey); + } else { + this->ble_keyboard_->release(this->code_.value(x...)); + } + } - void play(Ts... x) override { this->ble_keyboard_->release(); } + void set_keys(const std::vector &keys) { keys_ = keys; } protected: + std::vector keys_; Esp32BleKeyboard *ble_keyboard_; }; diff --git a/components/ble_keyboard/ble_keyboard.cpp b/components/ble_keyboard/ble_keyboard.cpp index d264db6..1d6e128 100644 --- a/components/ble_keyboard/ble_keyboard.cpp +++ b/components/ble_keyboard/ble_keyboard.cpp @@ -99,8 +99,6 @@ void Esp32BleKeyboard::press(MediaKeyReport key, bool with_timer) { this->update_timer(); } bleKeyboard.press(key); - delay(8); - bleKeyboard.release(key); } } @@ -110,6 +108,21 @@ void Esp32BleKeyboard::release() { bleKeyboard.releaseAll(); } } + +void Esp32BleKeyboard::release(uint8_t key) { + if(this->is_connected()) { + this->cancel_timeout((const std::string) TAG); + bleKeyboard.release(key); + } +} + +void Esp32BleKeyboard::release(MediaKeyReport key) { + if(this->is_connected()) { + this->cancel_timeout((const std::string) TAG); + bleKeyboard.release(key); + } +} + } // namespace ble_keyboard } // namespace esphome diff --git a/components/ble_keyboard/ble_keyboard.h b/components/ble_keyboard/ble_keyboard.h index c575f1f..c1d7cff 100644 --- a/components/ble_keyboard/ble_keyboard.h +++ b/components/ble_keyboard/ble_keyboard.h @@ -31,6 +31,8 @@ class Esp32BleKeyboard : public PollingComponent { void press(std::string message); void press(uint8_t key, bool with_timer = true); void press(MediaKeyReport key, bool with_timer = true); + void release(uint8_t key); + void release(MediaKeyReport key); void release(); void start(); diff --git a/components/ble_keyboard/const.py b/components/ble_keyboard/const.py index 1f20b16..9e80b10 100644 --- a/components/ble_keyboard/const.py +++ b/components/ble_keyboard/const.py @@ -53,7 +53,7 @@ """Libraries""" LIBS_DEFAULT: Final = [ - ("ESP32 BLE Arduino", "1.0.1", None), + ("ESP32 BLE Arduino", "1.0.2", None), ] LIBS_ADDITIONAL: Final = [ From 6023c6039f17f6c47b1ef4dc953e237f81e206c7 Mon Sep 17 00:00:00 2001 From: Jonathan Williams Date: Tue, 21 Feb 2023 12:59:24 -0500 Subject: [PATCH 4/4] Updated Readme to include release specific key, and release all keys. --- README.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e7d7953..9b6c65c 100644 --- a/README.md +++ b/README.md @@ -115,10 +115,27 @@ ble_keyboard.press: #### ble_keyboard.release -Release keys. +Release all keys. ```yaml -ble_keyboard.release: my_ble_keyboard +ble_keyboard.release: + id: my_ble_keyboard +``` + +Release a specific Key +```yaml +ble_keyboard.release + id: my_ble_keyboard + code: 0x80 +``` + +Release a specific media Key +```yaml +ble_keyboard.release + id: my_ble_keyboard + code: + - 0 + - 1 ``` #### ble_keyboard.combination