Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get a if a KeySequence is pressed in main(); (While Application Launch)
Forum Updated to NodeBB v4.3 + New Features

How to get a if a KeySequence is pressed in main(); (While Application Launch)

Scheduled Pinned Locked Moved Unsolved General and Desktop
30 Posts 3 Posters 4.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ademmlerA Offline
    ademmlerA Offline
    ademmler
    wrote on last edited by
    #1

    Hi there,

    what is the way to get a KeySequence() within main() - aka while Applikation launch ?
    When the MainWindow is active, this should have not influence any longer.

    Use case: Some Application do a Maintenance task, if ALT is pressed while the start phase.

    thx in Advance

    Pl45m4P 1 Reply Last reply
    1
    • ademmlerA ademmler

      Hi there,

      what is the way to get a KeySequence() within main() - aka while Applikation launch ?
      When the MainWindow is active, this should have not influence any longer.

      Use case: Some Application do a Maintenance task, if ALT is pressed while the start phase.

      thx in Advance

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @ademmler

      You could add a splash screen widget to your app which handles the key press events and triggers your maintenance action before your window shows up.
      Or check within your window constructor if the ALT key is down and then do want you want to do.

      Maybe even a custom QEventLoop which runs for like 1-2 seconds... in that time it checks if your keyevent was sent and then handles it before it exists again?!
      (don't know if that works for you)

      You could also make use of

      • https://doc.qt.io/qt-6/qguiapplication.html#keyboardModifiers

      it's static so you can call it before your window shows up


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      ademmlerA 2 Replies Last reply
      2
      • Pl45m4P Pl45m4

        @ademmler

        You could add a splash screen widget to your app which handles the key press events and triggers your maintenance action before your window shows up.
        Or check within your window constructor if the ALT key is down and then do want you want to do.

        Maybe even a custom QEventLoop which runs for like 1-2 seconds... in that time it checks if your keyevent was sent and then handles it before it exists again?!
        (don't know if that works for you)

        You could also make use of

        • https://doc.qt.io/qt-6/qguiapplication.html#keyboardModifiers

        it's static so you can call it before your window shows up

        ademmlerA Offline
        ademmlerA Offline
        ademmler
        wrote on last edited by
        #3

        @Pl45m4

        Hi, thx for all those nice hints. I ll check what is working for me and let you know.

        1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @ademmler

          You could add a splash screen widget to your app which handles the key press events and triggers your maintenance action before your window shows up.
          Or check within your window constructor if the ALT key is down and then do want you want to do.

          Maybe even a custom QEventLoop which runs for like 1-2 seconds... in that time it checks if your keyevent was sent and then handles it before it exists again?!
          (don't know if that works for you)

          You could also make use of

          • https://doc.qt.io/qt-6/qguiapplication.html#keyboardModifiers

          it's static so you can call it before your window shows up

          ademmlerA Offline
          ademmlerA Offline
          ademmler
          wrote on last edited by
          #4

          @Pl45m4

          ok, I tried the solution using a Splash screen. - bracuase it exists anyway in my aplication.
          Here is a stripped down code for testing. I get the splash screen, but not any Key-Event ...
          Any ideas whats wrong?

          main.cpp

          #include "mainwindow.h"
          #include "mysplashscreen.h"
          #include <QApplication>
          #include <QPixmap>
          #include <QTimer>
          
          int main(int argc, char *argv[]) {
              QApplication app(argc, argv);
          
              QPixmap pixmap(":logo");
              QSplashScreen splash(pixmap);
              splash.show();
              splash.raise();
              splash.activateWindow();
          
              qDebug() << "Splash screen is displayed.";
          
              // Delay splash screen
              QTimer::singleShot(3000, [&splash]() {
                  splash.finish(nullptr);
              });
          
              // Proceed to the main window initialization here
              MainWindow mainWindow;
              mainWindow.show();
          
              return app.exec();
          }
          

          mysplashscreen.cpp

          #include "mysplashscreen.h"
          
          MySplashScreen::MySplashScreen(const QPixmap &pixmap) : QSplashScreen(pixmap) {
          }
          
          void MySplashScreen::keyPressEvent(QKeyEvent *event) {
              qDebug() << "Key Pressed:" << event->key(); /
              if (event->key() == Qt::Key_Escape) {
                  qDebug() << "Escape key pressed during splash screen.";
                  this->close();
              }
              QSplashScreen::keyPressEvent(event); // Ensure base class handling as well
          }
          

          mysplashscreen.h

          #ifndef MY_SPLASH_SCREEN_H
          #define MY_SPLASH_SCREEN_H
          
          #include <QSplashScreen>
          #include <QKeyEvent>
          #include <QPixmap>
          #include <QDebug>
          
          class MySplashScreen : public QSplashScreen {
              Q_OBJECT 
          public:
              explicit MySplashScreen(const QPixmap &pixmap);
          
          protected:
              void keyPressEvent(QKeyEvent *event) override;
          };
          
          #endif // MY_SPLASH_SCREEN_H
          
          Pl45m4P 1 Reply Last reply
          0
          • ademmlerA ademmler

            @Pl45m4

            ok, I tried the solution using a Splash screen. - bracuase it exists anyway in my aplication.
            Here is a stripped down code for testing. I get the splash screen, but not any Key-Event ...
            Any ideas whats wrong?

            main.cpp

            #include "mainwindow.h"
            #include "mysplashscreen.h"
            #include <QApplication>
            #include <QPixmap>
            #include <QTimer>
            
            int main(int argc, char *argv[]) {
                QApplication app(argc, argv);
            
                QPixmap pixmap(":logo");
                QSplashScreen splash(pixmap);
                splash.show();
                splash.raise();
                splash.activateWindow();
            
                qDebug() << "Splash screen is displayed.";
            
                // Delay splash screen
                QTimer::singleShot(3000, [&splash]() {
                    splash.finish(nullptr);
                });
            
                // Proceed to the main window initialization here
                MainWindow mainWindow;
                mainWindow.show();
            
                return app.exec();
            }
            

            mysplashscreen.cpp

            #include "mysplashscreen.h"
            
            MySplashScreen::MySplashScreen(const QPixmap &pixmap) : QSplashScreen(pixmap) {
            }
            
            void MySplashScreen::keyPressEvent(QKeyEvent *event) {
                qDebug() << "Key Pressed:" << event->key(); /
                if (event->key() == Qt::Key_Escape) {
                    qDebug() << "Escape key pressed during splash screen.";
                    this->close();
                }
                QSplashScreen::keyPressEvent(event); // Ensure base class handling as well
            }
            

            mysplashscreen.h

            #ifndef MY_SPLASH_SCREEN_H
            #define MY_SPLASH_SCREEN_H
            
            #include <QSplashScreen>
            #include <QKeyEvent>
            #include <QPixmap>
            #include <QDebug>
            
            class MySplashScreen : public QSplashScreen {
                Q_OBJECT 
            public:
                explicit MySplashScreen(const QPixmap &pixmap);
            
            protected:
                void keyPressEvent(QKeyEvent *event) override;
            };
            
            #endif // MY_SPLASH_SCREEN_H
            
            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @ademmler said in How to get a if a KeySequence is pressed in main(); (While Application Launch):

            // Delay splash screen
            QTimer::singleShot(3000, [&splash]() {
                splash.finish(nullptr);
            });
            

            Try this instead:

            MainWindow mainWindow;
            splash.showMessage("Will close in 3s... Press [YourKey] for [YourAction");
            splash.finish(&mainWindow);
            QTimer::singleShot(3000, [&app, &mainWindow]() {
                app.processEvents();
                mainWindow.show();
            });
            return app.exec();
            
            
            

            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            ademmlerA 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @ademmler said in How to get a if a KeySequence is pressed in main(); (While Application Launch):

              // Delay splash screen
              QTimer::singleShot(3000, [&splash]() {
                  splash.finish(nullptr);
              });
              

              Try this instead:

              MainWindow mainWindow;
              splash.showMessage("Will close in 3s... Press [YourKey] for [YourAction");
              splash.finish(&mainWindow);
              QTimer::singleShot(3000, [&app, &mainWindow]() {
                  app.processEvents();
                  mainWindow.show();
              });
              return app.exec();
              
              
              
              ademmlerA Offline
              ademmlerA Offline
              ademmler
              wrote on last edited by
              #6

              @Pl45m4 HI -thanks for your response.

              I have followed your advice - hopefully correct. But I do not see any "Message" and the key event does not get hit.
              I am on MacOS - maybe it is OS related ...

              #include "mainwindow.h"
              #include "mysplashscreen.h"
              
              #include <QApplication>
              #include <QPixmap>
              #include <QTimer>
              #include <QEvent>
              
              int main(int argc, char *argv[]) {
                  QApplication app(argc, argv);
              
                  // Create a pixmap to show on the splash screen
                  QPixmap pixmap(":logo");
                  QSplashScreen splash(pixmap);
                  splash.show();
                  splash.raise();
                  splash.activateWindow();
              
                  MainWindow mainWindow;
                  splash.showMessage("Will close in 3s... Press [YourKey] for [YourAction");
                  splash.finish(&mainWindow);
                  QTimer::singleShot(3000, [&app, &mainWindow]() {
                      app.processEvents();
                      mainWindow.show();
                  });
              
                  return app.exec();
              }
              
              1 Reply Last reply
              0
              • ademmlerA Offline
                ademmlerA Offline
                ademmler
                wrote on last edited by ademmler
                #7

                Ok, I changed the Splash creation to this and now I can read the message:

                QPixmap pixmap(100,100);
                pixmap.fill(Qt::white);
                QSplashScreen splash(pixmap);
                

                But still the KeyEvent does not get hit. Didi I forgot to include something important.

                jsulmJ 1 Reply Last reply
                0
                • ademmlerA ademmler

                  Ok, I changed the Splash creation to this and now I can read the message:

                  QPixmap pixmap(100,100);
                  pixmap.fill(Qt::white);
                  QSplashScreen splash(pixmap);
                  

                  But still the KeyEvent does not get hit. Didi I forgot to include something important.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @ademmler Where do you use MySplashScreen?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • ademmlerA Offline
                    ademmlerA Offline
                    ademmler
                    wrote on last edited by
                    #9

                    In main - because I want to get the KeyEvent before the MainWindow appears - see top to this thread.

                    Pl45m4P jsulmJ 2 Replies Last reply
                    0
                    • ademmlerA ademmler

                      In main - because I want to get the KeyEvent before the MainWindow appears - see top to this thread.

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #10

                      @ademmler

                      Urrgh... @jsulm is right...

                      Your splash variable has the type QSplashScreen...
                      You never create any instance of your custom MySplashScreen class, which handles the key event ;-)

                      I totally missed that... My code should work now


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      0
                      • ademmlerA ademmler

                        In main - because I want to get the KeyEvent before the MainWindow appears - see top to this thread.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @ademmler said in How to get a if a KeySequence is pressed in main(); (While Application Launch):

                        In main

                        no, you don't...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • ademmlerA Offline
                          ademmlerA Offline
                          ademmler
                          wrote on last edited by
                          #12

                          Ok guys - you have been right - but still it does not work. I changed within main() to:

                          // Create a pixmap to show on the splash screen
                          QPixmap pixmap(500,100);
                          pixmap.fill(Qt::white);
                          MySplashScreen splash(pixmap);
                          splash.show();
                          splash.raise();
                          ... and so on

                          jsulmJ Pl45m4P 2 Replies Last reply
                          0
                          • ademmlerA ademmler

                            Ok guys - you have been right - but still it does not work. I changed within main() to:

                            // Create a pixmap to show on the splash screen
                            QPixmap pixmap(500,100);
                            pixmap.fill(Qt::white);
                            MySplashScreen splash(pixmap);
                            splash.show();
                            splash.raise();
                            ... and so on

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @ademmler Maybe your splash screen does not have focus? Try to klick on it and then press a key.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • ademmlerA ademmler

                              Ok guys - you have been right - but still it does not work. I changed within main() to:

                              // Create a pixmap to show on the splash screen
                              QPixmap pixmap(500,100);
                              pixmap.fill(Qt::white);
                              MySplashScreen splash(pixmap);
                              splash.show();
                              splash.raise();
                              ... and so on

                              Pl45m4P Offline
                              Pl45m4P Offline
                              Pl45m4
                              wrote on last edited by Pl45m4
                              #14

                              @ademmler

                              This works for me:

                                  QPixmap pixmap(500,100);
                                  pixmap.fill(Qt::white);
                                  MySplashScreen splash(pixmap);
                                  splash.show();
                                  QTimer::singleShot(3000, &app, [&]() {
                                      // even with finish() outside the lamda,
                                      // but then you have to be fast with pressing the key
                                      splash.finish(&w);
                                      app.processEvents(); // needed?!
                                      w.show();
                                  });
                              

                              @jsulm clicking will make the Splash screen disappear.
                              The above code works for me on Windows without doing anything... Splash shows up, I spam ESC-key, I get dozens of Key pressed messages from my qDebug in SplashScreen's keyEvent handler.

                              Edit:

                              Weird that I don't even need the processEvents call... commented that line, still works.


                              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                              ~E. W. Dijkstra

                              ademmlerA 1 Reply Last reply
                              0
                              • ademmlerA Offline
                                ademmlerA Offline
                                ademmler
                                wrote on last edited by
                                #15

                                @jsulm Hi, when I do so the Splash window vanishes imdiatly. I tried also to use splash.setFocus ();

                                1 Reply Last reply
                                0
                                • Pl45m4P Pl45m4

                                  @ademmler

                                  This works for me:

                                      QPixmap pixmap(500,100);
                                      pixmap.fill(Qt::white);
                                      MySplashScreen splash(pixmap);
                                      splash.show();
                                      QTimer::singleShot(3000, &app, [&]() {
                                          // even with finish() outside the lamda,
                                          // but then you have to be fast with pressing the key
                                          splash.finish(&w);
                                          app.processEvents(); // needed?!
                                          w.show();
                                      });
                                  

                                  @jsulm clicking will make the Splash screen disappear.
                                  The above code works for me on Windows without doing anything... Splash shows up, I spam ESC-key, I get dozens of Key pressed messages from my qDebug in SplashScreen's keyEvent handler.

                                  Edit:

                                  Weird that I don't even need the processEvents call... commented that line, still works.

                                  ademmlerA Offline
                                  ademmlerA Offline
                                  ademmler
                                  wrote on last edited by ademmler
                                  #16

                                  @Pl45m4 Hi - thx again. I copied your code 1:1 and added "MainWindow w;" above.
                                  when I am not mistaken I should get - as you do "Any key printed" as Debug message, because I have
                                  a genial log message for any Key also.

                                  But still - being on Mac OS - I do not get a Debug() message and also, when I run in Debugger mode, the routine gets never hit.

                                  I ll try on windows - for cross test now ...

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • ademmlerA ademmler

                                    @Pl45m4 Hi - thx again. I copied your code 1:1 and added "MainWindow w;" above.
                                    when I am not mistaken I should get - as you do "Any key printed" as Debug message, because I have
                                    a genial log message for any Key also.

                                    But still - being on Mac OS - I do not get a Debug() message and also, when I run in Debugger mode, the routine gets never hit.

                                    I ll try on windows - for cross test now ...

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @ademmler Try to play with some flags like: Qt::WindowTransparentForInput and Qt::WindowDoesNotAcceptFocus. Maybe on Mac splash screen does not accept key events by default.

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • ademmlerA Offline
                                      ademmlerA Offline
                                      ademmler
                                      wrote on last edited by
                                      #18

                                      Hi, to both of you - I can approve on Windows 11 it is working as expected.
                                      Hence the question is whats needed for MacOS to get it work or is it a Bug ...

                                      @jsulm Yep I will test the flags and let you know.

                                      Pl45m4P 1 Reply Last reply
                                      0
                                      • ademmlerA ademmler

                                        Hi, to both of you - I can approve on Windows 11 it is working as expected.
                                        Hence the question is whats needed for MacOS to get it work or is it a Bug ...

                                        @jsulm Yep I will test the flags and let you know.

                                        Pl45m4P Offline
                                        Pl45m4P Offline
                                        Pl45m4
                                        wrote on last edited by Pl45m4
                                        #19

                                        @ademmler said in How to get a if a KeySequence is pressed in main(); (While Application Launch):

                                        Hence the question is whats needed for MacOS to get it work or is it a Bug ...

                                        Either bug or just how it works on Mac...

                                        See:
                                        (also includes a workaround... )

                                        • https://stackoverflow.com/questions/23728058/get-keypresses-in-qsplashscreen-on-osx

                                        maybe worth a try if you are fine with creating an empty widget/window before the splashscreen is created


                                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                        ~E. W. Dijkstra

                                        ademmlerA 1 Reply Last reply
                                        0
                                        • Pl45m4P Pl45m4

                                          @ademmler said in How to get a if a KeySequence is pressed in main(); (While Application Launch):

                                          Hence the question is whats needed for MacOS to get it work or is it a Bug ...

                                          Either bug or just how it works on Mac...

                                          See:
                                          (also includes a workaround... )

                                          • https://stackoverflow.com/questions/23728058/get-keypresses-in-qsplashscreen-on-osx

                                          maybe worth a try if you are fine with creating an empty widget/window before the splashscreen is created

                                          ademmlerA Offline
                                          ademmlerA Offline
                                          ademmler
                                          wrote on last edited by ademmler
                                          #20

                                          @Pl45m4 Hi, thx for pointing me to the stack overflow thread. I have tried two things:

                                          1. I just added this splash.grabKeyboard();:
                                            In this case the key event gets hit, when MainWindow is active.

                                          2. The solution from stack overflow is adding a "hidden" window before the Splashscreen is called.
                                            But also in this case the key event gets hit, when MainWindow is active. This would have a negative effect on the Main Application for sure.

                                          Maybe the simplest way is to create my Own "Dialog" or Framless Widget as a Splashscreen replacement ...
                                          The Event filter should be no longer active, when the dialog has been closed - right?

                                          Pl45m4P 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved