Results 1 to 1 of 1

Thread: How to send a keypress to an other application (first to AtmelStudio) that running ??

  1. #1
    Join Date
    Mar 2018
    Posts
    34
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Windows

    Post How to send a keypress to an other application (first to AtmelStudio) that running ??

    I make you my apologise for my bad english, and i'm newer to programming, please read all; now:

    The OS is WINDOWS 7 Professional 64 bit and the version of Qt is Desktop Qt 5.5.1 MinGW 32 bit

    On internet i found this code to find the application that run on my computer then the goal is : send/emulate a keypress (for esample F5) to an other application (in this case to AtmelStudio) for more details go on comment line who is marked [ //**AD**// ] in file mainwindow.cpp; in

    ************************************************** ****
    ---------->THIS IS THE CODE OF FILE : TestBench.pro : <------------
    ************************************************** ****

    #-------------------------------------------------
    #
    # Project created by QtCreator 2018-03-02T17:15:10
    #
    #-------------------------------------------------

    QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = TestBench
    TEMPLATE = app


    SOURCES += main.cpp\
    mainwindow.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui

    ************************************************** ****
    ---------->THIS IS THE CODE OF FILE : mainwindow.h : <----------
    ************************************************** ****

    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "windows.h"
    #include <QKeyEvent>
    #include <QApplication>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    static BOOL CALLBACK StaticEnumWindowsProc(HWND hwnd, LPARAM lParam);
    BOOL EnumWindowsProc(HWND hwnd);

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H

    ************************************************** ****
    ---------->THIS IS THE CODE OF FILE : main.cpp : <----------
    ************************************************** ****

    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
    }

    ************************************************** *****
    ---------->THIS IS THE CODE OF FILE : mainwindow.cpp : <----------
    ************************************************** *****

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    //--------------------------------------------------------------------------------------
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    EnumWindows(StaticEnumWindowsProc, reinterpret_cast<LPARAM>(this));

    }
    //--------------------------------------------------------------------------------------
    MainWindow::~MainWindow()
    {
    delete ui;
    }
    //--------------------------------------------------------------------------------------
    BOOL MainWindow::StaticEnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
    MainWindow *pThis = reinterpret_cast<MainWindow*>(lParam);
    return pThis->EnumWindowsProc(hwnd);
    }
    //--------------------------------------------------------------------------------------
    BOOL MainWindow::EnumWindowsProc(HWND hwnd)
    {
    WCHAR title[255];
    WCHAR test[255] = L"TIMER_prova_C";
    if(GetWindowText(hwnd, title, 255)){
    ui->listWidget->addItem(QString::fromWCharArray(title));
    }
    if(wcsstr(title,test)!=0){
    ui->listWidget->addItem("----------> I FIND THE PROCESS/THREAD <-----------");
    LPDWORD id_processo;
    GetWindowThreadProcessId(hwnd,id_processo);
    QKeyEvent *event_01 = new QKeyEvent (QEvent::KeyPress,Qt::Key_F5,Qt::NoModifier);

    //**AD**//
    // this is the goal: to send a key press F5 to the application(the application is AtmelStudio)
    // that have open the file TIMER_prova_C; in listWidget it show up
    // i want to do it by using like this:
    // 1) QApplication::sendEvent(title, &event_01);
    // or by:
    // 2) QApplication::sendEvent(id_processo, &event_01);
    // In Qhelp i find that the first parameter of QApplication::sendEvent is a QObject
    // in what way i can reach goal

    }
    return TRUE;
    }
    //--------------------------------------------------------------------------------------

    The OS is WINDOWS 7 Professional 64 bit and the version of Qt is Desktop Qt 5.5.1 MinGW 32 bit

    On internet i found this code to find the application that run on my computer then the goal is : send/emulate a keypress (for esample F5) to an other application (in this case to AtmelStudio) for more details go on comment line who is marked [ //**AD**// ] in file mainwindow.cpp; in

    ************************************************** ****
    ---------->THIS IS THE CODE OF FILE : TestBench.pro : <------------
    ************************************************** ****

    #-------------------------------------------------
    #
    # Project created by QtCreator 2018-03-02T17:15:10
    #
    #-------------------------------------------------

    QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = TestBench
    TEMPLATE = app


    SOURCES += main.cpp\
    mainwindow.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui

    ************************************************** ****
    ---------->THIS IS THE CODE OF FILE : mainwindow.h : <----------
    ************************************************** ****

    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "windows.h"
    #include <QKeyEvent>
    #include <QApplication>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    static BOOL CALLBACK StaticEnumWindowsProc(HWND hwnd, LPARAM lParam);
    BOOL EnumWindowsProc(HWND hwnd);

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H

    ************************************************** ****
    ---------->THIS IS THE CODE OF FILE : main.cpp : <----------
    ************************************************** ****

    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
    }

    ************************************************** *****
    ---------->THIS IS THE CODE OF FILE : mainwindow.cpp : <----------
    ************************************************** *****

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    //--------------------------------------------------------------------------------------
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    EnumWindows(StaticEnumWindowsProc, reinterpret_cast<LPARAM>(this));

    }
    //--------------------------------------------------------------------------------------
    MainWindow::~MainWindow()
    {
    delete ui;
    }
    //--------------------------------------------------------------------------------------
    BOOL MainWindow::StaticEnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
    MainWindow *pThis = reinterpret_cast<MainWindow*>(lParam);
    return pThis->EnumWindowsProc(hwnd);
    }
    //--------------------------------------------------------------------------------------
    BOOL MainWindow::EnumWindowsProc(HWND hwnd)
    {
    WCHAR title[255];
    WCHAR test[255] = L"TIMER_prova_C";
    if(GetWindowText(hwnd, title, 255)){
    ui->listWidget->addItem(QString::fromWCharArray(title));
    }
    if(wcsstr(title,test)!=0){
    ui->listWidget->addItem("----------> I FIND THE PROCESS/THREAD <-----------");
    LPDWORD id_processo;
    GetWindowThreadProcessId(hwnd,id_processo);
    QKeyEvent *event_01 = new QKeyEvent (QEvent::KeyPress,Qt::Key_F5,Qt::NoModifier);

    //**AD**//
    // this is the goal: to send a key press F5 to the application(the application is AtmelStudio)
    // that have open the file TIMER_prova_C; in listWidget it show up
    // i want to do it by using like this:
    // 1) QApplication::sendEvent(title, &event_01);
    // or by:
    // 2) QApplication::sendEvent(id_processo, &event_01);
    // In Qhelp i find that the first parameter of QApplication::sendEvent is a QObject
    // in what way i can reach goal

    }
    return TRUE;
    }
    //--------------------------------------------------------------------------------------
    Attached Images Attached Images
    Last edited by high_flyer; 6th April 2018 at 11:24. Reason: duplicate

Similar Threads

  1. How C application send notification to Qt application on Linux
    By Rajesh.Rathod in forum General Programming
    Replies: 3
    Last Post: 28th February 2018, 14:13
  2. How to Send a command to already running process
    By Rajvesh in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2017, 06:02
  3. Replies: 10
    Last Post: 7th July 2015, 10:12
  4. Replies: 4
    Last Post: 19th November 2012, 15:35
  5. Replies: 2
    Last Post: 29th April 2010, 13:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.