2

Hello I'm mainly trying to find out if the current way I'm going about this small project idea makes sense because my idea is sort of in-between a few different ideas. I would describe it as an active profanity filter that is meant to alert the user if profanity is used. Ultimately this is meant to preempt them making the full statement and think about a more constructive way to phrase the statement in question.

I'm still kindof in the design phase and was wondering if I could get some clarity on some things I didn't understand about pynput in general.

def on_press(key):
    # Do something on each keystroke
    if key == Key.enter:
        #record and add additional key characters to string for analysis while some escape character isn't pressed, second listener maybe? A while loop of some kind or a listener lock/semaphore combo?

def on_release(key):
    if key == Key.esc:
        # Stop listener
        return False

From what I've looked into the above seems to be the goto type of setup for this type of situation and although it probably doesn't matter something about it seemed a bit off to me. My project will have periods where key inputs are both being recorded and times where it is unimportant. The player will press "enter" to begin chatting and this is when it is important. There are a couple reasons why this kindof confuses me as it seems that each individual keypress triggers this listener and if I need to first recognize the enter key press first, then do I then need an additional listener for each time I enter this "chatting mode". The reason I felt this didn't make sense is because I need there to be some way for the original listener to halt while the second continues. Not only do I not want more than two listeners in this case, but due to the design, one of the few release keys on the second listener would have to be the "enter" key which is what would start the secondary listening process.

The only simple solution that I thought of was just trying to immediately capture the string in a variable with input() but hitting enter to submit might cause cascading text inputs that can't end since the listener is still listening. This also doesn't work because I need the user to be able to discard or enter the text by some means that won't cause additional problems and I feel that would be difficult.

Even if for a small scale example the "cost" doesn't matter too much, but I do like to make sure I'm not doing something that is stupid or wasteful. Considering that there are hundreds of potential keystrokes that can happen I still feel like not wasting too many resources is still preferable to the alternative.

Maybe there was something simple that I'm missing about this particular library but I found no real information on keystroke input that is the trigger for more keystroke input.

I didn't feel it too necessary but the only other functions would be to add keystrokes to a string and to run that string though a profanity filter and provide feedback if it contained profanity to alert the user to their wrongdoing.

  • I've learned a little bit about this situation after coming back to it and it would seem that the easiest way, that I can see at least is to simply try to suppress new threads created by the listener with a flag/boolean. I've done a few tests and I think each keystroke even should work like its own thread even though it's completed very fast most times so when I am "in chat" after pressing the enter key I flip a flag and all keystrokes until I flip the flag back will simply be ignored. This still doesn't seem entirely right, but it's the only solution I have so far. – Smorgleberry Dec 02 '21 at 00:00
  • I won't consider this issue solved yet but until it is solved I would like to learn some more about pynput that I didn't get from the documentation. One other question I had involving the threads. Since it seems difficult to create a scenario where the listener is not listening, is there much overhead for creating these threads and just doing nothing with them. It feels wasteful but since the threads are doing next to nothing besides a conditional check and the program itself is rather small maybe its not an issue. – Smorgleberry Dec 02 '21 at 00:05

0 Answers0