Hi, I have been making a macro building program. I am trying to handle mouse events right now. I have made a program that hooks the mouse, then puts information into a text file which is then read by another program. The recorder writes the delay in milliseconds, so the events don't just happen at one time. It then writes the type of the event. Then it writes the other information like X and Y, right mouse button, wheel, intensity, etc. . I have no idea if this is a good way to do this or not, or how to read the file so I can simulate mouse events. Please help.:icon_neutral:

The code is attached. I'm using Dev-C++ 4.9.9.2.
Here is some output from the program that records the mouse:

1922 MMOVE 530 437
1938 MMOVE 529 437
3078 MWHEEL 120
7422 MDOWN R
7594 MUP R
11031 MMOVE 529 436
11094 MMOVE 529 435
11172 MMOVE 529 435
11188 MMOVE 529 434
11250 MMOVE 529 434
11860 MMOVE 528 434
11875 MMOVE 528 434
11953 MMOVE 527 434
12547 MDOWN L
12625 MUP L

Dani AI

Generated

This thread shows a compact recorder/playback approach; the core problems to solve for reliable playback are robust parsing, correct coordinate mapping, accurate timing, and using a modern input-injection API that behaves consistently across DPI/multi-monitor setups. The following points extend ’s recorder and tie into suggestions from and without repeating their exact snippets.

Parsing and storage

  • Keep the on-disk format self-describing: a small header/version, then one event per record with an enum type and a flexible payload. Text is fine for debugging; binary is fine for production (but include versioning and fixed-width integer types).
  • Parse lines with a tokenizer (getline + istringstream or similar), validate token counts per event type, and store either absolute timestamps (high-resolution ticks) or deltas. Record whether coordinates are screen or client-relative so playback can map them back correctly.

Playback and injection

  • Prefer SendInput over SendInput’s deprecated alternatives. SendInput supports absolute, virtual-desktop mapping, and multiple events batched in one call. Convert pixel coordinates to SendInput’s 0..65535 absolute space using the screen (or virtual desktop) size:
INPUT in = {};
in.type = INPUT_MOUSE;
in.mi.dx = (x * 65535) / (screenW - 1);
in.mi.dy = (y * 65535) / (screenH - 1);
in.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
SendInput(1, &in, sizeof(in));
  • Wheel deltas match Windows WHEEL_DELTA (120 per notch). Use MOUSEEVENTF_WHEEL with the recorded delta.

Timing and reliability

  • Use a high-resolution clock (QueryPerformanceCounter) and either a high-resolution waitable timer or timeBeginPeriod(1) + Sleep for millisecond timing. Raise playback thread priority while replaying to reduce jitter.
  • Compress or interpolate long sequences of move-only events if the recorder floods disk; too many tiny moves can be coalesced by the OS and hurt accuracy.
  • Finally, handle focus and elevation caveats: injected input won’t reach the secure desktop and SetForegroundWindow behavior can be restricted — record target hwnds and map client-to-screen when appropriate.

These additions bridge the simple recorder idea to a robust player suitable for real-world apps while keeping debugging and format flexibility in mind.

Recommended Answers

All 4 Replies

you want to read it? easy.
*ahem*

use an ifstream object on the file, read it into an event_info structure (like struct event_info { int something;string event;int x;int y; } )

and simulate the events (mouse_event for mup/mdown, SetCursorPos() for move)

*walks away now*

funnily enough, I have just finished making the exact same thing :), take a look at it if you like, its pretey cool and allows you set set playback speeds & more. Hopefully I will also help you with your problem.

My one saves all the data in pure binary, so it is a bit more compressed than the way your trying to do it. It saves each event in the following structure:

[B]eventID[/B]   [B]keyCode[/B]   [B]x[/B]   [B]y[/B]   [B]delay[/B]

Each variable uses exactly 2 bytes.

I have attached the project, I forgot to mention that its not a console application, but a Win32 API project.
Sorry about my un-neat / un-commented code :P

commented: You always give me something I am looking for. Great! +1

Ive gone through mine, formatted and commented it to make it easier to understand, also fixed a couple big problems in it.

Hope it helps :P

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.