MFC keyboard long press executes multiple times and executes once

Source: Internet
Author: User

Let's start by explaining what happens when you press a keyboard to not release: Multiple pressed messages are generated, and the content of the message is the same as the key you pressed.


Implementation of long-press time to execute multiple times: for example, you long press the "A" button, you can make the interface of a control of the value has been increased, then you just in the implementation of this button in response to the event to increase one time, as long as you press, automatically will call you many times this function.


Implement long press execution once: this according to the Internet, you can add a static variable to record which key was last pressed. The main code is as follows


(1) Adding a static variable to a class

Private
Static WPARAM Last_key;

(2) The value of the initialization variable in the source file is a non-keyboard value for the first keystroke response

WPARAM Crobotclientdlg::last_key =-1;

(3) In response to the time button press the time to determine whether to press the first time

if (Wm_keydown = = pmsg->message)

if (Pmsg->wparam = = Vk_left && pmsg->wparam! = Last_key)

{

Last_key = pmsg->wparam;

DoSomething

}

Release the Control key
if (Wm_keyup = = pmsg->message)

if (M_use_keyboard_contrl = = True && Pmsg->wparam = = vk_left)
{
Last_key =-1;

DoSomething

}

Note that there are several bugs that you might write to

(1) If you do not reply to the Last_key mark when you release the button, then you have to control the keyboard to perform a number of times can be executed only once, because there are no other keys in the meantime will change the value of Last_key

(2) If Last_key is set to a static variable of a function, it should also be possible, because the static variable of the function is initialized only once, and the subsequent sentence is not executed.

MFC keyboard long press executes multiple times and executes once

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.