How to show/hide application with shortcut?



  • hi
    I'm using windows
    how can i show or hide my application by press shortcut key?
    "such as some application that show with F11 or ..."



  • Qt Developers usually use "QShortcut":http://qt-project.org/doc/qt-5.0/qshortcut.html class. It allows to set key sequence on slot.

    Example use:
    @
    QShortcut * shortcut = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")), parent);
    connect(shortcut, SIGNAL(activated()), this, SLOT(openFile()));
    @

    It's easy as 1, 2, 3.





  • [quote author="AcerExtensa" date="1340617362"]Simply use WINAPI function: RegisterHotKey -> http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309(v=vs.85).aspx[/quote]

    Why WINAPI? There is QShortcut which allows to do it on cross-platform level.



  • tucnak variant works for application-wide association, RegisterHotKey - is system wide. Have thought you are searching for system wide association...



  • i used this code
    @
    QShortcut * shwHide = new QShortcut(QKeySequence("Ctrl+Space"), this);
    QObject::connect(shwHide, SIGNAL(activated()), this, SLOT(showhide()));
    ///
    void MainWindow::showhide(){
    if (isVisible()){
    hide();
    }
    else{
    show();
    }
    }
    @

    what is Difference?



  • [quote author="AcerExtensa" date="1340617534"]tucnak variant works for application-wide association, RegisterHotKey - is system wide. Have thought you are searching for system wide association...[/quote]

    I can't understand what are you talking about. WINAPI working well on Windows, but if author want to extend it for Linux, he will add some X11 functions. But Qt has QShortcut class which allow to do it on the high-level.



  • [quote author="abbas farahmand" date="1340617665"]i used this code
    @
    QShortcut * shwHide = new QShortcut(QKeySequence("Ctrl+Space"), this);
    QObject::connect(shwHide, SIGNAL(activated()), this, SLOT(showhide()));
    ///
    void MainWindow::showhide(){
    if (isVisible()){
    hide();
    }
    else{
    show();
    }
    }
    @

    what is Difference? [/quote]

    Your code looks clear. Try to change it to it:
    @
    void MainWindow::showhide(){
    qDebug() << "Ctrl+Space called.';
    }
    @

    and check Application Output on execution.

    P.S. In other way you can implement QWidget::keyPressEvent(QKeyEvent*):
    @
    void MainWindow::keyPressEvent(QKeyEvent *e) {
    if(e->key() == Qt::Key_Space && e->modifiers() == Qt::CtrlModifier) {
    qDebug() << "called";
    return;
    }
    QWidget::keyPressEvent(e);
    }
    @



  • bq. I can’t understand what are you talking about. WINAPI working well on Windows

    1. platform is declared exactly.

    bq. I’m using windows

    1. Like I've said, I thought he need systemwide shortcut for his application. QShortcut will work only if application is activated(focused), RegisterHotKey can associate systemwide shortcut for application(like yakuake in linux or whatever). I was wrong, I'm sorry, forgive me peoples. sarcasm

    Can you understand it now?



  • [quote author="AcerExtensa" date="1340618195"]bq. I can’t understand what are you talking about. WINAPI working well on Windows

    1. platform is declared exactly.

    bq. I’m using windows

    1. Like I've said, I thought he need systemwide shortcut for his application. QShortcut will work only if application is activated(focused), RegisterHotKey can associate systemwide shortcut for application(like yakuake in linux or whatever). I was wrong, I'm sorry, forgive me peoples. sarcasm

    Can you understand it now? [/quote]

    I've understood. But I think that author want application-wide shortcut (like F11), so QShortcut will be better.

    And I said about platform too. He can begin to want to build it on Linux for example.



  • bq. But I think that author want application-wide shortcut (like F11), so QShortcut will be better.

    Whats right, after authors second post I see it too, what right choice for him is QShortcut :)

    bq. And I said about platform too. He can begin to want to build it on Linux for example.

    He can begin to want to capture the universe! Who knows? What we will do then? :) The platform was specified. :)

    Lets stop this discussion :)



  • [quote author="AcerExtensa" date="1340618982"]bq. But I think that author want application-wide shortcut (like F11), so QShortcut will be better.

    Whats right, after authors second post I see it too, what right choice for him is QShortcut :)

    bq. And I said about platform too. He can begin to want to build it on Linux for example.

    He can begin to want to capture the universe! Who knows? What we will do then? :) The platform was specified. :)

    Lets stop this discussion :)[/quote]

    OK. Thanks for discussion. Good luck.



  • [quote author="abbas farahmand" date="1340617665"]
    Your code looks clear. Try to change it to it:
    @
    void MainWindow::showhide(){
    qDebug() << "Ctrl+Space called.';
    }
    @

    and check Application Output on execution.
    [/quote]

    i changed it,but how can i check Application Output on execution?(sorry I'm not professional in qt)



  • [quote author="abbas farahmand" date="1340619266"][quote author="abbas farahmand" date="1340617665"]
    Your code looks clear. Try to change it to it:
    @
    void MainWindow::showhide(){
    qDebug() << "Ctrl+Space called.';
    }
    @

    and check Application Output on execution.
    [/quote]

    i changed it,but how can i check Application Output on execution?(sorry I'm not professional in qt)[/quote]

    You should Run (Ctrl+R) your application in Qt Creator and open in Qt Creator tab "Application Output". There will be all messages which your application prints with cout, cerr, qDebug(), qWarning(), etc.



  • thanks for your reply.
    good luck.



  • You are welcome. Good luck and happy coding!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.