Creating/posting input events in C# - c#

Is it possible to programmatically create input events in c#.
I want to be able to simulate any input, such as pressing a button on the keyboard or moving the mouse pointer from my application. The events generated by the application need to behave (preferably) in exactly the same way that an actual key press for instance does (post an event to the system event queue that is given for applications to process).
I seem to be unable to find any documentation about doing this from c# code after googling for an hour.
As always, any answers will be greatly appreciated.

You can try SendKeys for keyboard input. For more control, and mouse events too, you should P/invoke SendInput().
Be warned that this is difficult to get right and you may find alternative approaches more amenable.

Related

C# Hooking specific Keyboard keystroke and stop system propagation

I have an application where I have multiple keyboards connected through USB.
I need to hook a specific keyboard to get the keypress directly into the software, even if this one is not on the foreground. This so far works based on this project. The other keyboards shall work as normal.
Although it seems by using Direct Input, it is not possible to stop propagation (we don't want other apps to get that particular keyboard input).
Concerning Global Hook, it can block the keypress system-wise, but it is impossible, as far as I know, to identify the source of the keystroke (which keyboard it is from) and thus to selectively block them.
There is another project, here, that combines the two, but it is quite messy and heavy.
Is there a better way to achieve this? I am surprised that simple task is so complex.
If you are going to make the app windows only, you should look into Windows raw input api
It isn't that complicated.

Listening to keyboard C# (WinForm)

I'm writing a program that I want to run when the user is "idle" (I.e. No input from the user - not moving the mouse, or typing anything). I have taken care of detecting if the mouse is moving (through storing the mouse position as a variable and comparing the current position to that variable at a later time), but what I can't figure out is how to detect keyboard input.
I know that if the user is typing in the form itself, I can use event listeners (KeyDown, KeyPressed, etc.), but I want this program to detect keyboard input anywhere on the computer, in any program.
Is there a way to accomplish this? All my research has yielded ways to check inside the form, but not globally.
Note: Not sure if this matters, but I don't care what the user is typing (what keys are being pressed). All I need to know is that there is keyboard activity.
Thanks in advance!
What you're looking for is considered a Global Keyboard Hook.
A sample with explanation and source:
here
Edit: In terms #FᴀʀʜᴀɴAɴᴀᴍ answer, it would be improper to grab using GetLastInputInfo as system calls (SendInput(), keybd_event(), etc) all will trigger it as well, so that doesn't mean the user is technically there.

Event when dragging over valid drop target outside of app?

I have a very simple method that writes a file locally but I only want to fire it if the user looks like they are going to drop outside of the app because firing it every time they start dragging would result in lots of unnecessary files being written.
So my question is: if a user drags something within the app outside of the app, is it possible to detect when they drag over a valid drop target (e.g. the desktop or windows explorer)?
EDIT: At a more general level, my question is: how can I respond to mouse/drag events that occur outside of my app?
Not entirely sure what it is you're exactly trying to achieve, but this may help:
WPF: Drag and drop virtual files into Windows explorer
For the most part the drag / drop events should fire regardless of where you're dropping to (I think), but you can certainly be notified when a drop has been performed.
As #Quarzy stated, unless you're in communication with the other app, there may be no direct way of testing for data that the underlying windows drag / drop system doesn't expose.
More specifically that question points to this article: http://blogs.msdn.com/b/delay/archive/2009/11/04/creating-something-from-nothing-asynchronously-developer-friendly-virtual-file-implementation-for-net-improved.aspx
I post this purely because I wonder if maybe it may lead to other things, apart from that you might be able to get the Hwnd of the control under the cursor - possibly http://social.msdn.microsoft.com/Forums/windows/en-US/3df9fc84-4020-4ae1-8b4f-942bce65568f/find-the-object-under-the-mouse?forum=winforms as a starting point.
There may then be a way to query whether that particular control is a valid drop target through interop as well.
Good luck!
This might be a possible answer for your question: Register a global hook to detect whether mouse dragging files/text
How ever the following suggestion might help (Require you to create c++ external lib):
Capture other possible processes window message (Global hook WH_GETMESSAGE) (See this link How to Create a global WH_GETMESSAGE HOOK without DLL)
Listen for WM_DROPFILES
see the following link: http://www.experts-exchange.com/Programming/Languages/CPP/Q_10203575.html
You have also an helping answer here How can I capture mouse events that occur outside of a (WPF) window?
But I do not see any way to detect if the target is valid, as long as you have no communication with the potential targets.

Is it possible to block keyboard input selectively in Windows? How?

My app (in C#) need to interface with a USB bar-code scanner, which is basically working like a keyboard. It inputs the bar-code with an enter key at the end.
The app need to be work even when it's at background, so I am using low level keyboard hook to get and filter the bar-code out in the global key events. This part is already working.
Here is my problem: I don't want other apps to get the keyboard(scanner) inputs if it is a bar-code. And the normal key events should not be interfered. In one word, block the key events selectively. Is this possible?
My app is in C#, but I have no problem with C++ or more native solutions as long as it's easy to integrate in C#.
Thanks.
Additional Information:
The whole idea is working at background, even when it's not active. It watches the global key events stream and spot the bar-code sequence (already implemented with Hook). And most importantly, it do NOT interfere with normal keyboard events nor other applications' operation. That's why I cannot block all the key events or make it top-most.
I already can get the bar-code. I need to prevent other applications from getting the bar-code.
At the end of your keyboard hook you would call CallNextHookEx to execute next hook in the chain.
I would suggest that put some unique signature as a preamble for your barcode so that your keyboard hook procedure can detect it as a valid barcode input from your scanner. Now, when you get this data, just skip the call to 'CallNextHookEx' so that the chain will be discontinued and other programs won't get your barcode. Otherwise - call 'CallNextHookEx' so the chain can continue.
Note: This is my theory, I have never tried the exact same thing myself. I have however, written hooks in C++ and C#.
Check this project out
http://globalmousekeyhook.codeplex.com/
It is in C# as well so will make your coding easier. Sounds like all you need is to hook up the global key press event and suppress it by setting the Handled value or something similar.

Any other method to simulate user mouse input than WinAPI?

I know there's been some questions around this topic, but the ones I'd like to ask are somewhat more specific:
Does there exist any other method of simulating mouse movement than throwing WinAPI calls all over the place? If yes, is it any good compared to wrapping these calls in my own class?
For the record: by "mouse movement", i mean all the click and repositioning, preferably, but not absolutely necessary, with visible animation of cursor moving between positions.
Back in DOS programming I had dynamically grabbed the interrupt which you can't do the same way under protected mode but I am pretty sure that DirectInput also has some functions somewhere that would be different than the Win32 way
There are alternative libraries for input handling which I would think the SDL has and possibly GLFW but it may be a stretch to go adding those to a project for mouse handling.
Correction: SDL uses DirectInput on windows.
I don't know how much more agreeable you find .NET than win32 but there is one other way too. I was sure I had a bookmark somewhere to a library that would fit perfectly =/ where is it...
.NET way, not bad if you're already using .NET especially:
MSDN Reference Page
As far as clicking goes for that solution, just send a message to your main loop or where you are already detecting clicks instead of setting mouse click. Though I'm sure there are other libraries with the function out there.
UPDATE: Check out http://wiki.osdev.org/Mouse_Input for some information on more direct usage and driver writing. This and, as it turns out, use of INT 33h is also possible under windows (and other more dev-friendly OSes) with some effort!
If you can live with only simulating clicking on the various controls, try manually triggering the controls' Click event, or, for buttons you can use Control.PerformClick().

Categories

Resources