Created
October 21, 2026 01:33
-
-
Save xssfox/7df0510b0e4185e12ff9d335e56a563c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import argparse | |
| import asyncio | |
| from bleak import BleakScanner, BleakClient, BleakGATTCharacteristic | |
| import traceback | |
| import time | |
| import subprocess | |
| CMD = "/Users/mwheeler/src/Karabiner-DriverKit-VirtualHIDDevice/examples/virtual-hid-device-service-client/build/Release/virtual-hid-device-service-client" | |
| p = subprocess.Popen(["/Users/mwheeler/src/Karabiner-DriverKit-VirtualHIDDevice/examples/virtual-hid-device-service-client/build/Release/virtual-hid-device-service-client"], stdin=subprocess.PIPE) | |
| async def scan(): | |
| devices = await BleakScanner.discover( | |
| return_adv=True, | |
| service_uuids=["0000ffe0-0000-1000-8000-00805f9b34fb"], | |
| cb={"use_bdaddr": False}, | |
| ) | |
| for device in devices: | |
| #breakpoint() | |
| print(device + " - " + devices[device][0].name) | |
| def press(): | |
| print("press") | |
| p.stdin.write("1\n".encode()) | |
| p.stdin.flush() | |
| def release(): | |
| print("release") | |
| p.stdin.write("0\n".encode()) | |
| p.stdin.flush() | |
| async def connect(address, keycode): | |
| async with BleakClient(address) as client: | |
| print(f"Connected: {client.is_connected}") | |
| async def notification_handler( | |
| characteristic: BleakGATTCharacteristic, data: bytearray | |
| ): | |
| if data == bytearray(b'\xcd'): | |
| print("release ptt") | |
| release() | |
| elif data == bytearray(b'\xab'): | |
| print("press ptt") | |
| press() | |
| else: | |
| print(f"{characteristic.description}: {data}") | |
| await client.start_notify("EF01CDAA-D46E-D46E-0128-1616D66ED46E", notification_handler) | |
| while client.is_connected: | |
| await asyncio.sleep(0.2) | |
| release() | |
| print("Disconnected") | |
| parser = argparse.ArgumentParser( | |
| prog='pybleptt', | |
| description='Remaps PTT-Z01 to a keypress', epilog="Start with no arguments to scan") | |
| parser.add_argument('address', type=str, nargs='?',) | |
| parser.add_argument('keycode', type=int, nargs='?', default=248) | |
| args = parser.parse_args() | |
| if not args.address: | |
| asyncio.run(scan()) | |
| else: | |
| while 1: | |
| try: | |
| asyncio.run(connect(args.address, args.keycode)) | |
| except KeyboardInterrupt: | |
| raise | |
| except: | |
| traceback.print_exc() | |
| release() | |
| time.sleep(0.2) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // from https://github.com/pqrs-org/Karabiner-DriverKit-VirtualHIDDevice/tree/main | |
| #include <atomic> | |
| #include <filesystem> | |
| #include <iostream> | |
| #include <pqrs/karabiner/driverkit/virtual_hid_device_driver.hpp> | |
| #include <pqrs/karabiner/driverkit/virtual_hid_device_service.hpp> | |
| #include <thread> | |
| using namespace std; | |
| namespace { | |
| std::atomic<bool> exit_flag(false); | |
| std::atomic<bool> tx(false); | |
| } | |
| int main() { | |
| std::signal(SIGINT, [](int) { | |
| exit_flag = true; | |
| }); | |
| // Needed before using `pqrs::karabiner::driverkit::virtual_hid_device_service::client`. | |
| pqrs::dispatcher::extra::initialize_shared_dispatcher(); | |
| std::mutex client_mutex; | |
| auto client1 = std::make_unique<pqrs::karabiner::driverkit::virtual_hid_device_service::client>(); | |
| std::mutex keyboard_thread_mutex; | |
| std::unique_ptr<std::thread> keyboard_thread1; | |
| // | |
| // client1 | |
| // | |
| client1->warning_reported.connect([](auto&& message) { | |
| std::cout << "warning: " << message << std::endl; | |
| }); | |
| client1->connected.connect([&client1] { | |
| std::cout << "connected" << std::endl; | |
| pqrs::karabiner::driverkit::virtual_hid_device_service::virtual_hid_keyboard_parameters parameters; | |
| parameters.set_country_code(pqrs::hid::country_code::us); | |
| client1->async_virtual_hid_keyboard_initialize(parameters); | |
| }); | |
| client1->connect_failed.connect([](auto&& error_code) { | |
| std::cout << "connect_failed " << error_code << std::endl; | |
| }); | |
| client1->closed.connect([] { | |
| std::cout << "closed" << std::endl; | |
| }); | |
| client1->error_occurred.connect([](auto&& error_code) { | |
| std::cout << "error_occurred " << error_code << std::endl; | |
| }); | |
| client1->driver_activated.connect([](auto&& driver_activated) { | |
| static std::optional<bool> previous_value; | |
| if (previous_value != driver_activated) { | |
| std::cout << "driver_activated " << driver_activated << std::endl; | |
| previous_value = driver_activated; | |
| } | |
| }); | |
| client1->driver_connected.connect([](auto&& driver_connected) { | |
| static std::optional<bool> previous_value; | |
| if (previous_value != driver_connected) { | |
| std::cout << "driver_connected " << driver_connected << std::endl; | |
| previous_value = driver_connected; | |
| } | |
| }); | |
| client1->driver_version_mismatched.connect([](auto&& driver_version_mismatched) { | |
| static std::optional<bool> previous_value; | |
| if (previous_value != driver_version_mismatched) { | |
| std::cout << "driver_version_mismatched " << driver_version_mismatched << std::endl; | |
| previous_value = driver_version_mismatched; | |
| } | |
| }); | |
| client1->virtual_hid_keyboard_ready.connect([&client1, &client_mutex, &keyboard_thread1, &keyboard_thread_mutex](auto&& ready) { | |
| if (!keyboard_thread1) { | |
| std::cout << "virtual_hid_keyboard_ready " << ready << std::endl; | |
| } | |
| if (ready) { | |
| std::lock_guard<std::mutex> lock(keyboard_thread_mutex); | |
| if (!keyboard_thread1) { | |
| keyboard_thread1 = std::make_unique<std::thread>([&client1, &client_mutex] { | |
| { | |
| std::lock_guard<std::mutex> lock(client_mutex); | |
| while (client1 && !exit_flag) { | |
| // key down (shift+e) | |
| { | |
| pqrs::karabiner::driverkit::virtual_hid_device_driver::hid_report::keyboard_input report; | |
| if (tx){ | |
| std::cout << "tx" << std::endl; | |
| report.keys.insert(type_safe::get(pqrs::hid::usage::keyboard_or_keypad::keyboard_left_shift)); | |
| } | |
| client1->async_post_report(report); | |
| } | |
| std::this_thread::sleep_for(std::chrono::milliseconds(100)); | |
| } | |
| } | |
| }); | |
| } | |
| } | |
| }); | |
| client1->async_start(); | |
| // | |
| // Wait control-c | |
| // | |
| std::cout << std::endl; | |
| std::cout << "Press control+c to quit." << std::endl; | |
| std::cout << std::endl; | |
| std::cout << "pqrs::karabiner::driverkit::driver_version::embedded_driver_version: " << pqrs::karabiner::driverkit::driver_version::embedded_driver_version << std::endl; | |
| std::cout << "pqrs::karabiner::driverkit::client_protocol_version::embedded_client_protocol_version: " << pqrs::karabiner::driverkit::client_protocol_version::embedded_client_protocol_version << std::endl; | |
| char ch; | |
| while (std::cin.get(ch)) { | |
| if (ch == '1') { | |
| tx=true; | |
| }if (ch == '0') { | |
| tx=false; | |
| } | |
| std::this_thread::sleep_for(std::chrono::milliseconds(100)); | |
| } | |
| // | |
| // Termination | |
| // | |
| std::this_thread::sleep_for(std::chrono::milliseconds(500)); | |
| { | |
| std::lock_guard<std::mutex> lock(client_mutex); | |
| client1 = nullptr; | |
| } | |
| { | |
| std::lock_guard<std::mutex> lock(keyboard_thread_mutex); | |
| if (keyboard_thread1) { | |
| keyboard_thread1->join(); | |
| } | |
| } | |
| // Needed after using `pqrs::karabiner::driverkit::virtual_hid_device_service::client`. | |
| pqrs::dispatcher::extra::terminate_shared_dispatcher(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment