【问题标题】:Alt and Ctrl keys not working with keyboard.is_pressedAlt 和 Ctrl 键不适用于 keyboard.is_pressed
【发布时间】:2021-05-07 12:15:55
【问题描述】:

很简单,我试图创建一个代码,当您按下某些按钮时,它会写出您放入 Tk 输入的任何内容。我试图用 alt 和另一个字母创建一个热键,但是每当我使用 alt 或 ctrl 时,该功能都不起作用并在我的屏幕上打开随机应用程序。例如,使用 shift + a 之类的键一切正常,但由于使用了 shift,所以将其全部输出为大写字母。我编写热键的部分是所有热键变量的位置

from tkinter import *
import keyboard, pyautogui
import os
import sys
import tkinter.messagebox
import time

root = Tk()
root.resizable(False, False)
root.iconbitmap('Logo.ico')
root.title(" ")
root.geometry("170x300")
Canvas(root, width=170, height=300)

hotkey1 = "alt + q"
hotkey2 = "alt + w"
hotkey3 = "alt + e"
hotkey4 = "alt + r"

Label(root, text="MacMaker").place(x = 50)
Label(root, text="Type in Macros below:").place(x = 27, y = 21)
Label(root, text="F4:").place(x = 5, y = 48)
Label(root, text="F7:").place(x = 5, y = 78)
Label(root, text="F8:").place(x = 5, y = 108)
Label(root, text="F9:").place(x = 5, y = 138)

thing1 = Entry(width=20)
thing1.place(x = 35, y = 50)
thing2 = Entry(width=20)
thing2.place(x = 35, y = 80)
thing3 = Entry(width=20)
thing3.place(x = 35, y = 110)
thing4 = Entry(width=20)
thing4.place(x = 35, y = 140)


def my_mainloop():
    macro1 = thing1.get()
    macro2 = thing2.get()
    macro3 = thing3.get()
    macro4 = thing4.get()
    if keyboard.is_pressed(hotkey1):
        pyautogui.typewrite(macro1)
    elif keyboard.is_pressed(hotkey2):
        pyautogui.typewrite(macro2)
    elif keyboard.is_pressed(hotkey3):
        pyautogui.typewrite(macro3)
    elif keyboard.is_pressed(hotkey4):
        pyautogui.typewrite(macro4)
    root.after(1, my_mainloop)  

root.after(1, my_mainloop)
root.mainloop()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    docs看来应该是这样的:

    hotkey1 = "alt+q"
    hotkey2 = "alt+w"
    hotkey3 = "alt+e"
    hotkey4 = "alt+r"
    

    没有空格

    【讨论】:

    • 我在班次方面存在差距,但它奏效了。无论如何,我尝试过没有任何差距,但它似乎仍然无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 2016-09-25
    • 2014-09-12
    • 2014-01-13
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多