Intercept keystrokes to a window - winapi

Is it possible to intercept a keystroke (and characters) sent to a window? By intercept, I mean play man-in-the-middle, instead of having just hooks onto the Window.
I'd like to filter (i.e. eliminate some keystrokes) keystrokes to a window.

Use SetWindowsHookEx(). Just eliminating keystrokes can be done with a simple WH_KEYBOARD_LL hook. It is not a global hook so is easy to get right. Googles very well too.

Related

During XGrabKey(board), discover which window had been focused

A program has called XGrabKey() to make a hotkey.
The user presses that key combination (while another window is focused).
The program receives control to do something in response to the key combination. Meanwhile, the program has been temporarily focused (because of the effects of XGrabKey (see man XGrabKey, man XGrabKeyboard)).
I want the program to create a synthetic X event (a keypress or mouse click) to the originally focused window. In some cases this means I need to focus that window before sending it the event (Firefox ignores synthetic events when it is not focused), which means I need to know which window it is. How can I find out which window it is?
Wait for the next FocusOut event, verify that the mode is set to NotifyUngrab, get the focus with XGetInputFocus(), and send away your synthetic events.

block ALL keyboard access, mouse access and keyboard shortcut events

In order to block ALL keyboard access, mouse access and keyboard shortcut events in one of my projects, I:
Created a full screen transparent borderless window, in front of other windows, but invisible.
Handle all keyboard and mouse events with simple return; the window itself.
Make the window modal [NSApp runModalForWindow:myWindow] in order to block keyboard shortcuts.
Release window from touchpad's gesture events only.
But this guy made it look simple in a tiny app -MACIFIER:
How did he do it?
not really sure if this would be usable, but you could use the program hotkeynet (generally used for gaming, but I have had success using other methods) and map every single key/mouse action to do nothing. I did something similar by blocking access to a specific program with it in about 20-30 minutes.
not sure if it will help; but it might be the solution you need?
I believe you can use Quartz Event Services. In particular, have a look at CGEventTapCreate, and note the 4th parameter, which allows you to specify what kinds of events you'd like to intercept. The available kinds of events are listed in the CGEventType enum.
If you set your tap to be an active filter, returning NULL from the callback will delete the event.

Simulating a drag & drop operation using SendInput

Can SendInput be used to simulate a drap & drop operation?
I've got an application that accepts files of a certain format that are dropped on it, but not from the command line, and I want to associate it with a file. I thought I'd create a small tool that finds the window, and simulates a drag & drop of the file - is this at all possible? Do I need to use SendInput or possibly SendMessage? What would be the parameters?
Yes, pretty likely. SendInput injects mouse events at a very low level. SendMessage won't work.
You'll need a thread since DoDragDrop is a blocking call. Fake the mouse down first, start the thread, call DoDragDrop. The thread should sleep to give enough time for DoDragDrop to get started, then fake mouse move and mouse up. Keep fingers crossed that it works the first time, it is impossible to debug if it doesn't.
The shell already has a function that simulates a drop: SHDoDragDrop, no need for hacks like faking mouse input.
Since you are talking about the commandline, XP added support for simulating D&D for applications/registered file types: How do I accept files to be opened via IDropTarget instead of on the command line?

How can I fire a key press or mouse click event without touching any input device at system level?

How can I fire an automatic key press or mouse click event when a color appears on the screen
on other application or browser?
It depends a lot on what you want. Do you want to send the keys to
your Application
another fixed Application
Simulate a global keypress
Simulating keys globally
All of these will cause problems targeting a specific application and the active window changes.
SendKeys Sends Messages to the active app. It's a high level function taking a string which encodes a sequence of keys.
keybd_event is very low level and injects a global keypress. In most cases SendKeys is easier to use.
mouse_event simulates mouse input.
SendInput supersedes these functions. It's more flexible but a bit harder to use.
Sending to a specific window
When working with a fixed target window, sending it messages can work depending on how the window works. But since this doesn't update all states it might not always work. But you don't have a race condition with changing window focus, which is worth a lot.
WM_CHAR sends a character in the basic multilingual plane (16 bit)
WM_UNICHAR sends a character supporting the whole unicode range
WM_KEYDOWN and WM_KEYUP Sends keys which will be translated to characters by the keyboard layout.
My recommendation is when targeting a specific window/application try using messages first, and only if that fails try one of the lower level solutions.
when a color appears on the screen on other application or browser
I made one program using OpenCV and C++ for operating mouse with finger gesture. I used 3 color strips for 3 mouse function.
Yellow color for Left click
Blue color for Right click
Pink color for controlling cursor position
Whenever camera detect these colors, associated function takes place, I have used mouse_event for performing mouse function.
For more information you may read my code, blog, video.
I'm not 100% sure what you want, but if all you are after is running the method linked the the button.Clicked event, then you can manually run the method just like any other method.
You can use the .NET SendKeys class to send keystrokes.
Emulating mouse clicks requires P/Invoke.
I don't know how to detect colors on the screen.

Is it possible to tell Scintilla to ignore certain keystrokes and pass them to the parent window?

I would like Scintilla to ignore certain key combinations like, Ctrl+Enter or Ctrl+D, and to notify the parent window when they are entered. I read through the documentation and could not figure out how to do this. Is this possible?
Well, if all else fails you could subclass the Scintilla control's window procedure. It would be fairly straightforward to intercept the WM_KEYDOWN and WM_KEYUP messages, filter them, and then pass them either to the main Scintilla window proc or to your parent window.
There are two options, really. Hooking into WM_KEYDOWN, as suggested, is one. The other is to use an accelerator table (see http://msdn.microsoft.com/en-us/library/ms645526(VS.85).aspx) to translate the keypresses into higher-level command IDs and process the command IDs in your WM_COMMAND handler.
Using the accelerator table is undoubtedly the "right" way, but WM_KEYDOWN seems to work just as well, and doesn't require changing the message loop code/tracking down the magic framework function that needs to be overridden/etc.
(If using MFC, the magic framework function for window-specific accelerator tables is CWnd::PreTranslateMessage. Override it, call TranslateAccelerator in there (passing in the accelerator table that is loaded in the constructor/OnCreate/etc.) and return FALSE -- if TranslateAccelerator returned 0 -- or TRUE -- if it returned something else. This allows the use of keyboard shortcuts that are specific to Scintilla windows.)
By the way, both these methods coexist quite happily, so some keypresses can be handled with accelerators and some with WM_KEYDOWN. My last Scintilla program did this; I totally can't remember why, I'm afraid, but it certainly worked fine.

Resources