0
votes

I have code that works on CMD where i can type 1 or 2 to run a C script to either click infinitely, or press F infinitely. I am trying to make a GUI, but the code seems to be really funky and new to me, possibly because it is more C++ based. The goal of the gui is to enter 1 or 2, and then press the button to let the while loop run.

#include <windows.h>
#include <stdio.h>
#include <random>


void LeftClick();
namespace Project6 {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for MyForm
    /// </summary>
    public ref class MyForm : public System::Windows::Forms::Form
    {
    public:
        MyForm(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MyForm()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^ label1;
    private: System::Windows::Forms::TextBox^ textBox1;
    private: System::Windows::Forms::Button^ button1;


    protected:

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(37, 63);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(197, 13);
            this->label1->TabIndex = 0;
            this->label1->Text = L"Press 1 for mouse click or 2 for key click";
            this->label1->Click += gcnew System::EventHandler(this, &MyForm::label1_Click);
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(72, 79);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(100, 20);
            this->textBox1->TabIndex = 1;
            this->textBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox1_TextChanged);
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(82, 105);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 2;
            this->button1->Text = L"button1";
            this->button1->UseVisualStyleBackColor = true;

            // 
            // MyForm
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Center;
            this->ClientSize = System::Drawing::Size(271, 203);
            this->Controls->Add(this->button1);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->label1);
            this->Name = L"MyForm";
            this->Text = L"Auto Press";
            this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
    }
    private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
    }
    private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
    }


    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

        String^ in = textBox1->Text;

        int ini = System::Convert::ToInt16(in);

        INPUT ip;
        while (TRUE)
        {   // Pause for 1 seconds.
            int output = rand() % 1000 + 1;
            Sleep(output);
            // Set up a generic keyboard event.
            ip.type = INPUT_KEYBOARD;
            ip.ki.wScan = 0; // hardware scan code for key
            ip.ki.time = 0;
            ip.ki.dwExtraInfo = 0;

            if (ini == 1)
            {
                ip.ki.wVk = 0x41;//F
            }
            if (ini == 2)
            {
                LeftClick();
            }
            ip.ki.dwFlags = 0; // 0 for key press
            SendInput(1, &ip, sizeof(INPUT));

            // Release the "A" key
            ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
            SendInput(1, &ip, sizeof(INPUT));
        }
    }
    };

}
void LeftClick()
{
    INPUT    Input = { 0 };
    // left down 
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    ::SendInput(1, &Input, sizeof(INPUT));

    // left up
    ::ZeroMemory(&Input, sizeof(INPUT));
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    ::SendInput(1, &Input, sizeof(INPUT));
}

error 1:

LNK2028: unresolved token (0A00028F) "extern "C" unsigned int __stdcall SendInput(unsigned int,struct tagINPUT *,int)" (?SendInput@@$$J212YGIIPAUtagINPUT@@H@Z) referenced in function "void __cdecl LeftClick(void)" (?LeftClick@@$$FYAXXZ)

error 2:

error LNK2019: unresolved external symbol "extern "C" unsigned int __stdcall SendInput(unsigned int,struct tagINPUT *,int)" (?SendInput@@$$J212YGIIPAUtagINPUT@@H@Z) referenced in function "void __cdecl LeftClick(void)" (?LeftClick@@$$FYAXXZ)
2
Unrelated: You may find that the error messages in the full build output of the Output tab (found not far from the Error List tab) works better in Stack Overflow posts because it it plain text. As an added bonus, the full build output often contains additional information you can use to make your job easier.user4581301
Welcome to Stack Overflow. Please read the help pages, take the SO tour, read about how to ask good questions, as well as this question checklist. Lastly please learn how to create a minimal reproducible example to show us, with emphasis on the minimal part.Some programmer dude
so i found out SendInput is giving me the errors, i dont know how to prevent that thoughpapaya

2 Answers

0
votes

You need to import the function into your project: -

[DllImport("user32.dll", SetLastError = true)] private static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);

Put the above into your code, as shown here Send keys through SendInput in user32.dll

As a separate issue, it is a bad idea to put while loops(or any other kind of loop forever code) inside the events of controls.

Instead launch a timer(or something else that works in the background) so that the event is not locked up in a permanent loop.

Locking up the event will make your whole form unresponsive to almost all other user input.

0
votes

You need something like this:

#pragma once

#include "Windows.h"
#pragma comment(lib, "user32.lib")

namespace .....