C++ Builder Tutorials

C++ Builder: Keyboard and Mouse events

Keyboard

When an ASCII key is pressed (such as A or 7 or space), a KeyPress event occurs.
You can use the resulting event handler to make something happen. Example:

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, System::WideChar &Key)
{
  // do something with Key
}

The Keyparameter of the OnKeyPress handler gives the ASCII code of the key that was pressed. This also includes control characters like #13 for carriage return, #3 for Control+C, and so on.

Keys or key combinations that do not correspond to an ASCII value (the SHIFT key or F1, for example) do not generate an OnKeyPress event. They only generate a KeyDown or KeyUp event. To respond to non-ASCII keys or key combinations, use the OnKeyDown (or the OnKeyUp) event handler. Example:

void __fastcall TForm1::Edit1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
  // do something with Key and Shift
}

Note that the Key parameter of the OnKeyDown handler is a number that indicates the code of the key that was pressed, such as 13 for the return key, 27 for escape, and so on.
The Shift parameter gives the state of the modifier keys. It is a set of flags that can include ssShift, ssAlt, ssCtrl or a combination of these.

Note that when a KeyPress event occurs, also a KeyDown event occurs, usually followed somewhat later by a KeyUp. 


Mouse

A MouseDown event occurs when the user presses a mouse button with the mouse pointer over a control. Use the OnMouseDown event handler to handle any additional processing. Example:

void __fastcall TForm1::Edit1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
   int X, int Y)   { ... }

The OnMouseDown event handler can respond to left, right, or center mouse button presses and to shift key plus mouse-button combinations.
The Shift parameter is a set of flags that can include ssShift, ssAlt, ssCtrl or a combination of these.
X and Y are the coordinates of the mouse pointer, in relation to the client area of the Sender (for example the distance from left and top of a TEdit control).


Simulating mouse click

To simulate a mouse click on a control that is shown on a form of your application, use it's Click() method.
Example:

Button2->Click();

To simulate a click outside the window of your application, a click anywhere on the screen:

1. Firstly, set the cursor position with the function SetCursorPos.
2. Next, use the function mouse_event to simulate mouse clicks.


Sending keys to external application

To send key strokes to an external application:

1. Activate the window of the external application.
2. Use function keybd_event() to send the "virtual key scan" code of every keyboard key that you want to simulate, specifying if it is a "key-down" or "key-up" event.


Code examples

A simple keypress:

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, System::WideChar &Key)
{
  Label1->Caption = Key;
}

Responding to a keydown event:

void __fastcall TForm1::Edit1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
  Label1->Caption = "Key: " + IntToStr(Key);
  if (Shift.Contains(ssShift))
    Label1->Caption = "Shift + " + Label1->Caption;
  else if (Shift.Contains(ssAlt))
    Label1->Caption = "Alt + " + Label1->Caption;
  else if (Shift.Contains(ssCtrl))
    Label1->Caption = "Ctrl + " + Label1->Caption;
}

Responding to a mouse click event (click on a TEdit component):

void __fastcall TForm1::Edit2MouseDown(TObject *Sender, TMouseButton Button,
  TShiftState Shift, int X, int Y)
{
  if (Button == mbLeft)
    Label2->Caption = "Left button";
  else if (Button == mbMiddle)
    Label2->Caption = "Middle button";
  else if (Button == mbRight)
    Label2->Caption = "Right button";

  if (Shift.Contains(ssShift))
    Label2->Caption = "Shift + " + Label2->Caption;
  else if (Shift.Contains(ssAlt))
    Label2->Caption = "Alt + " + Label2->Caption;
  else if (Shift.Contains(ssCtrl))
    Label2->Caption = "Ctrl + " + Label2->Caption;
}

Simulate clicking somewhere on the screen:

void __fastcall TForm1::btnClickScreenClick(TObject *Sender)
{
  int x = 300; // screen coordinate
  int y = 30;; // screen coordinate
  SetCursorPos(x, y);
  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  mouse_event(MOUSEEVENTF_LEFTUP,0, 0, 0, 0);
}

Sending some text to the application NotePad:

void __fastcall TForm1::btnSendkeysClick(TObject *Sender)
{
  HWND W;
  String S = "the text";
  char C;
  int i;
  ShellExecute(NULL, L"open", L"notepad.exe", NULL, NULL, SW_SHOWNORMAL);
  Hide(); // hide the form
  W = GetForegroundWindow();
  ShowWindow(W, SW_SHOWNORMAL);
  Sleep(500);
  for (i = 1; i <= S.Length(); i++) {
    C = S[i];
    keybd_event(VkKeyScan(C), 0, 0, 0);
    keybd_event(VkKeyScan(C), 0, KEYEVENTF_KEYUP, 0);
  }
  Sleep(3000);
  Show(); // show form again
}