How to judge the effective button pressing

Source: Internet
Author: User
Tags time in milliseconds

Author: Zhu Jincan

Source:Http://blog.csdn.net/clever101

 

In game pole programming, you usually need to obtain the button status to execute specific events, such as pressing the button1Once, variableNumIncrement100But this often happens when you debug the system: you can press the button once, but the variableNumIt has increased by several hundred. What's going on? At first, I was a bit puzzled. HoweverProgramAnd thinking, I found the reason. In the original program, I used a clock event and set a clock, which indicates that every50Obtain the button status of the player in milliseconds.CodeAs follows:

Settimer (hdlg, 0 , 50 , Null );

At the same timeOntimerWhen the judgment button in the function is pressed, I use the following judgment statement:

  // JS. rgbbuttons [0] indicates button 1, JS. rgbbuttons [0] & 0x80 indicates that button 1 is being pressed
If (JS. rgbbuttons [ 0 ] &   0x80 )
{
Num = Num + 100 ;
}

 

Later I thought there was a problem with this judgment condition. Where is the problem? The main reason is that the real-time status update is not taken into account.Press the button once, but the variableNumWhen the number of increments is several hundred? This is because the duration (that is, the interval from when a button is pressed to the time when it is popped up) of a person is uncertain, which may be dozens of milliseconds or seconds, however, the time for the system to obtain the button status is determined (no matter how many seconds your button has been pressed, the system is50Obtain the button status once in milliseconds.100In milliseconds, the system thinks that you have already pressed the button2Times. A direct solution is to change the response time of the clock event from50Change the time in milliseconds to several seconds. If you press the button later than this time, it will be considered valid. However, I don't think this is an effective solution because it cannot prove that it is appropriate to set the response time of the clock event to a long time. I thought of a more effective way, that is, to redefine the effective press: The effective press is that the button is currently in the pop-up state, but its previous status is press, the specific code is:


/* !
\ Brief button status enumeration variable, down is the press status, up is the pop-up status

*/
Enum Status
{
Down =   0 ,
Up
};

Then define a private data member in the dialog box class for obtaining the player status:

Status m_btn1prestatus; // Used to save the previous status of the button

Class constructor initialization in the dialog boxM_btn1prestatusIs in the pop-up status:

M_btn1prestatus = Up;

ModifyVariableNumIncrement100Part of the Code:

Code // If the current button is pressed and its status changes to pop-up, change its status to press
If (JS. rgbbuttons [ 0 ] &   0x80 ) && (M_btn1prestatus = Up ))
{
M_btn1prestatus = Down;
}
// If the current button is in the pop-up state and its front state is pressed, num increments and its front state is changed to the pop-up state.
Else   If (JS. rgbbuttons [ 0 ] &   0x80 ) = 0 ) && (M_btn1prestatus = Down)
{
Num = Num + 100 ;
M_btn1prestatus = Up;
}

The experience gained this time is that the real-time performance must be considered when obtaining the hardware status.

 

 

 

 

 

 

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.