PDA

View Full Version : send key even to other aplication



peter.mlich
21st May 2015, 08:27
Hi. I have bug in one aplication. It have some problem with logout in inactivity and reconnect. I need program which create key event in this aplication for stay online in it.
I have aplication with timer, buttons, key event for main aplication button...
I need code, when i can use other aplication. I cant find in examples or in google. Delphi something as this can do, i hope qt can do it too.

My code...



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

#include <QTimer>

#include <QWidget>
#include <QDesktopWidget>

#include "windows.h" keybd_event;
// key_codes https://msdn.microsoft.com/en-us/library/ms927178.aspx

class class_ppdata {
public:
bool start;
QString window;
QString timer_time;
int timer_value;
QString key;
};

class_ppdata PP_DATA;
//QList<class_ppdata> PP_DATA;
//QList<class_ppdata> PP_DATA = {"start":""; "window":""; "timer":""; "key":""};
//int timerId;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&timer, SIGNAL(timeout()), this, SLOT(TimerEvent()));
}

MainWindow::~MainWindow()
{
delete ui;
}



void MainWindow::TimerEvent()
{
// std::cout << "Timer expired." << std::endl;
//this->dialog.cancel();
PP_DATA.timer_value += 1;
//ui->label_5->setText(QString::number(PP_DATA.timer_value) );
ui->label_6->setText("Stav: casovac " + QString::number(PP_DATA.timer_value));
}


void MainWindow::on_pushButton_clicked()
{
int a;
//QTimer* timer = new QTimer(this);
if (PP_DATA.start ) //|| !timer->isActive()
{
//timerId = startTimer(1000);
a = PP_DATA.timer_time.toInt();
//ui->pushButton->setText(QString::number(a));
if (a>=1000 && a<=1200000) // 1s az 20*60 s
{
ui->pushButton->setText("Zastavit");
PP_DATA.start = PP_DATA.start ? false : true;
PP_DATA.timer_value = 0;
this->timer.stop();
timer.start(a);
}
// key pomoci QT
// QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_F1, Qt::NoModifier, "Red", 0);
// QApplication::postEvent(this, event);
// key pomoci "windows.h"
// keybd_event(VK_F1, 0, 0, 0);
// keybd_event(VK_F1, 0, KEYEVENTF_KEYUP, 0);

keybd_event(VK_F1, 0, 0, 0);
keybd_event(VK_F1, 0, KEYEVENTF_KEYUP, 0);

}
else
{
ui->pushButton->setText("Spustit");
//killTimer(timerId);
timer.stop();
PP_DATA.start = PP_DATA.start ? false : true;
}
}

void MainWindow::on_plainTextEdit_textChanged()
{
PP_DATA.window = ui->plainTextEdit->toPlainText();
}

void MainWindow::on_keySequenceEdit_editingFinished()
{
//PP_DATA.key = ui->keySequenceEdit->data;
}

void MainWindow::on_plainTextEdit_2_textChanged()
{
PP_DATA.timer_time = ui->plainTextEdit_2->toPlainText();

}

void MainWindow::on_actionMenu_triggered()
{
this->close();
}

//#include <QDesktopWidget>

void MainWindow::on_actionNapoveda_triggered()
{

/*
int WIDTH = 250;
int HEIGHT = 150;

int screenWidth;
int screenHeight;

int x, y;

QWidget w;

QDesktopWidget *desktop = QApplication::desktop();

screenWidth = desktop->width();
screenHeight = desktop->height();

x = (screenWidth - WIDTH) / 2;
y = (screenHeight - HEIGHT) / 2;

//w.resize(WIDTH, HEIGHT);
w.move( x, y );
*/

ui->label_6->setText("Stav: napoveda - event F1" + screenHeight);
}

peter.mlich
21st May 2015, 15:25
I found this, work. Now finding how get name opened window. Notepad is notepad, but firefox not firefox :) And workmate use your aplication. I cant testing on his pc.



#include "windows.h"

HWND mywindow; uint keyCode;
QString str;

str = 'notepad';
wchar_t* name = new wchar_t[str.length() + 1];
str.toWCharArray(name);
mywindow = FindWindow(name,0);
SetForegroundWindow(mywindow);
keyCode = VK_F1; // F1 key
//uint keyCode = PP_DATA.key;
PostMessage(mywindow, WM_KEYDOWN, keyCode, 0); // key down
PostMessage(mywindow, WM_KEYUP, keyCode, 0); // key up