I am developing one application for the PTZ, i have taken one grid, and when i press Up Arrow key then on KeyDown event of UP Arroa key i am starting my movement and on KeyUp even i m stopping the movement. But when i Pres Arrow Key its KeyDown event is called and then focus is getting moved to another control so its KeyUp is not getting called....so i want stop this focus movement on arrowkeys so that i can get both event.....so how to do that.
Asked
Active
Viewed 2,823 times
3 Answers
6
I'd suggest that you read up on the WPF Routed Events system. Specifically, if you look at tunneling or "Preview" events, you'll find that you should be able to capture and supress the key event that is causing you problems.

Dave White
- 3,451
- 1
- 20
- 25
1
I guess you could override it.
this might help: How to disable navigation on WinForm with arrows in C#?
-
He isn't using WinForms (question is tagged as WPF), so eventing is not the same. It's interesting that there is the Preview event though. – Dave White Mar 29 '11 at 21:01
-
1It was not my intent to make any question look better or worse. The answers can stand on their own merit. But your answer does direct people to a similar but incorrect approach for dealing with this situation in WPF and I felt that that clarification was worth commenting on. – Dave White Mar 30 '11 at 23:09
-
It is what it is (weak sauce) intended or not. – capdragon Mar 31 '11 at 13:23
0
You can use this code snippet from Diamonddrake as follows: Paste snippet in your form body such as other methods
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData == Keys.Right) || (keyData == Keys.Left) ||
(keyData == Keys.Up) || (keyData == Keys.Down))
{
//Do custom stuff or nothing.
//true if key was processed by control, false otherwise
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
Note that for some controls like ComboBox, using up and down keys to scroll up and down between Items, may be necessary, so if you want to utilize them, remove this part of snippet (keyData == Keys.Up) || (keyData == Keys.Down)
.

oMiD
- 322
- 4
- 20