How to get a if a KeySequence is pressed in main(); (While Application Launch)
-
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@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(); -
@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();@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(); } -
-
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.
-
In main - because I want to get the KeyEvent before the MainWindow appears - see top to this thread.
-
In main - because I want to get the KeyEvent before the MainWindow appears - see top to this thread.
@ademmler said in How to get a if a KeySequence is pressed in main(); (While Application Launch):
In main
no, you don't...
-
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 -
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 -
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 onThis 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 spamESC-key, I get dozens ofKey pressedmessages from myqDebugin SplashScreen'skeyEventhandler.Edit:
Weird that I don't even need the
processEventscall... commented that line, still works. -
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 spamESC-key, I get dozens ofKey pressedmessages from myqDebugin SplashScreen'skeyEventhandler.Edit:
Weird that I don't even need the
processEventscall... commented that line, still works.@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 ...
-
@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 ...
-
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.
@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... )maybe worth a try if you are fine with creating an empty widget/window before the splashscreen is created
-
@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... )maybe worth a try if you are fine with creating an empty widget/window before the splashscreen is created
@Pl45m4 Hi, thx for pointing me to the stack overflow thread. I have tried two things:
-
I just added this splash.grabKeyboard();:
In this case the key event gets hit, when MainWindow is active. -
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? -
-
@Pl45m4 Hi, thx for pointing me to the stack overflow thread. I have tried two things:
-
I just added this splash.grabKeyboard();:
In this case the key event gets hit, when MainWindow is active. -
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?@ademmler said in How to get a if a KeySequence is pressed in main(); (While Application Launch):
I just added this splash.grabKeyboard();:
In this case the key event gets hit, when MainWindow is active.This part of the solution is not relevant for you.
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.I was talking about this part.
Can't test it, as I don't use any Mac, but how should this influence your main window?!
You do literally nothing with this label... it's just there to somehow bypass the Mac behavior.
You don't even see it, because it has no size and is hidden when you show your main app.
Otherwise I'm out of ideas.
You could try another widget instead ofQSplashScreenbut you said, you want to use a splashscreen for your app anyway.// Create a dummy window so that we get keypresses on OSX QLabel v(0, Qt::FramelessWindowHint); v.setMinimumSize(QSize(0, 0)); v.move(0,0); v.show(); // ... // your code v.hide(); -
-
@ademmler said in How to get a if a KeySequence is pressed in main(); (While Application Launch):
I just added this splash.grabKeyboard();:
In this case the key event gets hit, when MainWindow is active.This part of the solution is not relevant for you.
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.I was talking about this part.
Can't test it, as I don't use any Mac, but how should this influence your main window?!
You do literally nothing with this label... it's just there to somehow bypass the Mac behavior.
You don't even see it, because it has no size and is hidden when you show your main app.
Otherwise I'm out of ideas.
You could try another widget instead ofQSplashScreenbut you said, you want to use a splashscreen for your app anyway.// Create a dummy window so that we get keypresses on OSX QLabel v(0, Qt::FramelessWindowHint); v.setMinimumSize(QSize(0, 0)); v.move(0,0); v.show(); // ... // your code v.hide();@Pl45m4 Yes and yes - but even if this label has been hidden the KeyEvent get still caught within the MainWindow ...
Hence if the user presses ESC, while in the MainWindow again, the same/associated action will take place again.
This is not what wanted. -
@Pl45m4 Yes and yes - but even if this label has been hidden the KeyEvent get still caught within the MainWindow ...
Hence if the user presses ESC, while in the MainWindow again, the same/associated action will take place again.
This is not what wanted.@ademmler said in How to get a if a KeySequence is pressed in main(); (While Application Launch):
even if this label has been hidden the KeyEvent get still caught within the MainWindow ...
Hence if the user presses ESC, while in the MainWindow again, the same/associated action will take place again.Have you tried it already?
Again:
I'm not able to test it, but the label does nothing, except fooling the Mac Window System to process input events (seems like it).
The label does not react on any key event or print any messages.
You just add the label to your code. Show it before you show the splash, hide it with the splash before the main window shows up.
Does it work now?