77#include " src/core/qguiappcurrentscreen.h"
88#include " src/utils/globalvalues.h"
99#include " toolfactory.h"
10+ #include < QCheckBox>
1011#include < QCursor>
12+ #include < QDir>
1113#include < QHeaderView>
1214#include < QIcon>
1315#include < QKeyEvent>
1416#include < QLabel>
17+ #include < QMessageBox>
1518#include < QRect>
1619#include < QScreen>
1720#include < QStringList>
@@ -34,11 +37,20 @@ ShortcutsWidget::ShortcutsWidget(QWidget* parent)
3437 m_layout = new QVBoxLayout (this );
3538 m_layout->setAlignment (Qt::AlignHCenter | Qt::AlignVCenter);
3639
40+ #if defined(Q_OS_WIN)
41+ checkPrintScreenForcesSnipping ();
42+ #endif
43+
3744 initInfoTable ();
3845 connect (ConfigHandler::getInstance (),
3946 &ConfigHandler::fileChanged,
4047 this ,
4148 &ShortcutsWidget::populateInfoTable);
49+
50+ #if defined(Q_OS_WIN)
51+ initMsScreenclipCheckbox ();
52+ #endif
53+
4254 show ();
4355}
4456
@@ -202,13 +214,15 @@ void ShortcutsWidget::loadShortcuts()
202214 appendShortcut (" SCREENSHOT_HISTORY" , tr (" Screenshot history" ));
203215#endif
204216#elif defined(Q_OS_WIN)
205-
217+ if (this ->isPrintScreenKeyForSnippingDisabled ()) {
218+ m_shortcuts << (QStringList () << " " << QObject::tr (" Capture screen" )
219+ << " Print Screen" );
220+ }
221+ appendShortcut (" TAKE_SCREENSHOT" , tr (" Capture screen" ));
206222#ifdef ENABLE_IMGUR
207223 m_shortcuts << (QStringList () << " " << QObject::tr (" Screenshot history" )
208224 << " Shift+Print Screen" );
209225#endif
210- m_shortcuts << (QStringList ()
211- << " " << QObject::tr (" Capture screen" ) << " Print Screen" );
212226#else
213227 // TODO - Linux doesn't support global shortcuts for (XServer and Wayland),
214228 // possibly it will be solved in the QHotKey library later. So it is
@@ -241,3 +255,195 @@ const QString& ShortcutsWidget::nativeOSHotKeyText(const QString& text)
241255 return m_res;
242256}
243257#endif
258+
259+ #if defined(Q_OS_WIN)
260+ void ShortcutsWidget::checkPrintScreenForcesSnipping ()
261+ {
262+ if (!isPrintScreenKeyForSnippingDisabled () &&
263+ !ConfigHandler ().ignorePrntScrForcesSnipping ()) {
264+ QMessageBox msgBox;
265+ msgBox.setWindowTitle (" Flameshot" );
266+ msgBox.setIcon (QMessageBox::Question);
267+ msgBox.setText (tr (" It seems, that Windows forces to open its screenshot"
268+ " tool when the 'Print Screen' key is pressed. Would "
269+ " you like to disable this so that Flameshot can use "
270+ " the 'Print Screen' key?" ) +
271+ " \n\n " +
272+ tr (" Flameshot must be restarted for changes to take "
273+ " effect." ));
274+ QPushButton* yesBtn = msgBox.addButton (QMessageBox::Yes);
275+ QPushButton* noBtn = msgBox.addButton (QMessageBox::No);
276+ QPushButton* noDontAskAgainBtn =
277+ new QPushButton (tr (" No, don't ask again" ));
278+ msgBox.addButton (noDontAskAgainBtn, QMessageBox::RejectRole);
279+ msgBox.setDefaultButton (yesBtn);
280+ msgBox.exec ();
281+
282+ if (msgBox.clickedButton () == yesBtn) {
283+ if (!disablePrintScreenKeyForSnipping ()) {
284+ QMessageBox::warning (
285+ this , " Flameshot" , tr (" The registry could not be changed!" ));
286+ }
287+ } else if (msgBox.clickedButton () == noDontAskAgainBtn) {
288+ ConfigHandler ().setIgnorePrntScrForcesSnipping (true );
289+ }
290+ }
291+ }
292+
293+ bool ShortcutsWidget::isPrintScreenKeyForSnippingDisabled ()
294+ {
295+ QSettings PrintKeyForSnipping (" HKEY_CURRENT_USER\\ Control Panel\\ Keyboard" ,
296+ QSettings::NativeFormat);
297+ return PrintKeyForSnipping.value (" PrintScreenKeyForSnippingEnabled" , 1 )
298+ .toInt () == 0 ;
299+ }
300+
301+ bool ShortcutsWidget::disablePrintScreenKeyForSnipping ()
302+ {
303+ QSettings PrintKeyForSnipping (" HKEY_CURRENT_USER\\ Control Panel\\ Keyboard" ,
304+ QSettings::NativeFormat);
305+ PrintKeyForSnipping.setValue (" PrintScreenKeyForSnippingEnabled" , 0 );
306+ PrintKeyForSnipping.sync ();
307+ if (QSettings::AccessError == PrintKeyForSnipping.status ()) {
308+ return false ;
309+ }
310+ return this ->isPrintScreenKeyForSnippingDisabled ();
311+ }
312+
313+ void ShortcutsWidget::initMsScreenclipCheckbox ()
314+ {
315+ m_registerMsScreenclip =
316+ new QCheckBox (tr (" Register Flameshot as MS-SCREENCLIP application "
317+ " (administrator privileges required)" ),
318+ this );
319+ m_registerMsScreenclip->setToolTip (
320+ tr (" After registering, you can select Flameshot as the default "
321+ " screenshot application in Windows Settings." ));
322+ m_registerMsScreenclip->setChecked (isMsScreenclipRegistered ());
323+ m_layout->addWidget (m_registerMsScreenclip);
324+
325+ connect (
326+ m_registerMsScreenclip, &QCheckBox::clicked, this , [this ](bool checked) {
327+ if (checked) {
328+ if (!registerMsScreenclip ()) {
329+ QMessageBox::warning (
330+ this ,
331+ " Flameshot" ,
332+ tr (" The registry could not be changed!" ) + " \n " +
333+ tr (" You may start Flameshot as administrator ONCE and "
334+ " try again!" ));
335+ m_registerMsScreenclip->setChecked (false );
336+ }
337+ } else {
338+ if (!unregisterMsScreenclip ()) {
339+ QMessageBox::warning (
340+ this ,
341+ " Flameshot" ,
342+ tr (" The registry could not be changed!" ) + " \n " +
343+ tr (" You may start Flameshot as administrator ONCE and "
344+ " try again!" ));
345+ m_registerMsScreenclip->setChecked (true );
346+ }
347+ }
348+ });
349+ }
350+
351+ bool ShortcutsWidget::isMsScreenclipRegistered ()
352+ {
353+ QSettings URLAssociations (
354+ " HKEY_LOCAL_MACHINE\\ SOFTWARE\\ Flameshot\\ Capabilities\\ URLAssociations" ,
355+ QSettings::NativeFormat);
356+ QString value = URLAssociations.value (" ms-screenclip" , " " ).toString ();
357+ if (value.toLower () != " flameshot" )
358+ return false ;
359+
360+ QSettings RegisteredApplications (
361+ " HKEY_LOCAL_MACHINE\\ SOFTWARE\\ RegisteredApplications" ,
362+ QSettings::NativeFormat);
363+ value = RegisteredApplications.value (" Flameshot" , " " ).toString ();
364+ if (value.toLower () !=
365+ QString (" SOFTWARE\\ Flameshot\\ Capabilities" ).toLower ())
366+ return false ;
367+
368+ QSettings FlameshotShellCmd (
369+ " HKEY_CURRENT_USER\\ Software\\ Classes\\ Flameshot\\ Shell\\ Open\\ command" ,
370+ QSettings::NativeFormat);
371+ value = FlameshotShellCmd.value (" ." ).toString ();
372+ if (value.toLower () != QString (" \" " +
373+ QDir::toNativeSeparators (
374+ QCoreApplication::applicationFilePath ()) +
375+ " \" gui" )
376+ .toLower ())
377+ return false ;
378+
379+ return true ; // All registry entries found
380+ }
381+
382+ bool ShortcutsWidget::registerMsScreenclip ()
383+ {
384+ QSettings URLAssociations (
385+ " HKEY_LOCAL_MACHINE\\ SOFTWARE\\ Flameshot\\ Capabilities\\ URLAssociations" ,
386+ QSettings::NativeFormat);
387+ URLAssociations.setValue (" ms-screenclip" , " Flameshot" );
388+ URLAssociations.sync ();
389+ if (QSettings::AccessError == URLAssociations.status ()) {
390+ return false ;
391+ }
392+
393+ QSettings RegisteredApplications (
394+ " HKEY_LOCAL_MACHINE\\ SOFTWARE\\ RegisteredApplications" ,
395+ QSettings::NativeFormat);
396+ RegisteredApplications.setValue (" Flameshot" ,
397+ " SOFTWARE\\ Flameshot\\ Capabilities" );
398+ RegisteredApplications.sync ();
399+ if (QSettings::AccessError == RegisteredApplications.status ()) {
400+ return false ;
401+ }
402+
403+ QSettings FlameshotShellCmd (
404+ " HKEY_CURRENT_USER\\ Software\\ Classes\\ Flameshot\\ Shell\\ Open\\ command" ,
405+ QSettings::NativeFormat);
406+ FlameshotShellCmd.setValue (
407+ " ." ,
408+ " \" " + QDir::toNativeSeparators (QCoreApplication::applicationFilePath ()) +
409+ " \" gui" );
410+ FlameshotShellCmd.sync ();
411+ if (QSettings::AccessError == FlameshotShellCmd.status ()) {
412+ return false ;
413+ }
414+
415+ return isMsScreenclipRegistered ();
416+ }
417+
418+ bool ShortcutsWidget::unregisterMsScreenclip ()
419+ {
420+ QSettings FlameshotShellCmd (" HKEY_CURRENT_USER\\ Software\\ Classes" ,
421+ QSettings::NativeFormat);
422+ FlameshotShellCmd.remove (" Flameshot" );
423+ FlameshotShellCmd.sync ();
424+ if (QSettings::AccessError == FlameshotShellCmd.status ()) {
425+ return false ;
426+ }
427+
428+ QSettings RegisteredApplications (
429+ " HKEY_LOCAL_MACHINE\\ SOFTWARE\\ RegisteredApplications" ,
430+ QSettings::NativeFormat);
431+ RegisteredApplications.remove (" Flameshot" );
432+ RegisteredApplications.sync ();
433+ if (QSettings::AccessError == RegisteredApplications.status ()) {
434+ return false ;
435+ }
436+
437+ QSettings URLAssociations (
438+ " HKEY_LOCAL_MACHINE\\ SOFTWARE\\ Flameshot\\ Capabilities\\ URLAssociations" ,
439+ QSettings::NativeFormat);
440+ URLAssociations.remove (" ms-screenclip" );
441+ URLAssociations.sync ();
442+ if (QSettings::AccessError == URLAssociations.status ()) {
443+ return false ;
444+ }
445+
446+ return !isMsScreenclipRegistered ();
447+ }
448+
449+ #endif
0 commit comments