Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 06:25

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Function not sending keystrokes

Discussion on Function not sending keystrokes within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 20
Join Date: May 2007
Posts: 1,166
Received Thanks: 82
Question Function not sending keystrokes

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
DarkTwilight is offline  
Old 01/18/2012, 13:38   #2
 
elite*gold: 0
Join Date: Nov 2009
Posts: 343
Received Thanks: 45
Never declare funktions in your header O_o
yihaaa is offline  
Old 01/18/2012, 14:08   #3
 
elite*gold: 20
Join Date: May 2007
Posts: 1,166
Received Thanks: 82
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.
DarkTwilight is offline  
Reply


Similar Threads Similar Threads
How to send keystrokes to a game
01/14/2012 - C/C++ - 13 Replies
Dear reader, I am recently starting to learn C++. I have the following problem: I build this GUI program which should repeat my shouts by a simple method. Send a "space", then "up key", then "enter key" command. Only this is, that it will not respons if the window is not focussed.
Simulating Keystrokes
11/18/2011 - General Gaming Releases - 6 Replies
Hi folks, usually I am not releasing my stuff. But here is a small hint for "do it yourself" people, how to simulate keystrokes for Warhammer Online. The usual way to do that via Postmessage or kbdevent does NOT work here, because Warhammer is a DirectInput game. It cost me some hours to figure out a proper but easy way how to do it: INPUT input;
Help with Postmessage, Sending keystrokes.. !
04/12/2011 - AutoIt - 6 Replies
Hello everyone , i use this sourcecode that i found for sending keys on a minimized window. ; #FUNCTION# ================================================== ================================================== ================ ; Name...........: SimulKey ; Description ...: Simulate a Key-Send to a specified handle in the Background ; Author ........: Felix Lehmann ; Modified.......: If you modify this Script, please enter your name here ; Remarks .......: - ; Related .......: - ; Parameters...
Aion 1.9.0.3 sending keystrokes in background mode
08/29/2010 - Aion - 1 Replies
Hello to all! I'am trying to send keystrokes in BACKGROUND MODE to AION client using C++ (Builder2009): I do it like that: ... PostMessage(Window,WM_KEYDOWN, VK_OEM_MINUS, 0x00020001); ... This function works good for me, but as i can see i can't send WASD or VK_MENU(ALT) and more, something like game protection
The difference between hard/software keystrokes
02/17/2009 - 9Dragons - 2 Replies
Hi all, I recently joined so forgive my noobies questions I was wondering how Keystrokes made by software and hardware are different. The reason for this the "Logitech Revolution mouse" can asign other Keys to it's Buttons even keyboad keys how come Gameguard allows this? Does Gameguard only block what it knows or is this only partly the case?



All times are GMT +2. The time now is 06:25.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.