C + + Capture key combinations

Source: Internet
Author: User

The VC view class program can receive onkeydown messages, but on the dialog it needs to PreTranslateMessage handle the key messages.

And the System Key ALT,F10 also need special treatment.

or directly on the code:


BOOL Ctestdlg::P retranslatemessage (msg* pMsg)
{
Todo:add your specialized code here and/or call the base class
CString str;
CString STRMSG;
int valid_down=0;

static int shift=0;
int capital=0;
static int ctrl=0;

static int alt=0;
static int f10=0;
Short key_down=0;
Short alt_down=0;
Short f10_down=0;
Short ctrl_down=0;

By detecting the message, you know that the key status of these keys is as follows:
The 1th time Press is 0xffffff81, but it is 1.
The 2nd time Press is 0XFFFFFF80, but it is 0.
Repeat the above logical process later
Therefore, the lowest 0x0f is not easy to determine, to determine the high level can be


Key_down = Getkeystate (vk_menu) & 0XFF00;//81 80
if (Key_down) alt=1;
else alt=0;


Key_down = Getkeystate (vk_shift) & 0XFF00;//81 80
if (Key_down) shift=1;
else shift=0;

Key_down = Getkeystate (Vk_control) & 0XFF00;//81 80
if (Key_down) ctrl=1;
else ctrl=0;

F10 2 Reads
Key_down = Getkeystate (VK_F10) & 0XFF00;//81 80
if (Key_down) f10=1;
else f10=0;

Str. Format ("%0x,m=%d,w=%d,l=%ld", Key_down,pmsg->message,pmsg->wparam,pmsg->lparam);//pMsg->wParam);
SetWindowText (str);

Because after pressing the ALT key, and then pressing other keys, there is no KeyDown message, is still wm_syskeydown, but Pmsg->wparam is the key code system
For the sake of simplicity, here remember that the ALT F10 and other system keys, if there are still other keys, then through the Pmsg->wparam to determine
There is no system button to detect WM_KEYDOWN, so the three are parallel relationships

if (CTRL | | | ALT | | SHIFT)
{
if (pmsg->message==260) return true;//have other keys pressed, return directly, wait for the bounce to be processed
if (CTRL)
{//ctrl key is pressed, it is not a general key, or the F10 key bounces, it returns directly
if ((pmsg->message!=257 && pmsg->message!=261)) return true;//have other keys pressed, return directly, wait for the bounce to be processed
}

}

if (Pmsg->message==wm_keyup | | CTRL | | ALT | | SHIFT | | F10)//detection keys bounce up
{

if (0x8000 & Getkeystate (vk_shift)) shift=1;
if (0x8000 & Getkeystate (Vk_control)) ctrl=1;
if (0x0080 & Getkeystate (Vk_menu)) alt=1;
if (Getkeystate (vk_capital)!=0) capital=1;
F1-f24
if (pmsg->wparam>=0x70 && pmsg->wparam<=0x87)
{
if (pmsg->wparam>=0x70 && pmsg->wparam<=0x78)//f1-f9
Str. Format ("f%c", pmsg->wparam-0x70+ ' 1 ');
else if (pmsg->wparam>=0x79 && pmsg->wparam<=0x7b)//f10-f12
Str. Format ("f1%c", pmsg->wparam-0x79+ ' 0 ');
else if (pmsg->wparam>=0x7c && pmsg->wparam<=0x84)//f13-f21
Str. Format ("f2%c", pmsg->wparam-0x7c+ ' 3 ');
else if (pmsg->wparam>=0x85 && pmsg->wparam<=0x87)//f22-f24
Str. Format ("f2%c", pmsg->wparam-0x85+ ' 2 ');

Valid_down=1;
}
A-Z
if (pmsg->wparam>= ' A ' && pmsg->wparam<= ' Z ')
{
if (SHIFT | | Capital)
Str. Format ("%c", Pmsg->wparam);
Else
Str. Format ("%c", pmsg->wparam-' a ' + ' a ');
Valid_down=1;

}
0-9
if (pmsg->wparam>= ' 0 ' && pmsg->wparam<= ' 9 ')
{//Normal numeric keypad
Str. Format ("%c", pmsg->wparam-' 0 ' +48);
Valid_down=1;
}
0-9
if (pmsg->wparam>=0x60 && pmsg->wparam<=0x69)
{//numeric keys on keypad
Str. Format ("%c", pmsg->wparam-0x60+48);
Valid_down=1;
}


Switch (Pmsg->wparam)
{
Case Vk_return:
Str= "Enter";
Valid_down=1;
Break
Case Vk_back:
Str= "Back";
Valid_down=1;
......

}
if (valid_down==1)//Active key
{
if (capital) str= "CapsLock +" +STR;
if (shift) str= "SHIFT +" +STR;
if (ALT) str= "ALT +" +STR;
if (ctrl) str= "CTRL +" +STR;
GetDlgItem (idc_button1)->setwindowtext (str);

}

}
Return CDialog::P retranslatemessage (PMSG);
}

C + + Capture key combinations

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.