Results 1 to 2 of 2

Thread: send key even to other aplication

  1. #1
    Join Date
    May 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default send key even to other aplication

    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...
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QTimer>
    5.  
    6. #include <QWidget>
    7. #include <QDesktopWidget>
    8.  
    9. #include "windows.h" keybd_event;
    10. // key_codes https://msdn.microsoft.com/en-us/library/ms927178.aspx
    11.  
    12. class class_ppdata {
    13. public:
    14. bool start;
    15. QString window;
    16. QString timer_time;
    17. int timer_value;
    18. QString key;
    19. };
    20.  
    21. class_ppdata PP_DATA;
    22. //QList<class_ppdata> PP_DATA;
    23. //QList<class_ppdata> PP_DATA = {"start":""; "window":""; "timer":""; "key":""};
    24. //int timerId;
    25.  
    26. MainWindow::MainWindow(QWidget *parent) :
    27. QMainWindow(parent),
    28. ui(new Ui::MainWindow)
    29. {
    30. ui->setupUi(this);
    31. connect(&timer, SIGNAL(timeout()), this, SLOT(TimerEvent()));
    32. }
    33.  
    34. MainWindow::~MainWindow()
    35. {
    36. delete ui;
    37. }
    38.  
    39.  
    40.  
    41. void MainWindow::TimerEvent()
    42. {
    43. // std::cout << "Timer expired." << std::endl;
    44. //this->dialog.cancel();
    45. PP_DATA.timer_value += 1;
    46. //ui->label_5->setText(QString::number(PP_DATA.timer_value) );
    47. ui->label_6->setText("Stav: casovac " + QString::number(PP_DATA.timer_value));
    48. }
    49.  
    50.  
    51. void MainWindow::on_pushButton_clicked()
    52. {
    53. int a;
    54. //QTimer* timer = new QTimer(this);
    55. if (PP_DATA.start ) //|| !timer->isActive()
    56. {
    57. //timerId = startTimer(1000);
    58. a = PP_DATA.timer_time.toInt();
    59. //ui->pushButton->setText(QString::number(a));
    60. if (a>=1000 && a<=1200000) // 1s az 20*60 s
    61. {
    62. ui->pushButton->setText("Zastavit");
    63. PP_DATA.start = PP_DATA.start ? false : true;
    64. PP_DATA.timer_value = 0;
    65. this->timer.stop();
    66. timer.start(a);
    67. }
    68. // key pomoci QT
    69. // QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_F1, Qt::NoModifier, "Red", 0);
    70. // QApplication::postEvent(this, event);
    71. // key pomoci "windows.h"
    72. // keybd_event(VK_F1, 0, 0, 0);
    73. // keybd_event(VK_F1, 0, KEYEVENTF_KEYUP, 0);
    74.  
    75. keybd_event(VK_F1, 0, 0, 0);
    76. keybd_event(VK_F1, 0, KEYEVENTF_KEYUP, 0);
    77.  
    78. }
    79. else
    80. {
    81. ui->pushButton->setText("Spustit");
    82. //killTimer(timerId);
    83. timer.stop();
    84. PP_DATA.start = PP_DATA.start ? false : true;
    85. }
    86. }
    87.  
    88. void MainWindow::on_plainTextEdit_textChanged()
    89. {
    90. PP_DATA.window = ui->plainTextEdit->toPlainText();
    91. }
    92.  
    93. void MainWindow::on_keySequenceEdit_editingFinished()
    94. {
    95. //PP_DATA.key = ui->keySequenceEdit->data;
    96. }
    97.  
    98. void MainWindow::on_plainTextEdit_2_textChanged()
    99. {
    100. PP_DATA.timer_time = ui->plainTextEdit_2->toPlainText();
    101.  
    102. }
    103.  
    104. void MainWindow::on_actionMenu_triggered()
    105. {
    106. this->close();
    107. }
    108.  
    109. //#include <QDesktopWidget>
    110.  
    111. void MainWindow::on_actionNapoveda_triggered()
    112. {
    113.  
    114. /*
    115.   int WIDTH = 250;
    116.   int HEIGHT = 150;
    117.  
    118.   int screenWidth;
    119.   int screenHeight;
    120.  
    121.   int x, y;
    122.  
    123.   QWidget w;
    124.  
    125.   QDesktopWidget *desktop = QApplication::desktop();
    126.  
    127.   screenWidth = desktop->width();
    128.   screenHeight = desktop->height();
    129.  
    130.   x = (screenWidth - WIDTH) / 2;
    131.   y = (screenHeight - HEIGHT) / 2;
    132.  
    133.   //w.resize(WIDTH, HEIGHT);
    134.   w.move( x, y );
    135. */
    136.  
    137. ui->label_6->setText("Stav: napoveda - event F1" + screenHeight);
    138. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: send key even to other aplication

    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.

    Qt Code:
    1. #include "windows.h"
    2.  
    3. HWND mywindow; uint keyCode;
    4. QString str;
    5.  
    6. str = 'notepad';
    7. wchar_t* name = new wchar_t[str.length() + 1];
    8. str.toWCharArray(name);
    9. mywindow = FindWindow(name,0);
    10. SetForegroundWindow(mywindow);
    11. keyCode = VK_F1; // F1 key
    12. //uint keyCode = PP_DATA.key;
    13. PostMessage(mywindow, WM_KEYDOWN, keyCode, 0); // key down
    14. PostMessage(mywindow, WM_KEYUP, keyCode, 0); // key up
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Restart aplication with some proprieties from the previous
    By SamuelNLP in forum Qt Programming
    Replies: 2
    Last Post: 13th March 2013, 17:44
  2. How execute a script in a Qt aplication?
    By Gerard in forum Qt for Embedded and Mobile
    Replies: 8
    Last Post: 7th September 2011, 15:11
  3. Simple CAD aplication - how to
    By tommnaj in forum Newbie
    Replies: 3
    Last Post: 5th April 2011, 23:47
  4. Aplication just works where it was compile
    By metRo_ in forum Qt Programming
    Replies: 7
    Last Post: 13th April 2010, 00:02
  5. Creating a STANDALONE aplication
    By Alienxs in forum KDE Forum
    Replies: 4
    Last Post: 17th August 2006, 17:02

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.