PDA

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



andreaQt
4th March 2018, 16:17
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;
}
//--------------------------------------------------------------------------------------