How to send mouse click event to a game application? - c++

I try to send a mouse click event to a game application. First, i use Spy++ to find what message the application receive. I see something like : WM_MOUSEACTIVATE, WM_WINDOWPOSCHANGING, WM_ACTIVATEAPP, WM_ACTIVATE, WM_SETFOCUS, ...
i try to send the same as i see on Spy++ but it doesn't work. How to send mouse click to a game application without give it focus? . it's run in window mode. Thanks in advance.

You want WM_LMOUSEDOWN. You can always check MSDN for the documentation on which messages mean what.

The best way to automate applications and games is via SendInput. While in theory it should be possible to drive an application via WM_LUBTTONDOWN etc, many applications read the key state directly via lower level APIs (such as GetAsyncKeyState) which don't change their state to reflect the messages processed from the message queue.
Using SendInput requires actually setting the game to the foreground as the input events are synthesized at a low level and are thus delivered to the active/focused window.

Related

WM_ACTIVATE and WM_SYSCOMMAND message equivalents for X11

I am currently porting a game engine from win32 to linux and was wondering about retrieving messages from the x window. As I stated in the topic I am searching for a way to intercept window messages that trigger screen saver/screen lock and minimized/maximized state, because I want to pause the rendering loop in those occations.
I guess it's something I have to manually tell X that I am interested in some particular client events and set them using the XSetWMProtocols()?
I'd appreciate some directions of what I have to look out for.
Use ScreenSaverSelectInput request / ScreenSaverNotify event from screen saver extension

Keyboard messages from child controls

I am currently developing a user interface DLL that uses the WIN32 API. The DLL must work for numerous platforms, XP, WIN CE, etc. I have managed to incorporate docking, anchoring and so on but appear to have a problem regarding owner-drawn buttons. I can draw the button's correct state, focus, clicked, default. However, I cannot receive key notifications. I specifically want to perform a click operation on a button that currently has focus, should the user press enter.
Note that I am using a windows message loop rather than a dialog message loop. I use windows hooks to hook into the window creation and set the user data to 'point' to my control instance. If I test for WM_KEYDOWN in the main message loop I can get a handle to my button control instance and could forward the message to the relevant control. Unfortunately, I am dealing with a lot of legacy code and this may not be an ideal solution.
So, my question is what is the best way forward. Is subclassing the button control's window procedure a viable option or is there an easier way?
Many thanks in advance.
The comments above are correct. The button with focus should be getting the key messages. But buttons don't (by themselves) respond to Enter--they respond to Space. It sounds like what you're missing is the typical dialog keyboard navigation, like Tab key moving the focus and Enter activating the "default" button.
If you've got a typical Windows message pump, and you want the keyboard behavior normally associated with dialogs, then you need to use the IsDialogMessage API in your message loop. This means your window is essentially a "modeless dialog".
Looks like standard window proc subclassing should do the trick. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx for details.

how to find where a mouse event come from?

Is there any way to figure out where did a mouse event come from?
I mean, if I code a C/C++ program on Windows, and get a mouse click event on it, how can I find if this event come from a mouse driver, a touchpad, or if it was send by an application (mouse event simulation by sending appropriate message like WM_LBUTTONDOWN).
Thanks for any help :)
This is not possible for an application in user mode - mouse events generally don't provide documented info on event source. There is the way to obtain some message extra info by Win32 API function GetMessageExtraInfo but there is no safe way to interpret this data. It is very device specific, undocumented and never guaranteed to ever present.
To solve this task you need to develop your own Mouse Filter driver basing on Windows DDK sample.
Its callback has input parameter MOUSE_INPUT_DATA - structure containing mouse event info. There is the field UnitId:
UnitId Specifies the unit number of the mouse device. A mouse device name has the format \Device\PointerPortN, where the suffix N is the unit number of the device. For example, a device, whose name is \Device\PointerPort0, has a unit number of zero, and a device, whose name is \Device\PointerPort1, has a unit number of one.
GetAsyncKeyState function can be used to check if the button was pressed, and unfortunately SendInput cannot trick this function.
So you can simulate a mouse click, but the program can check if the button was really pressed.
So creating your own mouse driver is better.
I needed a safe way so simulate mouse/keyboard behavior for my bot, and I wrote a detailled article on my blog http://poker-botting.blogspot.fr/2012/11/how-to-simulate-mouse-and-keyboard.html

Stop playing sound and volume when application is minimized

I was using the PlaySound function in one C++ application under Windows.
When this application is running a local var command PlaySound and it starts to play a WAV file.
But when you click on the minimize dialog box of my application, the sound continues playing, when the logical rule must be silenced or muted until you maximize the application again.
The question is:
Is there application-state within Windows to detect when application is minimized?
In order to set the waveOutSetVolume function with 0x00 = mute.
Have your app's window catch the WM_SYSCOMMAND message and check it for the SC_MINIMIZE, SC_MAXIMIZE, and SC_RESTORE notifications.
Your application will receive a WM_WINDOWPOSCHANGED message when the application window has been minimized or maximized. Check the WINDOWPOS struct in lParam to determine the new state. You can then turn the sound off or on, respectively. (Note: this message is also sent when the window is moved, sent to back, etc. So check the lParam.)
If the application is currently minimized can be checked with IsIconic.
And I don't find it logical that the sound has to stop playing when the application is minimized. It's a multitasking OS after all. I want it to do things in parallel.

C++ console keyboard events

Is there any way to get key events in a Windows console? I need a way to get keydown and keyup events quickly without a GUI. I've tried using getch(), but it doesn't get keyups and waits until a key has been pressed to return.
Use ReadConsoleInput() API. Watch for events of kind KEY_EVENT. This won't work for all keydown events (Ctrl-key, shift-key, Pause-key cannot be read), but most can be read.
Use GetNumberOfConsoleInputEvents to avoid blocking.
You can use GetKeyState or GetAsyncKeyState, but that won't give you keydown/keyup events. It will only tell you what keys are currently down.
So if you really need to get the keydown/keyup events, you could install a hook.
A Console window has a window handle that is owned by code in Windows and a message pump, also owned by code in Windows.
You can get the window handle of of the console window by using GetConsoleWindowThen install a WH_CALLWNDPROC hook using SetWindowsHookEx to listen in on messages send to the console window.
You might try a WH_MSGFILTER hook instead. I don't know if this works for console windows, but it would generate less messages to be ignored if it does work.
I was just curious, how comes such a logical question doesn't have any explanation on Google,
So one has to ask it here. So I googled for: "keyboard events console application" and
guess what ... first 2 links are interesting (but unfortunately, not exactly answers to your question):
Processing mouse / keyboard input on MSDN.
Console event handlers (like Ctrl-C and window close button).
There are a number of ways. GetKeyboardState would be one of the most obvious.
You can also try SetConsoleCtrlHandler