Форум сайта python.su
Hello Everybody!!!
I have a problem with SystemTray and PyQt…
I want to write little project, which calculate how many times we press “ENTER”
when I minimize my program to “tray” and open another window then press “ENTER”, in this case program doesn't calucalate. How can I running my program in SystemTray?
my code is the following…
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
class SystemTrayIcon(QtGui.QSystemTrayIcon):
def __init__(self, parent=None):
QtGui.QSystemTrayIcon.__init__(self, parent)
self.setIcon(QtGui.QIcon(“icon.jpg”))
self.mainMenu = QtGui.QMenu(parent)
#==================== M E N U ====================
systemInformation = self.mainMenu.addAction(“System Information”)
aboutButton = self.mainMenu.addAction(“About”)
exitButton = self.mainMenu.addAction(“Exit”)
self.setContextMenu(self.mainMenu)
#==================== EVENT ======================
self.connect(systemInformation,QtCore.SIGNAL('triggered()'),self.keyPressEvent)
self.connect(aboutButton,QtCore.SIGNAL('triggered()'),self.showAbout)
self.connect(exitButton,QtCore.SIGNAL('triggered()'),self.appExit)
self.show()
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_Enter: # IT DOESN'T WORK WHEN I MINIMIZE PROGRAM
print “some text”
def showAbout(self):
QtGui.QMessageBox.information(QtGui.QWidget(), self.tr(“About Tunarium”), self.tr(“Your text here.”))
def appExit(self):
sys.exit()
if __name__ == “__main__”:
app = QtGui.QApplication(sys.argv)
trayIcon = SystemTrayIcon()
trayIcon.show()
sys.exit(app.exec_())
Thanks!!!!
Офлайн
You can't capture key events when QT widget is not in focus. There is only platform depended ways to capture all key presses. Take a look at http://www.qtforum.org/article/33010/capturing-a-system-key-press-when-qt-form-is-not-in-focus-using-winevent.html
Also, if you only need to check if key is pressed on Windows, you can use GetAsyncKeyState function.
import win32api import win32con if win32api.GetKeyState(win32con.VK_RETURN) & 0x8000: print "some text"
Офлайн
reclosedev
You can't capture key events when QT widget is not in focus. There is only platform depended ways to capture all key presses. Take a look at http://www.qtforum.org/article/33010/capturing-a-system-key-press-when-qt-form-is-not-in-focus-using-winevent.htmlAlso, if you only need to check if key is pressed on Windows, you can use GetAsyncKeyState function.
Офлайн
You need to check GetAsyncKeyState() in loop or with timer.
Try this
http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=PyHook_Tutorial#tocpyHook_Tutorial4
Офлайн