键和鼠标监听,自动化

时间:2012-08-22 09:44:53

标签: c++ python keyboard mouse capture

我希望创建一个名为Dune 2000的策略游戏的叠加层。令人讨厌的是,要创建10个士兵,每次完成时都必须单击该图标。没有队列。 因此,在不干扰游戏如何工作的情况下,我想听一下鼠标移动,当在XY位置点击时,我希望重复例如十次,并在中间适当的时间。有没有允许我这样做的图书馆?

1 个答案:

答案 0 :(得分:2)

下面是鼠标右键的自动点击代码。对于鼠标左键使用 mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

bool KeyIsPressed( unsigned char k )
{
    USHORT status = GetAsyncKeyState( k );
    return (( ( status & 0x8000 ) >> 15 ) == 1) || (( status & 1 ) == 1);
}

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE P, LPSTR CMD, int nShowCmd )
{

    MessageBox( NULL, "[CTRL] + [SHIFT] + [HOME]: Start/Pause\n [CTRL] + [SHIFT] + [END]: Quit", "Instructions", NULL );
    HWND target = GetForegroundWindow();

    POINT pt;
    RECT wRect;
    int delay;
    bool paused = true;

    srand( time(NULL) );

    while ( 1 )
    {
        if ( KeyIsPressed( VK_CONTROL ) && KeyIsPressed( VK_SHIFT ) && KeyIsPressed( VK_HOME ) )
        {
            paused = !paused;
            if ( paused )
            {
                MessageBox( NULL, "Paused.", "Notification", NULL );
            }
            else
            {
                cout << "Unpaused.\n";
                target = GetForegroundWindow();
                cout << "Target window set.\n";
            }
            Sleep( 1000 );
        }

        // Shutdown.
        if ( KeyIsPressed( VK_CONTROL ) && KeyIsPressed( VK_SHIFT ) && KeyIsPressed( VK_END ) )
        {
            MessageBox( NULL, "AutoClicker Shutdown.", "Notification", NULL );
            break;
        }

        if ( paused == false && GetForegroundWindow() == target )
        {
            GetCursorPos( &pt );
            GetWindowRect( target, &wRect );

            // Make sure we are inside the target window.
            if ( pt.x > wRect.left && pt.x < wRect.right && pt.y > wRect.top && pt.y < wRect.bottom )
            {
                mouse_event( MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0 );
                mouse_event( MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 );
            }
            delay = (rand() % 3 + 1) * 100;
            Sleep( delay );
        }
    }

    return 0;
}