How can I detect key combinations?
Applies To: C++Builder 1 or higher
Category: Knowledge Base
- The Key parameter shows me which key has been pressed. But if I use key combinations such as Ctrl + A, Key contains only A.
-
- Start a new project using File | New Application.
-
- Double-click the OnKeyDown event:
-
- void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
- TShiftState Shift)
- {
- if(Shift.Contains(ssCtrl) && Key == 'A')
- ShowMessage("You pressed Ctrl + A");
- }
-
- TShiftState indicates the state of the Alt, Ctrl, and Shift keys and the mouse buttons.
-
- TShiftState seems easy to use but it supports only Alt, Ctrl, and Shift. How can I detect special key combinations such us left arrow button + up arrow button?
-
- Start a new project using File | New Application.
-
- Double-click the OnKeyDown event:
-
- void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
- TShiftState Shift)
- {
- if(GetAsyncKeyState(VK_LEFT) < 0 &&< 0 &&
- GetAsyncKeyState(VK_UP) < 0)< 0)
- {
- ShowMessage("Left arrow + Up arrow");
- }
- }
-
- The GetAsyncKeyState Win32 API function determines whether a key is up or down.
C++Builder Developer's Network
Copyright © Yoto Yotov