Keydown event - cool down - c#

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).....

Related

Using keybd_event in user32.ddl to know of a key is pressed. In C#

I am currently working on a program, where I need to get Event's if a Key is pressed. (Even if my program is not focused / minimized.)
For that I want to use the keybd_event.
I also found some things in the internet, but nothing helpfull for me. (I also looked in the MSDN, but I didn't find it something helpfull.)
What do I need implement to get this working?
Thank you for your answers
Marcel
The keybd_event documentation states that the function should be used for generating keystrokes and not for detecting them:
Synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message.
The documentation also states that the method is deprecated (which could explain the lack of resources):
Note This function has been superseded. Use SendInput instead.
... It looks as though you need a different function.
To detect a keypress irrespective of whether the window is in focus or not, you have a few options:
Use the RegisterHotkey function to detect the pressing of a hotkey.
Use the SetWindowsHookEx function to hook the keyboard and to receive notifications whenever any key is pressed.
Use the GetKeyState function to poll the state of a key.
Each of these functions have a copious number of tutorials that are only a Google search away.

C# change event order

Edit*
ok sorry for not giving the real scenario,
actually i have datagridview created programatically, i put 2 event, which is mouseclick and ColumnHeaderMouseClick.
currently whenever user click on column header it will trigger mouseclick first then followed by ColumnHeaderMouseClick
can i change the order of the trigger? or can i know when user click i can check whether he click on column header or other place in "mouseclick event"?
I don't know of any way to do that. The events occur according to the events that are actually taking place. In other words, that would be like saying can you walk through a door without opening it first. It doesn't really make sense in the context.
Could you possibly switch the code you are calling for down and click? Or just use click and execute the events in whatever order you like there?
You cannot do this, it does not make logical sense. The MouseDown event necessarily occurs prior to the MouseClick event because the mouse button had to go down in order to initiate a click. When the mouse button goes down, a MouseDown event is raised. The MouseClick event cannot be raised until some time after that.
The order of the mouse events is explicitly documented on MSDN and cannot be modified.
And you say that this was just an example, that you are working on other events. Unfortunately, the answer will be the same regardless. The order in which events are raised is something decided by the programmer who wrote the code that raises those events. There is no mechanism for you, the consumer of the library, to change that order.
Like the mouse events discussed above, the MSDN documentation lists the order of all significant events raised by the WinForms library.
Of course, if you are writing the code that raises the events, you can always modify it to raise them in whatever order you want. But I suspect that much is obvious and not why you are asking this question.
Most C# implementations will typically fire off the event handlers in the order that they are registered to each event. However this is not enforced!
It could change at any time and as such you should not create a dependency on the order of event handlers in your application.
I strongly suggest you take a step back and review your application design for ways to remove this dependency.
As many have suggested on their answers trying to change the event order is not recommended for multiple reasons.
In order to achieve what you want, you would have to re-implement the Windows Forms controls of the .NET Framework. You can also try to hijack the Windows message processing by overriding the WndProc procedure and process directly the messages provided by the OS, but you will learn that the event firing order follows the order in which the OS sends the input provided by the device (mouse) to the affected windows/controls.
i got the solution already, i get the index of the row, if -1 its mean it is a header so i can do the if else already, no need for different event, enough with cell-click event alone can handle both scenario.
sorry for asking in complicated way when the solution is so simple, thanks for helping.

Cancelling a Click event when the click is outside of a GUI's bounds in C#

So I'm trying to create a program that will lock the host computer. The way it works so far is it has a main form, where a password can be created, and the computer can be locked. When the password is created, it's stored in a location within the user's AppData, in MD5.
I have the password creation and checking bits finished just fine, but I can't manage to figure out how to make the lock work the way I want. Normally, the computers this would be run on would be locked via Windows (i.e. WinKey+L), but a need has arisen for monitoring of a different program which will always be running. However, as at times these computers may be in a public place, I am looking for a way to only allow access to input after the user's identity has been verified.
I can prevent the program from being terminated just fine, but it's somewhat problematic to detect clicks and keystrokes outside the GUI's boundaries for me. I tried handling the Deactivate event, but that doesn't work because I can't cancel the click that happens with it. I also tried using MouseMove, but then realized that the event is only raised when the mouse is moved within the form's boundaries. I also tried listening for the Leave event, but that also didn't work.
For keystrokes, I've tried checking a KeyPress event's arguments for e.KeyChar != '\t' && e.KeyChar != '\r' (is \r what the Enter key would be?) and cancelling the event, and even just bluntly using the statement e.Handled = true;, but that doesn't work either. Either way, I can still alt-tab.
Also, with the issue of keystroke checking, I realize that I can just use a Deactivate check instead, and handle FormClosing as well; however, my Deactivate event handler doesn't work either, where the only code it contains is:
this.BringToFront();
this.Focus();
At this point, I'm sure what I'm doing for the keystroke and the deactivation handlers are small errors, but I have no idea what to do for the clicking issue. Any help would be greatly appreciated.
What you are looking for is a more powerful set of Interfaces that is provided by the operating system. You can do a platform invocation of these functions, but you have to know how to marshal the API's. This is because a lot of the libraries that allow you greater control is written in C++. If you use native C++ it is more natural to invoke these commands, but C++ requires more careful attention and will take longer to learn.
If you are looking to cancel or override keystrokes or clicks, you can do so by using global hooks. Here's an article about it http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
But it only scratches the surface on the subject. You'll have to learn a bit about windows internals and the windows API. There are some books on this topic and it is very deep.

how to call onmousedown method on windows using 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.

Check if a drag&drop is in progress

Is there any way to check if a drag and drop is in progress? Some method or win32 api which can be checked? I know I can set AllowDrop and use events but it doesn't work in this case. Basically i want to check, with code, if any drag&drop is in progress.
I had a similar question which I answered myself (after some hours messing about) See - How do I tell if a Drag Drop has ended in Winforms?.
Basically if you do as earwicker suggests you need to set the flag when the drag drop begins near the DoDragDrop call. You will need to unset the flag in both the DragDrop event and in the QueryContinueDrag if the QueryContinueDragEventArgs indicate a drop or a cancel.
The GetCapture API function might be a good start. Basically, when a drag operation starts, the source window "captures" the mouse, which means that it will still receive all mouse events even if the mouse leaves the window.
However, applications can also capture the mouse for other reasons, so this is not 100% reliable. You can try it and see how well it works for you. And with applications doing their own drag&drop handling, there's no way to be sure what is going on anyway.
What about QueryContinueDrag event handler http://msdn.microsoft.com/en-us/library/system.windows.forms.control.querycontinuedrag.aspx ?
You can hook a handler to any control and check if there is a ongoing drag&drop operation and then cancel it if you want to.
Ooops, sorry, I just saw that the guy before me already mentioned that. Me bad.
Assuming it's in the context of just your own code, you could identify all the places in your code where a drag/drop happens, and set a global boolean flag to true for the duration of the operation, then back to false after it finishes.
So the next question is, how are drag/drop operations being started in your application?

Categories