首页 文章

Python for Maya:检测CTRL S然后模拟Return键击

提问于
浏览
0

我需要一个在后台(或理想情况下,在Maya中)运行的Python脚本,它执行以下操作:

  • 脚本正在运行

  • 我按下Ctrl S,脚本检测到它

  • 脚本模拟返回击键

  • 脚本正在运行

目前,根据这里和那里的一些答案,我可以成功检测到CTRL S键击 . 我尝试了this answer to emulate a keystroke with WScript.Shell,但没有成功 .

What I'm still missing: 在脚本检测到CTRL S击键后立即模拟返回击键(即步骤3) .

我的代码:

import Tkinter as tk
import win32com.client as comclt

class App(object):
    def __init__(self):
        self.comboKeys = False
        self.enterKey = False


    def keyPressed(self,event):
        print "--"

        # if Esc is pressed, stop script
        if event.keysym == 'Escape':
            root.destroy()

        # if CTRL+S is pressed
        elif event.keysym == 's':
            self.comboKeys = True


    def keyReleased(self,event):
        if event.keysym == 's':
            self.comboKeys = False


    def task(self):
        if self.comboKeys:
            print 'CTRL+S key pressed!'

        root.after(20,self.task)

application = App()
root = tk.Tk()
print( "Press arrow key (Escape key to exit):" )

root.bind_all('', application.keyPressed)
root.bind_all('', application.keyReleased)
root.after(20,application.task)

root.mainloop()

非常感谢你!如果我遗漏了任何信息,请告诉我 .

1 回答

  • 0

    如果您已经提交了TK并且需要运行它而不是直接与Maya交互,那么您可以使用TK应用程序启动一个单独的进程并通过maya命令端口或使用像rpyc这样的库进行通信 . 或zeromq将事件发送给Maya . 这很痛苦,因为你必须来回串行化通信 .

    如果您对应用程序内部的内容更加清楚,它可能会对我们有所帮助 . 是你试图做的文字输入吗?

相关问题