英文:
word listener made with python keyboard module being triggered despite the required word not being typed
问题
I've been making a script that I dub the "pissbot", made to automatically send the message "piss" after a specific amount of time, and it's been incredibly basic in the past. I've been activating it through a single hotkey - usually shift + enter - but now I wish to enable the script with a command, I'm just trying to use the word "enable" to enable the script. I've looked around the internet, trying to find examples of how the keyboard word listener works, but I couldn't find anything, so I tried to figure it out myself and learned that it requires the following:
-
a word to listen for. (here, I am using "enable")
-
a callback (I didn't really know what this was so I searched that up too and learned it's basically just a function that gets called when the requirements are met).
-
a trigger, something that tells the listener to check if the previous word is the correct word or not.
Now, I have the following code:
enabled = False
timer = 1
condition = False
def enable():
global condition
condition = True
# wsh.AppActivate("blank")
while True:
keyboard.register_word_listener("enable", enable(), ["\n"])
if condition:
enabled = True
if enabled:
time.sleep(0.1)
wsh.SendKeys("piss")
time.sleep(0.1)
wsh.SendKeys("\n")
time.sleep(timer)
For all I know, this code should work perfectly fine, and typing "enable" then hitting enter should enable the script without any issues. But it doesn't, and the script is activated immediately upon starting the program, no matter what kind of keyboard input I give, even if there is no keyboard input at all.
I've tried searching for anything at all on this specific function, but I wound up with zero useful results. I really don't know where to go from here, I have zero idea how I can get this working or how I can find what's wrong with my code.
I've checked stack overflow for any questions similar to mine, but none I found match my problem.
I hope that after I post this, I don't get a message saying a question like this has been asked before, cause I've posted a question after looking for a question similar to it and not finding any, just to be told a question similar to it has already been posted.
but hey, if I do get told someone has asked this before, then I guess I'll end up getting my answer.
side note:
I know my code can be improved a lot, this is only the pissbot after all, so I'm not trying to make this the most optimized thing in the world.
英文:
I've been making a script that I dub the "pissbot", made to automatically send the message "piss" after a specific amount of time, and it's been incredibly basic in the past. I've been activating it through a single hotkey - usually shift + enter - but now I wish to enable the script with a command, I'm just trying to use the word "enable" to enable the script. I've looked around the internet, trying to find examples of how the keyboard word listener works, but I couldn't find anything, so I tried to figure it out myself and learned that it requires the following:
-
a word to listen for. (here, I am using "enable")
-
a callback (I didn't really know what this was so I searched that up too and learned it's basically just a function that gets called when the requirements are met).
-
a trigger, something that tells the listener to check if the previous word is the correct word or not.
Now, I have the following code:
enabled = False
timer = 1
condition = False
def enable():
global condition
condition = True
# wsh.AppActivate("blank")
while True:
keyboard.register_word_listener("enable", enable(), ["\n"])
if condition:
enabled = True
if enabled:
time.sleep(0.1)
wsh.SendKeys("piss")
time.sleep(0.1)
wsh.SendKeys("\n")
time.sleep(timer)
For all I know, this code should work perfectly fine, and typing "enable" then hitting enter should enable the script without any issues. But it doesn't, and the script is activated immediately upon starting the program, no matter what kind of keyboard input I give, even if there is no keyboard input at all.
I've tried searching for anything at all on this specific function, but I wound up with zero useful results. I really don't know where to go from here, I have zero idea how I can get this working or how I can find what's wrong with my code.
I've checked stack overflow for any questions similar to mine, but none I found match my problem.
I hope that after I post this, I don't get a message saying a question like this has been asked before, cause I've posted a question after looking for a question similar to it and not finding any, just to be told a question similar to it has already been posted.
but hey, if I do get told someone has asked this before, then I guess I'll end up getting my answer.
side note:
I know my code can be improved a lot, this is only the pissbot after all, so I'm not trying to make this the most optimized thing in the world.
答案1
得分: 1
你的代码中存在一些问题。
主要问题是,你没有将 enable
回调函数对象传递给监听器,而是调用了它(使用括号 ()
),然后将其返回值传递给监听器。在Python中,默认的返回值是 None
,因此当监听器被激活时什么都不会发生,但每次注册监听器时,回调函数都会运行,导致脚本过早输出消息。解决方案很简单,只需省略括号。
其他次要问题包括:
- 你在循环的每次迭代中注册了一个新的监听器,这是不必要的。你只需要注册一次。
- 你需要指定触发键,而不是字母/字符。对于回车键,这是
"enter"
,而不是"\n"
。
以下是如何使其运行的简要示例:
import time
import keyboard
timer = 1
enabled = False
def callback():
global enabled
enabled = True
# 传递回调函数对象,而不是回调函数的结果
keyboard.register_word_listener("enable", callback, ["enter"])
while True:
if enabled:
time.sleep(0.1)
print("urine", end="")
time.sleep(0.1)
print("\n", end="")
time.sleep(timer)
请注意,我已经进行了必要的修正。
英文:
There are a couple of issues in your code.
The main one is that rather than passing your enable
callback function object to your listener you are calling it (by using the parentheses ()
) and passing its return value to the listener. The default return value in python is None
, so nothing is happening when the listener is activated, however, every time the listener is registered, the callback function is run, causing your script to output its message prematurely.
The solution is simply to omit the parenthesis.
The other minor issues are :
- you're registering a new listener at every iteration of your loop, which is not necessary. you only need to register it once.
- you need to specify the trigger key(s), not letters/characters. For the enter/return key, this is
"enter"
, rather than"\n
.
Below a small example of how you might get it to run :
import time
import keyboard
timer = 1
enabled = False
def callback():
global enabled
enabled = True
# pass the callback function object, not the result of the callback function
keyboard.register_word_listener("enable", callback, ["enter"])
while True:
if enabled:
time.sleep(0.1)
print("urine", end="")
time.sleep(0.1)
print("\n", end="")
time.sleep(timer)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论