sshkeyboard
sshkeyboard copied to clipboard
suggestion: add advanced example with threading to docs
Hi, I've been trying to use sshkeyboard for the GUI in a websockets-based client application. The main application is running in the main asyncio loop and I ended up running sshkeyboard in a separate thread. I also wanted to have a way to stop sshkeyboard from the main thread, instead of just with an until-key. I'd like to propose adding something akin to the following to the docs so people won't spend so much time beating their head against a wall:
async def sshkeyboard_listener(self):
await sshkeyboard.listen_keyboard_manual(
on_press=self.key_handler
)
async def sshkeyboard_monitor_stop(self):
while self.sshkeyboard_stop is not True:
await asyncio.sleep(0.101)
sshkeyboard.stop_listening()
async def sshkeyboard_coroutine_launcher(self):
await asyncio.gather(
self.sshkeyboard_listener(),
self.sshkeyboard_monitor_stop()
)
def sshkeyboard_loop(self):
asyncio.run(self.sshkeyboard_coroutine_launcher())
def sshkeyboard_start(self):
self.sshkeyboard_thread = threading.Thread(
target=self.sshkeyboard_loop,
daemon=True
)
self.sshkeyboard_thread.start()
print("sshkeyboard thread started.")
def sshkeyboard_shutdown(self):
self.sshkeyboard_stop = True
if self.sshkeyboard_thread and self.sshkeyboard_thread.is_alive():
self.sshkeyboard_thread.join()
self.sshkeyboard_thread = None
print("sshkeyboard thread shut down")
def key_handler(self, key):
if key == 'q':
print("'q' pressed, shutting down")
self.exit_reason = ExitStates.Shutdown
asyncio.run_coroutine_threadsafe(
self.terminate(), self.asyncio_loop)
Having keyboard input in applications running other stuff with asyncio could be much easier with such an example. Anyways, thank you very much for the amazing library!