Function not sending keystrokes

01/17/2012 23:00 DarkTwilight#1
Dear readers,

Probably a lot of fails in the coding but nevermind that please. I am still learning it. At the moment I made a simple GUI with just one function. Repeat a text someone shouted.

It will send 3 keystrokes each loop.

1. Spacebar
2. Arrow up
3. Enter key

Only thing is, the focus works, but no key strokes are send, nor am I receiving any error codes.

Could someone please be so kind to tell me what I am doing wrong? and please go easy on me since I am a C++ noob still :P

my header file:
Code:
#ifndef PROCESS_H
#define PROCESS_H
#include <QObject>
#include <QLabel>
#include <QSlider>
#include <QMessageBox>
#include <windows.h>
struct extraKeyInfo
{
    unsigned short repeatCount;
    unsigned char scanCode;
    bool extendedKey, prevKeyState, transitionState;
    operator unsigned int()
    {
	return repeatCount | (scanCode << 16) | (extendedKey << 24) |
	       (prevKeyState << 30) | (transitionState << 31);
    }
};
class initiateProcess : public QObject, public extraKeyInfo {
    Q_OBJECT
    public:
    int &amount;
    QLabel *label;
    QSlider *slider;
    initiateProcess( int &amount, QLabel *label, QSlider *slider )
	: amount( amount ), label( label ), slider( slider ) {}
    private:
    void pressKey( short code )
    {
	HWND hNotepad = FindWindow(0, L"Naamloos - Kladblok");
	if(!hNotepad){
	    QMessageBox iWindow;
	    iWindow.setWindowTitle("Window not found");
	    iWindow.setText("The window was not found");
	    iWindow.exec();
	}
	SetForegroundWindow(hNotepad);
	short vkCode = LOBYTE(VkKeyScan(code));
	extraKeyInfo lParam = {};
	lParam.scanCode = MapVirtualKey(vkCode, 29);
	PostMessage(hNotepad, WM_KEYDOWN, vkCode, lParam);
	lParam.repeatCount = 1;
	lParam.prevKeyState = true;
	lParam.transitionState = true;
	PostMessage(hNotepad, WM_KEYUP, vkCode, lParam);
	Sleep(100);
    }
    public slots:
    void startProcess( ){
	// update the label to status "executing"
	label->setText("<strong><font color=\"red\">executing</font></strong>");
	// update the amount according to the chosen
	// value with the slider / spinbox
	amount = slider->value();
	// loop through the amount of repeats
	if( amount != 0 )
	{
	    // bring the window to the front
	    HWND GameWindow = FindWindow(0, L"Naamloos - Kladblok");
	    SetForegroundWindow(GameWindow);
	    // execute the loop
	    for( int i = 0; i < amount; i++ ){
		// not the last loop so add a pause at the end
		if( i < (amount-1))
		{
		    pressKey( VK_SPACE );
		    pressKey( 't' );
		    Sleep(2000);
		}
		// last loop so dont add a pause at the end
		else
		{
		    pressKey( VK_SPACE );
		    pressKey( 't' );
		}
	    }
	}
	else
	{
	    // show a message box with an error
	    QMessageBox popup;
	    popup.setWindowTitle("NO AMOUNT CHOSEN!");
	    popup.setText("You have to chose an amount of shouts for me to repeat!\nIf you fail to do so I cannot repeat your shouts! doh, kupo!");
	    QPixmap popupIco("C:/Users/Username/Desktop/FFXIV Auto Shouter/favicon.png");
	    popup.setIconPixmap(popupIco);
	    popup.exec();
	}
	// update the label to status "finished"
	label->setText("<strong><font color=\"green\">finished</font></color>");
    }
};
#endif // PROCESS_H
my ccp file:
Code:
#include <QApplication>
#include <QWidget>
#include <QObject>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSlider>
#include <QSpinBox>
#include <QPushButton>
#include <QMessageBox>
#include <QLabel>
#include <QSound>
#include <windows.h>
#include "PROCESS.h"
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    // create a main window widget
    QWidget *main = new QWidget;
    main->setWindowTitle("FFXIV Auto Shouter");
    main->setWindowIcon(QIcon("C:/Users/ User /Desktop/FFXIV Auto Shouter/favicon.png"));
    main->setFixedWidth(325);
    // create the widgets which we will use
    QLabel *label1 = new QLabel("Amount: ");
    QLabel *label2 = new QLabel("<strong><font color=\"orange\">Configuring</font></strong>");
    QSlider *slider = new QSlider(Qt::Horizontal);
    QSpinBox *spinbox = new QSpinBox;
    QPushButton *button = new QPushButton;
    // connect the slider and the box for synchronisation
    QObject::connect(slider, SIGNAL(valueChanged(int)), spinbox, SLOT(setValue(int)));
    QObject::connect(spinbox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
    // connect the start button to the class and start
    // executing the process of repeated shouting, but first
    // we will set some options to be able to do all of this
    int amount = 0;
    initiateProcess execFunction( amount, label2, slider );
    QObject::connect(button, SIGNAL(clicked()), &execFunction, SLOT(startProcess()));
    // configurate the widgets
    slider->setRange(1, 199);
    spinbox->setRange(1, 199);
    slider->setValue(1);
    button->setText("SHOUT");
    // styling templates for the widgets
    slider->setStyleSheet("background:#000; color:#FFFFFF;");
    spinbox->setStyleSheet("background:#000; color:#FFFFFF;");
    button->setStyleSheet("background:#000; color:#FFFFFF;");
    // tooltips for the widgets
    slider->setToolTip("Select the amount of shouts you want to repeat");
    spinbox->setToolTip("Select the amount of shouts you want to repeat");
    button->setToolTip("Click here to start shouting");
    // create the bounding boxes
    QHBoxLayout *wrapper = new QHBoxLayout;
    QHBoxLayout *leftside = new QHBoxLayout;
    QVBoxLayout *rightside = new QVBoxLayout;
    // add the widgets to the boxes
    leftside->addWidget(label1);
    leftside->addWidget(slider);
    leftside->addWidget(spinbox);
    rightside->addWidget(button);
    rightside->addWidget(label2);
    // add the boxes to each other
    wrapper->addLayout(leftside);
    wrapper->addLayout(rightside);
    // set a couple of sounds
    QSound bootSND("C:/Users/User/Desktop/FFXIV Auto Shouter/greetings.wav");	// start up
    QSound executeSND("C:/Users/ User /Desktop/FFXIV Auto Shouter/execute.wav");	// starting loop
    QSound finishedSND("C:/Users/ User /Desktop/FFXIV Auto Shouter/cheer.wav");	// finished loop
    // play at startup
    bootSND.play();
    // show the widget
    main->setLayout(wrapper);
    main->show();
    return app.exec();
}
P.S. I know I shouldnt declare the full functions in the header but like I said, its just a learning process so it doesnt matter much to me. It's all about "how to link everything" and how to solve this error, which is the learning process for me :)
01/18/2012 13:38 yihaaa#2
Never declare funktions in your header O_o
01/18/2012 14:08 DarkTwilight#3
I know lol but its just a test thats why I did that haha :P
But, that cant be the reason that the function aint working right? or can it be?
Cause I receive no error message what so ever :P

Anyways, I made the code correct now. Declared everything in the cpp file now ;)
but still no error nor does it press the keys.