class application(QApplication): def __init__(self, argv): QApplication.__init__(self,argv) self.shortcut_copy = QxtGlobalShortcut() self.shortcut_copy.setShortcut(QKeySequence(SHORTCUT_COPY)) self.holder = contentholder() self.shortcut_copy.activated.connect(self.holder.contentchanged)
def __init__(self, icon, settings, parent=None): self.settings = settings QSystemTrayIcon.__init__(self, icon, parent) self.mainMenu = QMenu() self.actCapture = QAction(QIcon(":/config.png"), "Capture", self, triggered=self.onCapture) self.actCapture.setShortcut( QKeySequence.fromString(settings.value('SnapShotKey'))) self.mainMenu.addAction(self.actCapture) self.mainMenu.addAction( QAction("Set save directory...", self, triggered=self.onSetSavePath)) self.mainMenu.addAction( QAction("Set shortcut...", self, triggered=self.onConfigureShortCut)) self.mainMenu.addAction(QAction("Exit", self, triggered=self.onExit)) self.setContextMenu(self.mainMenu) self.activated.connect(self.onActived) self.gsKey = QxtGlobalShortcut() self.gsKey.setShortcut( QKeySequence.fromString(settings.value('SnapShotKey'))) self.gsKey.activated.connect(self.onCapture)
def test_functional(): app = QApplication(sys.argv) shortcut = QxtGlobalShortcut() shortcut.setShortcut(QKeySequence('Ctrl+Alt+E')) shortcut.activated.connect(app.exit) QTimer.singleShot(100, shortcut.activated.emit) app.exec_()
def CreateGlobalHotkey(keyCombo, func): """Create a global hotkey, connecting a callback function to a key or key combination (example: "Ctrl+Z"). A global hotkey can be created after an application has started running, but it must be created after the application has been created. Returns whether a global hotkey was set up or not.""" if QtLib() == PYQT4: shortcut = QxtGlobalShortcut() shortcut.setShortcut(QKeySequence(keyCombo)) shortcut.activated.connect(func) _shortcuts.append(shortcut) return True return False
def onConfigureShortCut(self): c = QXShortCutKeyConfigureDialog(self.settings.value('SnapShotKey'), ":/help.png") c.windowTitle = 'QuickCapture' c.exec_() if not c.key is None: self.settings.setValue('SnapShotKey', c.key) del self.gsKey self.gsKey = QxtGlobalShortcut() self.gsKey.setShortcut( QKeySequence.fromString(self.settings.value('SnapShotKey'))) self.gsKey.activated.connect(self.onCapture) self.actCapture.setShortcut( QKeySequence.fromString(self.settings.value('SnapShotKey')))
def onConfigureShortCut(self): c = QXShortCutKeyConfigureDialog(self.settings.value('SnapShotKey'), ":/help.png") c.windowTitle = 'QuickCapture' c.exec_() if not c.key is None: self.settings.setValue('SnapShotKey',c.key) del self.gsKey self.gsKey = QxtGlobalShortcut() self.gsKey.setShortcut(QKeySequence.fromString(self.settings.value('SnapShotKey'))) self.gsKey.activated.connect(self.onCapture) self.actCapture.setShortcut(QKeySequence.fromString(self.settings.value('SnapShotKey')))
def __init__(self,icon,settings,parent = None): self.settings = settings QSystemTrayIcon.__init__(self,icon,parent) self.mainMenu = QMenu() self.actCapture = QAction(QIcon(":/config.png"),"Capture",self,triggered=self.onCapture) self.actCapture.setShortcut(QKeySequence.fromString(settings.value('SnapShotKey'))) self.mainMenu.addAction(self.actCapture) self.mainMenu.addAction(QAction("Set save directory...",self,triggered=self.onSetSavePath)) self.mainMenu.addAction(QAction("Set shortcut...",self,triggered=self.onConfigureShortCut)) self.mainMenu.addAction(QAction("Exit",self,triggered=self.onExit)) self.setContextMenu(self.mainMenu) self.activated.connect(self.onActived) self.gsKey = QxtGlobalShortcut() self.gsKey.setShortcut(QKeySequence.fromString(settings.value('SnapShotKey'))) self.gsKey.activated.connect(self.onCapture)
class MainController(QSystemTrayIcon): appExit = pyqtSignal() def __init__(self,icon,settings,parent = None): self.settings = settings QSystemTrayIcon.__init__(self,icon,parent) self.mainMenu = QMenu() self.actCapture = QAction(QIcon(":/config.png"),"Capture",self,triggered=self.onCapture) self.actCapture.setShortcut(QKeySequence.fromString(settings.value('SnapShotKey'))) self.mainMenu.addAction(self.actCapture) self.mainMenu.addAction(QAction("Set save directory...",self,triggered=self.onSetSavePath)) self.mainMenu.addAction(QAction("Set shortcut...",self,triggered=self.onConfigureShortCut)) self.mainMenu.addAction(QAction("Exit",self,triggered=self.onExit)) self.setContextMenu(self.mainMenu) self.activated.connect(self.onActived) self.gsKey = QxtGlobalShortcut() self.gsKey.setShortcut(QKeySequence.fromString(settings.value('SnapShotKey'))) self.gsKey.activated.connect(self.onCapture) def onActived(self,r): if r == QSystemTrayIcon.Trigger: self.onClick() elif r == QSystemTrayIcon.DoubleClick: self.onCapture() def onCapture(self): self.w = CaptureWin(self.settings) self.w.captureDone.connect(self.onCaptureMessage) self.w.show() def onClick(self): self.showMessage("meo~","Press %s to capture" % self.settings.value('SnapShotKey'),QSystemTrayIcon.Information,50) def onConfigureShortCut(self): c = QXShortCutKeyConfigureDialog(self.settings.value('SnapShotKey'), ":/help.png") c.windowTitle = 'QuickCapture' c.exec_() if not c.key is None: self.settings.setValue('SnapShotKey',c.key) del self.gsKey self.gsKey = QxtGlobalShortcut() self.gsKey.setShortcut(QKeySequence.fromString(self.settings.value('SnapShotKey'))) self.gsKey.activated.connect(self.onCapture) self.actCapture.setShortcut(QKeySequence.fromString(self.settings.value('SnapShotKey'))) def onSetSavePath(self): d = QFileDialog.getExistingDirectory(None,"Open Directory", QDesktopServices.storageLocation(QDesktopServices.HomeLocation),QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks) if (not d is None) and (not d == '') and (QDir().exists(d)): self.settings.setValue('SnapShotSaveDir',d) self.showMessage("QuickCapture","Set save directory to %s" % d,QSystemTrayIcon.Information,50) def onCaptureMessage(self,s): self.showMessage("QuickCapture","Capture screen snapshot to %s" % s,QSystemTrayIcon.Information,50) def onExit(self): rtn = QMessageBox.question(None,"QuickCapture","Quit now?",QMessageBox.Yes | QMessageBox.No,QMessageBox.No) if rtn == QMessageBox.Yes: del self.gsKey QApplication.exit(0)
if os.environ['QT_SELECT'] == '4': from PyQt4.QtGui import QApplication, QKeySequence else: from PyQt5.QtWidgets import QApplication from PyQt5.QtGui import QKeySequence from pygs import QxtGlobalShortcut SHORTCUT_SHOW = "Ctrl+Alt+S" # Ctrl maps to Command on Mac OS X SHORTCUT_EXIT = "Ctrl+Alt+F" # again, Ctrl maps to Command on Mac OS X def show_activated(): print("Shortcut Activated!") app = QApplication([]) shortcut_show = QxtGlobalShortcut() shortcut_show.setShortcut(QKeySequence(SHORTCUT_SHOW)) shortcut_show.activated.connect(show_activated) shortcut_exit = QxtGlobalShortcut() shortcut_exit.setShortcut(QKeySequence(SHORTCUT_EXIT)) shortcut_exit.activated.connect(app.exit) return_code = app.exec_() del shortcut_show del shortcut_exit sys.exit(return_code)
if __name__ == '__main__': import sys app = QApplication(sys.argv) if not QtGui.QSystemTrayIcon.isSystemTrayAvailable(): QtGui.QMessageBox.critical(None, "Systray", "I couldn't detect any system tray on this system.") sys.exit(1) QtGui.QApplication.setQuitOnLastWindowClosed(False) SHORTCUT_SHOW = "Ctrl+Alt+P" # On Mac OS X: Ctrl maps to Command; Meta maps to Ctrl. # window = Window() # window.show() # window.raise_() w = QtGui.QWidget() trayIcon = SystemTrayIcon(w) trayIcon.show() shortcut_show = QxtGlobalShortcut() shortcut_show.setShortcut(QKeySequence(SHORTCUT_SHOW)) shortcut_show.activated.connect(trayIcon.showQuickpanel) return_code = app.exec_() del shortcut_show sys.exit(return_code)
try: from PyQt5.QtWidgets import QApplication from PyQt5.QtGui import QKeySequence except ImportError: from PyQt4.QtGui import QApplication, QKeySequence from pygs import QxtGlobalShortcut SHORTCUT_SHOW = "Ctrl+Alt+S" # Ctrl maps to Command on Mac OS X SHORTCUT_EXIT = "Ctrl+Alt+F" # again, Ctrl maps to Command on Mac OS X def show_activated(): print("Shortcut Activated!") app = QApplication([]) shortcut_show = QxtGlobalShortcut() shortcut_show.setShortcut(QKeySequence(SHORTCUT_SHOW)) shortcut_show.activated.connect(show_activated) shortcut_exit = QxtGlobalShortcut() shortcut_exit.setShortcut(QKeySequence(SHORTCUT_EXIT)) shortcut_exit.activated.connect(app.exit) return_code = app.exec_() del shortcut_show del shortcut_exit sys.exit(return_code)
def setGlobalHotkeys(self): self.sysTrayMenuRegionGlobalHotkey = QxtGlobalShortcut() self.sysTrayMenuRegionGlobalHotkey.setShortcut(QKeySequence(self.hotkey)) self.sysTrayMenuRegionGlobalHotkey.activated.connect(self.createDrawSurface)
class trayApp(QSystemTrayIcon): def __init__(self, parent=None): super(trayApp, self).__init__(parent) self.loadSettings() self.setIcon(QIcon("icon.png")) self.createSysTrayMenu() self.setGlobalHotkeys() self.imgurClient = Imgur("", "") self.clipboard = QApplication.clipboard() def createSysTrayMenu(self): self.sysTrayMenu = QMenu() self.setContextMenu(self.sysTrayMenu) self.createSysTrayActions() def createSysTrayActions(self): self.sysTrayMenuRegionAction = self.createAction("&Capture region", self.createDrawSurface, self.hotkey) self.sysTrayMenuHotkeyAction = self.createAction("&Change hotkey...", self.createKeySequenceDialog) self.sysTrayMenuUploadAction = self.createAction("&Upload to imgur", checkable=True) self.sysTrayMenuUploadAction.setChecked(self.upload) self.sysTrayMenuSaveAction = self.createAction("&Save locally", checkable=True) self.sysTrayMenuSaveAction.setChecked(self.save) self.sysTrayMenuExitAction = self.createAction("&Exit", self.quit) self.addActions(self.sysTrayMenu, (self.sysTrayMenuRegionAction, None, self.sysTrayMenuHotkeyAction, self.sysTrayMenuUploadAction, self.sysTrayMenuSaveAction, self.sysTrayMenuExitAction)) def createAction(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False): action = QAction(text, self) if slot is not None: action.triggered.connect(slot) if shortcut is not None: action.setShortcut(shortcut) if icon is not None: action.setIcon(QIcon(":/{}.png".format(icon))) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if checkable: action.setCheckable(True) return action def addActions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def createKeySequenceDialog(self): self.keySequenceDialog = KeySequenceDialog(self.hotkey) self.keySequenceDialog.accepted.connect(self.changeHotkey) self.keySequenceDialog.show() def changeHotkey(self): text = self.keySequenceDialog.keySequenceLineEdit.text() if text: self.hotkey = text self.sysTrayMenuRegionAction.setShortcut(self.hotkey) self.sysTrayMenuRegionGlobalHotkey.setShortcut(QKeySequence(self.hotkey)) def loadSettings(self): settings = QSettings("ninjapic", "settings") self.upload = settings.value("upload").toBool() self.save = settings.value("save").toBool() self.hotkey = settings.value("hotkey").toString() if not self.hotkey: self.hotkey = "Alt+C" def saveSettings(self): settings = QSettings("ninjapic", "settings") settings.setValue("upload", self.sysTrayMenuUploadAction.isChecked()) settings.setValue("save", self.sysTrayMenuSaveAction.isChecked()) settings.setValue("hotkey", self.hotkey) def createDrawSurface(self): self.surface = Surface() self.surface.imageReady.connect(self.storeImage) self.surface.show() def setGlobalHotkeys(self): self.sysTrayMenuRegionGlobalHotkey = QxtGlobalShortcut() self.sysTrayMenuRegionGlobalHotkey.setShortcut(QKeySequence(self.hotkey)) self.sysTrayMenuRegionGlobalHotkey.activated.connect(self.createDrawSurface) def storeImage(self): if not self.surface.image.save("screenshot.png", "PNG", -1): QMessageBox.warning(None, "Image Error", "The image couldn't be saved.") return if self.sysTrayMenuUploadAction.isChecked(): try: uploaded_image = self.imgurClient.upload_image("screenshot.png") self.clipboard.setText(uploaded_image.link) webbrowser.open(uploaded_image.link) except HTTPError, e: self.surface.dispose() QMessageBox.warning(None, "Imgur Error", unicode(e)) if not self.sysTrayMenuSaveAction.isChecked(): os.remove("screenshot.png")
class MainController(QSystemTrayIcon): appExit = pyqtSignal() def __init__(self, icon, settings, parent=None): self.settings = settings QSystemTrayIcon.__init__(self, icon, parent) self.mainMenu = QMenu() self.actCapture = QAction(QIcon(":/config.png"), "Capture", self, triggered=self.onCapture) self.actCapture.setShortcut( QKeySequence.fromString(settings.value('SnapShotKey'))) self.mainMenu.addAction(self.actCapture) self.mainMenu.addAction( QAction("Set save directory...", self, triggered=self.onSetSavePath)) self.mainMenu.addAction( QAction("Set shortcut...", self, triggered=self.onConfigureShortCut)) self.mainMenu.addAction(QAction("Exit", self, triggered=self.onExit)) self.setContextMenu(self.mainMenu) self.activated.connect(self.onActived) self.gsKey = QxtGlobalShortcut() self.gsKey.setShortcut( QKeySequence.fromString(settings.value('SnapShotKey'))) self.gsKey.activated.connect(self.onCapture) def onActived(self, r): if r == QSystemTrayIcon.Trigger: self.onClick() elif r == QSystemTrayIcon.DoubleClick: self.onCapture() def onCapture(self): self.w = CaptureWin(self.settings) self.w.captureDone.connect(self.onCaptureMessage) self.w.show() def onClick(self): self.showMessage( "meo~", "Press %s to capture" % self.settings.value('SnapShotKey'), QSystemTrayIcon.Information, 50) def onConfigureShortCut(self): c = QXShortCutKeyConfigureDialog(self.settings.value('SnapShotKey'), ":/help.png") c.windowTitle = 'QuickCapture' c.exec_() if not c.key is None: self.settings.setValue('SnapShotKey', c.key) del self.gsKey self.gsKey = QxtGlobalShortcut() self.gsKey.setShortcut( QKeySequence.fromString(self.settings.value('SnapShotKey'))) self.gsKey.activated.connect(self.onCapture) self.actCapture.setShortcut( QKeySequence.fromString(self.settings.value('SnapShotKey'))) def onSetSavePath(self): d = QFileDialog.getExistingDirectory( None, "Open Directory", QDesktopServices.storageLocation(QDesktopServices.HomeLocation), QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks) if (not d is None) and (not d == '') and (QDir().exists(d)): self.settings.setValue('SnapShotSaveDir', d) self.showMessage("QuickCapture", "Set save directory to %s" % d, QSystemTrayIcon.Information, 50) def onCaptureMessage(self, s): self.showMessage("QuickCapture", "Capture screen snapshot to %s" % s, QSystemTrayIcon.Information, 50) def onExit(self): rtn = QMessageBox.question(None, "QuickCapture", "Quit now?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if rtn == QMessageBox.Yes: del self.gsKey QApplication.exit(0)