Event Handling of a background program - events

Is there a way to let my program handle events like mouse movements and key strokes although the program is working in the background?!
Example : the user is working on notepad and I want my program to handle the events happening over there.

You can install mouse and keyboard hooks.

Related

Running a Windows process while controlling the mouse and keyboard

I have a Windows program which has a GUI that runs on a PC.
In order to automate some of the GUI actions, I want to be able to move the mouse and type using the keyboard, but without interfering with the user's activity.
I know that I could simulate input events using SendMessage and PostMessage, but that requires the window to be in focus, and I want to eliminate this requirement.
My question is - is it possible to implement sort of a 'wrapper' that internally runs the original program, while patching its mouse and keyboard, providing it with a 'virtual' version of a mouse of keyboard?
I think of that as taking only the mouse and keyboard capabilities of a VM. Is something of that kind exists?
Thanks!

No mousewheel events when using touchpad

I'm having troubles retrieving mousewheel events when my program is running on my laptop and when I'm scrolling using the touchpad.
I was initially using DirectInput to catch input events, but I've read here and there that DirectInput wasn't able to handle scroll events sent by touchpads.
I did some extra researchs and fell on this old topic: C++ DirectInput Mouse Scroll Wheel with a Laptop Touchpad
So i've tried to use a PeekEvent loop to catch my mouse inputs. Everything wen fine when using a real mouse, but when I switched to my laptop, ta-da: no WM_MOUSEWHEEL events received. (And this guy predicted it )
I don't receive any WM_VSCROLL or WM_GESTURE event either.
Additionnaly I've made another program based on wxWidgets and in this case, the mouse wheel events are propery catched by the application. I've parsed the source code to see how wxWidgets retrieve windows events and, except if I'm missing something, it seems to be the exact same code as mine.
Is there some kind of voodoo magic trick to catch mouse wheel events generated by a touchpad?
I can provide more informations about my code if needed.
Thanks
EDIT:
I did some extra debug to find what's going on:
First, I was wrong saying I don't catch WM_MOUSEWHEEL event at all. In fact, in the WindowProc Callback I actually receive wheel events.
However, the PeekMessage call doesn't return any event.
I could eventually change the way I collect mouse events to do it directly in the WindowProc callback, but I'll need to do some weird stuff just to handle something that should be working the same way using both a real mouse or a touchpad.

WH_KEYBOARD_LL hook doesn't capture input in own process

I'm using a low-level keyboard hook (WH_KEYBOARD_LL) to disable certain input, such as Alt-Tab. I create the hook on a thread with a message pump, so I can properly handle the notifications.
The hook's callback function is able to process keyboard events whenever I'm not focused in the window that created the hook (i.e. my main window), but as soon as I activate that window, no events show up in the hook until I deactivate the window again and the input instead propagates to the window's WindowProc.
Does anybody have any clue what's going on here?
UPDATE: So, it turns out this behavior is caused by also registering for raw input in the same process. Apparently, using raw input causes my low-level keyboard hook to be disabled whenever my process’s window is focused. Does anybody know why and how to work around this?
Windows doesn't call low-level keyboard hooks if the most recently registered hook (aka the first hook to be executed) comes from a process that registered itself for raw keyboard events.
So a workaround is to create a second low-level keyboard hook in another process afterwards. Yes, this will cause both low-level keyboard hooks to be executed even when the focus is on a window from the first process.
Bad for performance, and who knows what Windows will bodge next - so I'm not really endorsing it - but it works.

How to detect KeyPress while program is running in background in Win32 C++

I got a program that whenever I minimize it, it goes to the system tray.
i want to know is this:
a) how could i detect a key press while my program is in the system tray.
b) how could I know what they press in the keyboard specifically the function buttons.
You need to set up a keyboard hook using SetWindowsHookEx(). Look at the WH_KEYBOARD and WH_KEYBOARD_LL hooks.
If you know exactly what keystroke you're expecting, you can use RegisterHotkey and Windows will send you a message when that key is pressed.
If you want to detect all keystrokes, #OJ's answer will work.

VC++ mouse events

I want to write a console program for mouse events (Only mouse scroll). How do I do it in VC++? The application will listen only to scroll events.
Description: If the user scrolls down, the Desktop window fades down, and fades-in when user scrolls up.
Here I just need to know to to listen to mouse events in console app.
Note: I am developing using win32 API, and for development environment I am using VS2010.
I've never actually done this myself. It seems that a console application responding to mouse events almost belies its nature and intended purpose. Generally, you would only need to respond to keyboard input from a console app and leave the mouse stuff to a GUI app.
That being said, this tutorial indicates that it is in fact possible to capture these mouse events from a Win32 console application. Generally, the suggestion is to use the ReadConsoleInput function and extract the information of interest from the INPUT_RECORD structure that it fills. The only tricky thing is that the call to ReadConsoleInput is a blocking call, which means it will not return until there is an input event fired. You'll need to structure your application's code accordingly. Mouse events are covered in detail about 3/4 of the way down the page.

Resources