. NET WinForm keyDown event direction keys do not respond ---- C # C ++/CLI,
When performing 3D roaming, WASD and direction keys are used respectively to control the front and right sides of the angle of view. The KeyDown event records the roaming start mark. However, WASD can be accessed through Debug, but the direction keys are always inaccessible, it is strange that the KeyUp event cannot be accessed, but the KeyUp event can be responded.
However, Ctrl + and Alt + can be accessed. The analysis in many articles may be due to the fact that the arrow keys are used by default to process the Focus movement of controls and are handled by the controls themselves. There is no authoritative statement for the moment, but this problem is solved in the following routine.
Control: ProcessDialogKey: This function tells the program to handle the case by the Control itself or by writing the program.
This method is called during message preprocessing to handle dialog characters, such as TAB, RETURN, ESC, and arrow keys. this method is called only if the IsInputKey method indicates that the control is not processing the key. the ProcessDialogKey simply sends the character to the parent's ProcessDialogKey method, or returnsFalseIf the control has no parent. the Form class overrides this method to perform actual processing of dialog keys. this method is only called when the control is hosted in a Windows Forms application or as an ActiveX control.
The keys TAB, RETURN, ESC, and arrow keys are processed by the control, but can be customized after rewriting. (I only understand what he means, but I cannot translate accurately and have limited expression ability ).
The solution is that when the direction key and return false are met, the control will not be automatically processed and handed over to the KeyDown response.
C # code
protected override bool ProcessDialogKey(Keys keyData){ if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right) { return false; } else { return base.ProcessDialogKey(keyData); }}
C ++/CLI code
virtual bool ProcessDialogKey( System::Windows::Forms::Keys keyData)override{ if (keyData == System::Windows::Forms::Keys::Up || keyData == System::Windows::Forms::Keys::Down || keyData == System::Windows::Forms::Keys::Left || keyData == System::Windows::Forms::Keys::Right) { return false; } else { return __super::ProcessDialogKey(keyData); }}