Python Forum
PySide2 Hotkey works only once in Maya
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PySide2 Hotkey works only once in Maya
#1
I want to open a window that is defined in this script by my custom hotkey, in this case CTRL + TAB. I have this script in Maya(2020):

from PySide2 import QtWidgets, QtCore, QtGui
from PySide2.QtCore import QPropertyAnimation, QEasingCurve, QSize
from PySide2.QtGui import QKeySequence, QIcon, QFont, QPixmap, QWindow
from PySide2.QtWidgets import QDesktopWidget, QAction, QCheckBox, QDialog, QShortcut
from maya import OpenMayaUI
from functools import partial
import maya.cmds as cmds
import sys
import os

try:
	from shiboken import wrapInstance
	import shiboken
except:
	from shiboken2 import wrapInstance
	import shiboken2 as shiboken

class MainWindow(QtWidgets.QMainWindow):
	def __init__(self, parent = None):
		window = OpenMayaUI.MQtUtil.mainWindow()
		mayaWindow = shiboken.wrapInstance(long(window), QtWidgets.QMainWindow)
		super(MainWindow, self).__init__(mayaWindow)

		self.setWindowTitle('Test Window')
		self.setStyleSheet("background-color: rgb(65, 65, 65);")
		self.setWindowFlags(QtCore.Qt.Popup | QtCore.Qt.WindowType.NoDropShadowWindowHint)
		self.resize(630, 400)
		self.releaseKeyboard()
		self.releaseMouse()

		# main widget
		mainWidget = QtWidgets.QWidget(self)
		self.setCentralWidget(mainWidget)

		# layout initialize
		self.mainLayout = QtWidgets.QVBoxLayout()
		mainWidget.setLayout(self.mainLayout)
		self.shortcutSetup()
		self.initialize()


	def shortcutSetup(self):
		QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Escape), self, activated = self.closeWindow)

	def initialize(self, *args):
		cmds.evalDeferred('SetHotkey()', lp = True)

	def closeWindow(self):
		self.hide()


def SetHotkey(*args):
	mayaVersion = cmds.about(version=True)

	operatingSystem = sys.platform
	mainWindow = wrapInstance(long(OpenMayaUI.MQtUtil.mainWindow()), QtWidgets.QWidget)
	launcherAction = QtWidgets.QAction(mainWindow)
	hotkey = 'CTRL + TAB'
	launcherAction.setShortcut(QtGui.QKeySequence(QtCore.Qt.CTRL + QtCore.Qt.Key_Tab))

	def launcherHotkeyStrike(*args):
		print 'launcherHotkeyStrike'
		cmds.evalDeferred('StartApp()')

	launcherAction.setShortcutContext(QtCore.Qt.ApplicationShortcut)
	print hotkey
	launcherAction.triggered.connect(launcherHotkeyStrike)
	mainWindow.addAction(launcherAction)


def StartApp(*args):
	app = QtWidgets.QApplication.instance()
	if app is None: 
		app = QtWidgets.QApplication(sys.argv)

	w = MainWindow()
	w.show()
	#sys.exit(app.exec_())


if __name__ == '__main__':
	StartApp()
It only works as intended the first time, that means that a window pops up when I run the hotkey but the second time nothing happens. Why is that so?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to call an object in another function in Maya bstout 0 2,077 Apr-05-2021, 07:12 PM
Last Post: bstout
  how to make a hotkey for text adventure game myn2018 2 1,966 Jan-06-2021, 10:39 PM
Last Post: myn2018
  Making WebBrowser In PySide2 Harshil 0 1,892 Sep-16-2020, 05:03 PM
Last Post: Harshil
  Hotkey to restart code? DannyB 1 2,749 May-20-2020, 02:52 AM
Last Post: michael1789
  Terminate a process when hotkey is pressed 66Gramms 0 2,240 Dec-24-2019, 06:41 PM
Last Post: 66Gramms
  Code Clean-up and Maya/Python Slicing issues mvvthology 1 2,415 Aug-05-2019, 10:43 PM
Last Post: Yoriz
  pyautogui hotkey Rolfi05 0 3,276 Dec-27-2018, 04:28 PM
Last Post: Rolfi05
  Maya Python Noob Question Iurii_Ledin 2 3,276 Jun-08-2018, 09:09 PM
Last Post: Iurii_Ledin
  Python in MAYA I_suck_at_python 0 3,627 Apr-24-2017, 10:25 PM
Last Post: I_suck_at_python

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020