Mega Code Archive

 
Categories / Delphi / VCL
 

Sending keystroke #255 to a windows control

Question: Since the keybd_event() function only accepts key values up to 254, how can I send the keystroke #255 to a Windows control? Answer: This can be very useful for entering a value in an edit control for a foreign, symbol, or wingding character. The following example shows how to stuff the keyboard with the #255 character and direct it to any Window control. Note: This method should not be used to enter characters that could normally be processed by the keybd_event() function. procedure TForm1.Button1Click(Sender: TObject); var KeyData : packed record RepeatCount : word; ScanCode : byte; Bits : byte; end; begin {Let the button repaint} Application.ProcessMessages; {Set the focus to the window} Edit1.SetFocus; {Send a right so the char is added to the end of the line} SimulateKeyStroke(VK_RIGHT, 0); {Let the app get the message} Application.ProcessMessages; FillChar(KeyData, sizeof(KeyData), #0); KeyData.ScanCode := 255; KeyData.RepeatCount := 1; SendMessage(Edit1.Handle, WM_KEYDOWN, 255, LongInt(KeyData)); KeyData.Bits := KeyData.Bits or (1 shl 30); KeyData.Bits := KeyData.Bits or (1 shl 31); SendMessage(Edit1.Handle, WM_KEYUP, 255, LongInt(KeyData)); KeyData.Bits := KeyData.Bits and not (1 shl 30); KeyData.Bits := KeyData.Bits and not (1 shl 31); SendMessage(Edit1.Handle, WM_CHAR, 255, LongInt(KeyData)); Application.ProcessMessages; end;