Manipulators

Use ENTER key like as TAB key

Posted in Manipulators

the KeyPreview property of your form to True and use WM_NEXTDLGCTL message.

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key=VK_RETURN then
    Form1.Perform(WM_NEXTDLGCTL, 0, 0);
end;

Turn on\off the Caps Lock

Posted in Manipulators

For turning the Caps Lock key see this procedure (use VN_NUMLOCK instead of VK_CAPITAL for Num Lock).

procedure TForm1.Button1Click(Sender: TObject);
var
  KeyState: TKeyboardState;
begin
  GetKeyboardState(KeyState);
  if (KeyState[VK_CAPITAL]=0) then
  begin
    // Simulate a "CAPS LOCK" key release
    Keybd_Event(VK_CAPITAL, 1, KEYEVENTF_EXTENDEDKEY or 0, 0);
    // Simulate a "CAPS LOCK" key press
    Keybd_Event(VK_CAPITAL, 1, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
  end
  else
  begin
    Keybd_Event(VK_CAPITAL, 0, KEYEVENTF_EXTENDEDKEY or 0, 0);
    Keybd_Event(VK_CAPITAL, 0, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
  end;
end;

Simulate KeyDown event

Posted in Manipulators

Use SendMessage procedure or Perform method for sending WM_CHAR message:

procedure TForm1.Button1Click(Sender: TObject);
begin
  SendMessage(Memo1.Handle,WM_CHAR,Ord('A'),0);
end;

Related 

Key press processing

Posted in Manipulators

Set form's KeyPreview property to True and do something in OnKeyPress event:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key='a' then Label1.Caption:='OK';
end;

Hook the mouse

Posted in Manipulators

This example shows a creation of a hook for the WM_RBUTTONDOWN and WM_RBUTTONUP messages of the mouse. By the first message we change color of the Panel1 and by second message we change color of the Panel2.

...
type
  TMessageList = class(TList);

var
  Form1: TForm1;
  MessageList: TMessageList = nil;
  MessageBuffer: TEventMsg;
  HookHandle: hHook = 0;
  MessageCount: Word = 0;
  Go: Boolean = False;
  Pan: array[0..5] of TPanel;

implementation

{$R *.DFM}

procedure Stop;
begin
  if Go then UnHookWindowsHookEx(HookHandle);
  MessageList.Free;
  Go:=False;
end;

function FBack(Code: Integer; wParam, lParam: LongInt): LongInt; stdcall;
begin
  Inc(MessageCount);
  Randomize;
  if MessageCount>=MessageList.Count then Stop
  else MessageBuffer:=TEventMsg(MessageList.Items[MessageCount]^);
  Result:=CallNextHookEx(HookHandle, Code, wParam, lParam);
  Pan[MessageCount].Color:=RGB(Random(255), Random(255), Random(255))
end;

procedure SetHook;
begin
  MessageBuffer:=TEventMsg(MessageList.Items[0]^);
  MessageCount:=0;
  HookHandle:=SetWindowsHookEx(WH_MOUSE, FBack, hInstance, 0);
  Go:=True;
end;

procedure MakeMessage(Mes: Cardinal);
var
  MyEvent: PEventMsg;
begin
  New(MyEvent);
  with MyEvent^ do
  begin
    message:=Mes;
    ParamL:=50;
    ParamH:=50;
    Time:=GetTickCount;
    hWnd:=Form1.Handle;
  end;
  MessageList.Add(MyEvent);
end;

function SendMouse: Integer;
begin
  try
    MessageList:=TMessageList.Create;
    MakeMessage(WM_RBUTTONDOWN);
    MakeMessage(WM_RBUTTONUP);
    SetHook;      // set hook
  except
  end;
  Result:=0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Pan[1]:=panel1;
  Pan[2]:=Panel2;
  SendMouse;
end;