How can I detect special key presses?
Applies To: C++Builder 1 or higher
Category: Knowledge Base
- As described in How can I detect character key presses?, I can check the state of character keys. But what about other keys such as Enter, Tab or Ctrl?
-
- Start a new project using File | New Application.
-
- Double-click the OnKeyDown event:
-
- void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
- TShiftState Shift)
- {
- if(Key == VK_RETURN)
- ShowMessage("You pressed Enter");
- }
-
- While the OnKeyPress event is designed only for ASCII keys, the OnKeyDown event lets you trap all the possible keys. To check, the name of a key, you'll need to use virtual-key codes. For more information, open the Win32 Programmer's Reference help file and go to the section Virtual-Key Codes.
-
- Virtual-key codes
- · VK_LBUTTON: Left mouse button
- · VK_RBUTTON: Right mouse button
- · VK_CANCEL: Control-break processing
- · VK_MBUTTON: Middle mouse button (three-button mouse)
- · VK_BACK: BACKSPACE key
- · VK_TAB: TAB key
- · VK_RETURN: ENTER key
- · VK_SHIFT: SHIFT key
- · VK_CONTROL: CTRL key
- · VK_MENU: ALT key
- · VK_PAUSE: PAUSE key
- · VK_CAPITAL: CAPS LOCK key
- · VK_SPACE: SPACEBAR
- · VK_PRIOR: PAGE UP key
- · VK_NEXT: PAGE DOWN key
- · VK_END: END key
- · VK_HOME: HOME key
- · VK_LEFT: LEFT ARROW key
- · VK_UP: UP ARROW key
- · VK_RIGHT: RIGHT ARROW key
- · VK_DOWN: DOWN ARROW key
C++Builder Developer's Network
Copyright © Yoto Yotov