magmax / python-readchar

Python library to read characters and key strokes
MIT License
143 stars 43 forks source link

wrong repo. Sorry #81

Closed Cube707 closed 2 years ago

Cube707 commented 2 years ago

This version indeed solves issue 1 in #64 but unfortunately it doesn't fix issue 2 as per my test on my ovh server (running ubuntu). Below is my test script:

import sys
sys.path.insert(0, r"/root/python-readchar-v4")
from threading import Thread
from queue import Queue
from typing import Callable, Dict
import time
import logging
import readchar

logging.basicConfig(
    format='%(asctime)s - %(levelname)s - %(module)s - %(message)s', level=logging.INFO,
)

running = False

class KeyBoardMonitor(object):

    def __init__(self, callbacks: Dict[str, Callable[[str], None]] = None, quit_symbol: str = 'q'):
        self.callbacks = callbacks if callbacks else {}
        self.quit_symbol = quit_symbol
        self.running = True
        self.queue = Queue()
        self.thread = Thread(name="keyboard monitoring", target=self._keyboard_monitor)

    def register_callback(self, order: str, callback: Callable[[str], None]) -> None:
        self.callbacks[order] = callback

    def unregister_callback(self, order):
        del self.callbacks[order]

    def start(self):
        self.thread.start()

    def stop(self):
        self.running = False

    def _keyboard_monitor(self):
        while self.running:
            ch = str(readchar.readchar())
            if ch in self.callbacks:
                self.callbacks[ch](ch)
            if ch == self.quit_symbol:
                self.running = False

def myprint(ch):
    print(ch, "entered")
    if ch == 'q':
        global running
        running = False

if __name__=="__main__":
    watchdog = KeyBoardMonitor(callbacks={'q': myprint, 'p': myprint})
    watchdog.start()
    running = True
    for _ in range(300):
        if not running:
            break
        time.sleep(1)
        logging.info(f"current_time: {time.time()}")

copy this into file and change the path in sys.path.insert to the "master" branch of local clone of your repo in r"/root/python-readchar-v4", I still see unwanted blanks (should come from the \r generated by setraw and missing linebreaks between logs: image

In contrast, if using my version in pull request #64, I can get clean log outputs. image

Would you please take some further look?

Thanks!

_Originally posted by @qianyun210603 in github.com/magmax/python-readchar/issues/79#issuecomment-1179482070