how to call onmousedown method on windows using c# - c#

i need to perform the action done by using left mouse click when i press W for example:
i want to move the cruse to a folder and then press w so the folder open
i want to move the cruse to a file and press w and drag it and drop when i release w
i tried to use windowsFrom and performing the onKeyPress() and it calls onMouseDown() but of course it means that the onMouseDown() function will be called inside the form and what i need is a system behavior,,,, i am using windows 7 64 on VS2010 but i would like if it is some thing global for all windows
thanks a lot for helping,,,

The first step you'll need to capture Key Down events in an external application is define a global hotkey:
I recommend checking out some of the answers for this question for a global hot-key: Best way to tackle global hotkey processing in c#? or this CodeProject article: A Simple C# Global Low Level Keyboard Hook
Then, as M. Babcock said you'll need to send the mouse-down events. The link he provided in the comment is a good one. I'm repeating it here for completeness.
How to simulate Mouse Click in C#?
If you actually need to send keyboard inputs, you can use InputSimulator which is something I have advocated (and used in the past) and it provides a very flexible (and reliable) wrapper that is capable of simulating keyboard events.
It wraps SendInput under the hood but abstracts away all the PInvoke calls and other complexity.
InputSimulator.SimulateKeyDown(VirtualKeyCode.CTRL);
InputSimulator.SimulateKeyPress(VirtualKeyCode.KEYS_V);
InputSimulator.SimulateKeyUp(VirtualKeyCode.CTRL);
or
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);

Use a boolean flag for the clicking, and set it on both by clicking or pressing your hotkey. For instance, this flag should be declared inside the form, and the events KeyDown and MouseDown set it true, while KeyUp and MouseUp set it false. For all your actions now you use this flag for verifying the click. (Not sure if it would works in Windows app, in XNA it works fine). For using outside the class, just set this flag as a public property.

Related

How can I capture a key press, outside of the form?

I have been trying to capture the keys pressed outside of my winform, but obviously a KeyPress event won't work.
I haven't been able to get any closer than the KeyPress event, which only works on the form level, as specified
I suspect that I will have to do the
[DllImportAttribute("user32.dll")]
, but I have little to no experience with that.
Being able to capture key presses anywhere requires using Hooks.
There is a library on CodePlex which simplifies implementing Application and Global Mouse and Keyboard Hooks for C# users.

Control OS Mouse Event using Windows Form - C#

The program I am trying to do is to simulate the mouse event of a operating system using keyboard with Windows Form. Right now I am able to change the cursor and do different actions like mouse clicking inside the Form (when the Form is on the Top).
The problem is I would like to extend it to the whole Operating System, which means even if my Windows Form is not at the top, I am still able to control my cursor and do all sorts of mouse event on other applications while the Form is running. How should I do to implement this ?
You might want to look at this library Global System Hooks in .NET which uses global system hooks to detect all mouse and keyboard events include those outside of your application.
You can synthesize OS-wide keystrokes, mouse motions, and button clicks using the Win32 SendInput() API. You can call it from C# using P/Invoke. Sample code can be found here: SendInput on PInvoke.net.
I remember back in the day I used the SendInput (and a screenshot API) to create a Minesweeper bot in C# (2.0 I think). It could solve an Expert puzzle in about one second. I wish I still had the source code to sample here, but I don't.
EDIT: It appears someone has already created a nice .NET warpper for the SendInput(): Windows Input Simulator on CodePlex.

Keydown event - cool down

I have a project with keydown event, but as every keypress, i click on the key and if i kip clicking it, it will wait a half second and start spam quickly the key. I need it to spam with no cool down, what can i do?
This is called the Keyboard Repeat Delay, and it's a system-wide property that can be set in the Keyboard section in the Control Panel. Alternately, you can set it via code, using the SystemParametersInfo Win32 API function, setting the SPI_SETKEYBOARDDELAY flag.
To call it from C#, you probably need to define a P/Invoke signature, but luckily someone on PInvoke.net has done this for us already.
Don't forget that you are setting a system-wide setting! This might require admin privileges, and in any case, you should play nice and return it to the original setting once you're done.
Instead of changing the system-wide settings and still have a delay of 250ms, you can watch keydown and keyup events for the same key (don't forget that a user can press multiple keys at once and release them in different order). Start a timer with required frequency on keydown, and stop it on the keyup, and set your previous keydown handler as a timer handler.
Try using Reactive Extensions and use one of the time-related operator such as Sample or Interval to achieve what you need here.
http://msdn.microsoft.com/en-us/data/gg577609
As an example (just as a guide, typed without VS)
Observable.FromEventPattern<KeyPressEventArgs>(this, "KeyPress").Sample(500).....

Creating a Process in the background that will listen to keyboard

I have a project that runs in the background in a different process, I want it to be able to react to keyboard everywhere, for example I run the project, and afterwards I do other stuff in the computer such as browsing, facebook, watching movies etc.., and every time I press F9 I want my project to show up. Same as how you press a combination of keys to invoke Babylon... I want to implement it in C#, I have no idea how to begin.
You can register a hotkey with the RegisterHotKey API function. You can see an example of its usage from C# here.
I think you need to write a system-wide keyboard hook, check here for details:
https://stackoverflow.com/a/1764434/559144
How do I grab events for all applications? An example system-wide hook.

Detecting mousewheel over non-focused window?

My goal is to make a floating toolbar (as its own C# application), and when the user uses the scrollwheel over me I want to change the buttons that are visible. Sounds easy enough, should just be a matter of this one-liner:
MouseWheel += new MouseEventHandler(Form1_MouseWheel);
The problem I am having is that the mouse wheel handler is only invoked when my application has focus. That means the user has to first click, and then mousewheel. That won't do for what I'm trying to do.
I can hook the MouseHover event handler and call form.Activate() then, to get focus. That's suboptimal because if the user uses the scrollwheel immediately after mousing over my application (instead of waiting a little), the focus will still be on the previous app and it'll get the mousewheel event.
A natural thing to do would be to hook the MouseEnter event and call Activate() there, but then instead of my application coming to the front, its icon starts to blink on the task bar. I'm using Win7, but this problem is probably older than this.
Ideally, what I'd like to do would be to detect the mousewheel events without having to worry about whether my application has focus. It would really be better for the previous application to keep input focus, so for example if the user's in Notepad they can type, mouse over to my app, use the scroll wheel, look at what they see and decide to just resume typing in Notepad. Ideally I don't want them to have to click even once in this scenario.
I'll settle for a solution that switches focus to my application, though, if there's no other way.
What I have so far uses C# and Windows Forms, but I'd be open to using something different if that can solve my problems.
So: how can I see those mousewheel events without the user having to click to focus my application first?
If you need to catch mouse events outside your application, you can use a global system hook. There's a good .NET implementation here

Categories