Question and Answer Database FAQ1337D.txt Controlling shortcut keystrokes in a string grid Category :Windows API Platform :All Product :All 32 bit Question: I have a string grid that is read only. I am trapping any keystrokes sent to the string grid. I have a button that has a caption of E&xit. When ever the user presses the x key (even if the alt key is not pressed) in the string grid, the button click is executed. How can I prevent this? Answer: The following example demonstrates trapping the CM_DIALOGCHAR message at the form level. This will give you an opportunity to respond to a dialog character only if the alt key is held down, preventing the default handeling of the character shortcuts. Example: type TForm1 = class(TForm) Button1: TButton; StringGrid1: TStringGrid; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure StringGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); private { Private declarations } procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin Button1.Caption := 'E&xit'; end; procedure TForm1.Button1Click(Sender: TObject); begin Application.Terminate; end; procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin ShowMessage('Grid keypress = ' + Char(Key)); Key := 0; end; procedure TForm1.CMDialogChar(var Message: TCMDialogChar); begin if ssAlt in KeyDataToShiftState(Message.KeyData) then inherited; end; 7/16/98 4:31:28 PM
Last Modified: 01-SEP-99