. NET WinForm keyDown event direction keys do not respond ---- C # C ++/CLI,

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.